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 EqualityAssertMapComparator implements ObjectComparator {
24: /**
25: *
26: */
27: private static final long serialVersionUID = 400L;
28:
29: public EqualityAssertMapComparator() {
30: }
31:
32: public int hashCodeOf(final Object obj) {
33: if (obj instanceof FactHandle) {
34: return rehash(((InternalFactHandle) obj)
35: .getObjectHashCode());
36: }
37: return rehash(obj.hashCode());
38: }
39:
40: public int rehash(int h) {
41: h += ~(h << 9);
42: h ^= (h >>> 14);
43: h += (h << 4);
44: h ^= (h >>> 10);
45: return h;
46: }
47:
48: /**
49: * Special comparator that allows FactHandles to be keys, but always checks
50: * equals with the identity of the objects involved
51: */
52: public boolean equal(final Object o1, Object o2) {
53: if (o1 instanceof FactHandle) {
54: return o1 == o2;
55: }
56:
57: final InternalFactHandle handle = ((InternalFactHandle) o2);
58:
59: o2 = (handle.isShadowFact()) ? ((ShadowProxy) handle
60: .getObject()).getShadowedObject() : handle.getObject();
61:
62: return o1 == o2 || o1.equals(o2);
63: }
64:
65: public int compare(final Object o1, final Object o2) {
66: return ((Comparable) o1).compareTo(o2);
67: }
68:
69: public String toString() {
70: return "equality";
71: }
72: }
|