01: package org.drools.common;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * 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, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import org.drools.FactHandle;
20: import org.drools.base.ShadowProxy;
21: import org.drools.util.AbstractHashTable.ObjectComparator;
22:
23: public class IdentityAssertMapComparator implements ObjectComparator {
24: /**
25: *
26: */
27: private static final long serialVersionUID = 400L;
28:
29: public IdentityAssertMapComparator() {
30: }
31:
32: public int hashCodeOf(final Object obj) {
33: Object realObject = obj;
34: if (realObject instanceof FactHandle) {
35: realObject = ((InternalFactHandle) obj).getObject();
36: }
37: if (realObject instanceof ShadowProxy) {
38: realObject = ((ShadowProxy) realObject).getShadowedObject();
39: }
40: return rehash(System.identityHashCode(realObject));
41: }
42:
43: public int rehash(int h) {
44: h += ~(h << 9);
45: h ^= (h >>> 14);
46: h += (h << 4);
47: h ^= (h >>> 10);
48: return h;
49: }
50:
51: /**
52: * Special comparator that allows FactHandles to be keys, but always checks
53: * like for like.
54: */
55: public boolean equal(final Object o1, final Object o2) {
56: if (o1 instanceof FactHandle) {
57: return ((InternalFactHandle) o1).getObject() == ((InternalFactHandle) o2)
58: .getObject();
59: }
60: Object left = o1;
61: if (left instanceof ShadowProxy) {
62: left = ((ShadowProxy) left).getShadowedObject();
63: }
64: final InternalFactHandle handle = ((InternalFactHandle) o2);
65:
66: return left == ((handle.isShadowFact()) ? ((ShadowProxy) handle
67: .getObject()).getShadowedObject() : handle.getObject());
68: }
69:
70: public int compare(final Object o1, final Object o2) {
71: return ((Comparable) o1).compareTo(o2);
72: }
73:
74: public String toString() {
75: return "identity";
76: }
77: }
|