View Javadoc

1   /*
2    * Copyright (c) 2003, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of Genjava-Core nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package com.generationjava.compare;
33  
34  import java.util.Comparator;
35  import java.util.HashMap;
36  import java.util.Map;
37  
38  import com.generationjava.collections.ClassMap;
39  
40  import org.apache.commons.collections.comparators.ComparableComparator;
41  
42  /***
43   * Attempts to compare any Object.
44   * Some standard types of objects can be dealt with immediately, 
45   * but others will need a Comparator being added with the static 
46   * registerComparator method.
47   * 
48   * The following are already handled:
49   *
50   * Any extension of Comparable. This includes all the Number's and 
51   * String, Character amongst others.
52   * java.net.URL is handled.
53   */
54  public class ObjectComparator implements Comparator {
55  
56      static private Map registry = new ClassMap( new HashMap() );
57  
58      static {
59          Comparator cc = new ComparableComparator();
60          registry.put(java.lang.Comparable.class, cc);
61  
62          // Do all these comparables to increase lookup speed
63          registry.put(java.lang.String.class, cc);
64          registry.put(java.lang.Number.class, cc);
65          registry.put(java.io.File.class, cc);
66          registry.put(java.lang.Character.class, cc);
67          registry.put(java.math.BigDecimal.class, cc);
68          registry.put(java.lang.Byte.class, cc);
69          registry.put(java.lang.Long.class, cc);
70          registry.put(java.lang.Short.class, cc);
71          registry.put(java.lang.Float.class, cc);
72          registry.put(java.lang.Double.class, cc);
73          registry.put(java.math.BigInteger.class, cc);
74  
75          registry.put(java.net.URL.class, new UrlComparator());
76      }
77  
78      /***
79       * Register a Comparator to be used to compare a particular Class.
80       */
81      static public void registerComparator(Class clss, Comparator c) {
82          registry.put(clss, c);
83      }
84  
85      /***
86       * Get the Comparator to be used for a particular Class.
87       * If no Comparator is available, then it will search the 
88       * inheritence tree for one.
89       */
90      static public Comparator getComparator(Class clss) {
91  
92          if(clss == null) {
93              return null;
94          }
95  
96          return (Comparator)registry.get(clss);
97      }
98  
99      public ObjectComparator() {
100     }
101 
102     public int compare(Object o1, Object o2) {
103         if(o1 == null) {
104             return -1;
105         } else
106         if(o2 == null) {
107             return 1;
108         }
109 
110         if(o1.getClass() != o2.getClass()) {
111             return -1;
112         }
113 
114         Comparator subCom = getComparator(o1.getClass());
115         if(subCom == null) {
116             return -1;
117         } else {
118             return subCom.compare(o1, o2);
119         }
120     }
121 
122 }