View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.util;
21  
22  import java.text.CollationKey;
23  import java.text.Collator;
24  import java.util.Comparator;
25  
26  
27  public abstract class ComparatorBase implements Comparator {
28  
29    private Comparator comparator;
30  
31    private boolean reverse;
32  
33    protected ComparatorBase() {
34    }
35  
36    protected ComparatorBase(boolean reverse, Comparator comparator) {
37      this.comparator = comparator;
38      this.reverse = reverse;
39    }
40  
41    protected ComparatorBase(boolean reverse) {
42      this.reverse = reverse;
43    }
44  
45    protected ComparatorBase(Comparator comparator) {
46      this.comparator = comparator;
47    }
48  
49    protected int internalCompare(Object obj1, Object obj2) {
50  
51      if (obj1 == null || obj2 == null) {
52        if (obj1 == null && obj2 == null) {
53          return 0;
54        }
55        if (obj1 == null) {
56          return reverse ? 1 : -1;
57        } else {
58          return reverse ? -1 : 1;
59        }
60      }
61  
62      if (!obj1.getClass().isInstance(obj2)) {
63        throw new ClassCastException(obj1.getClass().getName() + " != "
64            + obj2.getClass().getName());
65      }
66  
67      int result;
68  
69  
70      if (comparator instanceof Collator) {
71        CollationKey collationKey1
72            = ((Collator) comparator).getCollationKey(obj1.toString());
73        CollationKey collationKey2
74            = ((Collator) comparator).getCollationKey(obj2.toString());
75        result = collationKey1.compareTo(collationKey2);
76  
77      } else if (comparator != null) {
78        result = comparator.compare(obj1, obj2);
79      } else {
80        if (obj1 instanceof String) {
81          result = ((String) obj1).compareToIgnoreCase((String) obj2);
82        } else if (obj1 instanceof Comparable) {
83          result = ((Comparable) obj1).compareTo(obj2);
84        } else {
85          result = obj1.toString().compareTo(obj2.toString());
86        }
87      }
88      return reverse ? -result : result;
89    }
90  
91    /*
92  
93    // TODO use this??
94    public boolean equals(Object o) {
95      if (this == o) {
96        return true;
97      }
98      if (o == null || getClass() != o.getClass()) {
99        return false;
100     }
101 
102     final ComparatorBase that = (ComparatorBase) o;
103 
104     return !(comparator != null ? !comparator.equals(that.comparator) : that.comparator != null);
105 
106   }
107 
108   public int hashCode() {
109     return (comparator != null ? comparator.hashCode() : 0);
110   } */
111 
112   public boolean equals(Object o) {
113     if (o == null) {
114       return false;
115     }
116     return ((ComparatorBase) o).getComparator().equals(comparator);
117   }
118 
119   public int hashCode() {
120     return (comparator != null ? comparator.hashCode() : 0);
121   }
122 
123   public Comparator getComparator() {
124     return comparator;
125   }
126 }