01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.meta;
20:
21: import java.util.Comparator;
22:
23: import org.apache.openjpa.enhance.PersistenceCapable;
24: import org.apache.openjpa.kernel.OpenJPAStateManager;
25: import org.apache.openjpa.conf.OpenJPAConfiguration;
26: import org.apache.openjpa.util.ImplHelper;
27:
28: /**
29: * Order by a field in the related type in memory.
30: *
31: * @author Abe White
32: */
33: class InMemoryRelatedFieldOrder implements Order, Comparator {
34:
35: private final FieldMetaData _rel;
36: private final boolean _asc;
37: private final OpenJPAConfiguration _conf;
38:
39: public InMemoryRelatedFieldOrder(FieldMetaData rel, boolean asc,
40: OpenJPAConfiguration conf) {
41: _rel = rel;
42: _asc = asc;
43: _conf = conf;
44: }
45:
46: public String getName() {
47: return _rel.getName();
48: }
49:
50: public boolean isAscending() {
51: return _asc;
52: }
53:
54: public Comparator getComparator() {
55: return this ;
56: }
57:
58: public int compare(Object o1, Object o2) {
59: if (o1 == o2)
60: return 0;
61: if (!(ImplHelper.isManageable(o1))
62: || !(ImplHelper.isManageable(o2)))
63: return 0;
64:
65: PersistenceCapable pc1 = ImplHelper.toPersistenceCapable(o1,
66: _conf);
67: PersistenceCapable pc2 = ImplHelper.toPersistenceCapable(o2,
68: _conf);
69: OpenJPAStateManager sm1 = (OpenJPAStateManager) pc1
70: .pcGetStateManager();
71: OpenJPAStateManager sm2 = (OpenJPAStateManager) pc2
72: .pcGetStateManager();
73: if (sm1 == null || sm2 == null)
74: return 0;
75:
76: Object v1 = sm1.fetchField(_rel.getIndex(), false);
77: Object v2 = sm2.fetchField(_rel.getIndex(), false);
78: if (v1 == v2)
79: return 0;
80: if (v1 == null)
81: return (_asc) ? -1 : 1;
82: if (v2 == null)
83: return (_asc) ? 1 : -1;
84: int cmp = ((Comparable) v1).compareTo(v2);
85: return (_asc) ? cmp : -cmp;
86: }
87: }
|