View Javadoc

1   package net.sf.jhunlang.jmorph.util;
2   
3   /***
4    * 
5    */
6   public class Comparables implements Comparable
7   {
8     static long id;
9   
10    protected Comparable a;
11    protected Comparable b;
12  
13    protected int hashCode;
14  
15    protected boolean identified;
16  
17    protected long lid;
18  
19    static synchronized long next()
20    {
21      return id++;
22    }
23  
24    /***
25     * <code>identified</code> tells if <code>a</code> and <code>b</code>
26     * together identify this Comparables. Identified Comparables can be used
27     * to differentiate otherwise equal Comparables instances by ordering them
28     * by their creation order.
29     */
30    public Comparables(Comparable a, Comparable b, boolean identified)
31    {
32      this.a = a;
33      this.b = b;
34      this.identified = identified;
35      hashCode = a.hashCode() * 31 + b.hashCode();
36      lid = next();
37    }
38    
39    public Comparables(Comparable a, long b, boolean identified)
40    {
41      this(a, new Long(b), identified);
42    }
43  
44    public Comparables(Comparable a, int b, boolean identified)
45    {
46      this(a, new Integer(b), identified);
47    }
48  
49    public Comparables(long a, long b, boolean identified)
50    {
51      this(new Long(a), new Long(b), identified);
52    }
53  
54    public Comparables(long a, int b, boolean identified)
55    {
56      this(new Long(a), new Integer(b), identified);
57    }
58  
59    public Comparable getA()
60    {
61      return a;
62    }
63  
64    public Comparable getB()
65    {
66      return b;
67    }
68  
69    /***
70     * Compare this object with <code>o</o>.
71     * Throws ClassCastException if o is not a Comparables.
72     * @param o the other object
73     * @return the relation of this object and <code>o</code> 
74     */
75    public int compareTo(Object o)
76    {
77      Comparables oc = (Comparables)o;
78  
79      int rel = a.compareTo(oc.getA());
80  
81      if (rel != 0)
82      {
83        return rel;
84      }
85      
86      rel = b.compareTo(oc.getB());
87  
88      if (!identified || rel != 0)
89      {
90        return rel;
91      }
92      // if identified and they are equal then return
93      // the relation of order of their creation
94      return lid < oc.lid ? -1 : (lid > oc.lid ? 1 : 0);
95    }
96  
97    public int hashCode()
98    {
99      return hashCode;
100   }
101 
102   public boolean equals(Object o)
103   {
104     return
105       o != null &&
106       (o instanceof Comparables) &&
107       hashCode == ((Comparables)o).hashCode &&
108       compareTo(o) == 0;
109   }
110 
111   // utility method for short class name
112   public String shortClassname()
113   {
114     return shorten(getClass().getName());
115   }
116 
117   // utility method for short class name
118   public  static String shorten(String s)
119   {
120     return s.substring(s.lastIndexOf('.') + 1);
121   }
122 
123   public String toString()
124   {
125     return a + "\t" + b;
126   }
127 }