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(final boolean reverse, final Comparator comparator) {
37      this.comparator = comparator;
38      this.reverse = reverse;
39    }
40  
41    protected ComparatorBase(final boolean reverse) {
42      this.reverse = reverse;
43    }
44  
45    protected ComparatorBase(final Comparator comparator) {
46      this.comparator = comparator;
47    }
48  
49    protected int internalCompare(final Object obj1, final Object obj2) {
50  
51      final int result;
52      if (obj1 == null || obj2 == null) {
53        if (obj1 == null && obj2 == null) {
54          result = 0;
55        } else {
56          if (obj1 == null) {
57            result = 1;
58          } else {
59            result = -1;
60          }
61        }
62      } else if (!obj1.getClass().isInstance(obj2)) {
63        throw new ClassCastException(obj1.getClass().getName() + " != " + obj2.getClass().getName());
64      } else {
65        if (comparator instanceof Collator) {
66          final CollationKey collationKey1
67              = ((Collator) comparator).getCollationKey(obj1.toString());
68          final CollationKey collationKey2
69              = ((Collator) comparator).getCollationKey(obj2.toString());
70          result = collationKey1.compareTo(collationKey2);
71        } else if (comparator != null) {
72          result = comparator.compare(obj1, obj2);
73        } else {
74          if (obj1 instanceof String) {
75            result = ((String) obj1).compareToIgnoreCase((String) obj2);
76          } else if (obj1 instanceof Comparable) {
77            result = ((Comparable) obj1).compareTo(obj2);
78          } else {
79            result = obj1.toString().compareTo(obj2.toString());
80          }
81        }
82      }
83      return reverse ? -result : result;
84    }
85  
86    public boolean equals(Object o) {
87      if (this == o) {
88        return true;
89      }
90      if (o == null || getClass() != o.getClass()) {
91        return false;
92      }
93  
94      final ComparatorBase that = (ComparatorBase) o;
95  
96      return comparator != null ? comparator.equals(that.comparator) : that.comparator == null;
97  
98    }
99  
100   public int hashCode() {
101     return comparator != null ? comparator.hashCode() : 0;
102   }
103 
104   public Comparator getComparator() {
105     return comparator;
106   }
107 }