01: /* IdentityComparator.java
02:
03: {{IS_NOTE
04: Purpose:
05: The comparator uses == and System.identifyHashCode to do
06: the comparison.
07: Description:
08:
09: History:
10: Fri Sep 13 10:14:11 2002, Created by tomyeh
11: }}IS_NOTE
12:
13: Copyright (C) 2002 Potix Corporation. All Rights Reserved.
14:
15: {{IS_RIGHT
16: This program is distributed under GPL Version 2.0 in the hope that
17: it will be useful, but WITHOUT ANY WARRANTY.
18: }}IS_RIGHT
19: */
20: package org.zkoss.util;
21:
22: import java.util.Comparator;
23:
24: /**
25: * The comparator uses == and System.identifyHashCode to compare two objects.
26: * It assumes if o1 != o2, then c1 != c2 where c1 = System.identityHashCode(o1).
27: *
28: * <p>This is useful if dynamic proxy is used with TreeSet or TreeMap
29: * (so equals is expensive).
30: * Reason: the speed of identifyHashCode is much faster than dynamic proxy
31: * (150:1).
32: *
33: * <p>However, if possible, java.util.IdentityHashMap and {@link IdentityHashSet}
34: * are preferred.
35: *
36: * @author tomyeh
37: * @see IdentityHashSet
38: */
39: public class IdentityComparator implements Comparator {
40: public IdentityComparator() {
41: }
42:
43: //-- Comparator --//
44: public int compare(Object o1, Object o2) {
45: if (o1 == o2)
46: return 0;
47:
48: int c1 = System.identityHashCode(o1);
49: int c2 = System.identityHashCode(o2);
50: assert c1 != c2; //if o1 != o2, c1 != c2
51: return c1 > c2 ? 1 : -1;
52: }
53: }
|