001    package org.apache.myfaces.tobago.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import java.text.CollationKey;
021    import java.text.Collator;
022    import java.util.Comparator;
023    
024    
025    public abstract class ComparatorBase implements Comparator {
026    
027    
028      private Comparator comparator;
029    
030      private boolean reverse;
031    
032    
033      protected ComparatorBase() {
034      }
035    
036      protected ComparatorBase(boolean reverse, Comparator comparator) {
037        this.comparator = comparator;
038        this.reverse = reverse;
039      }
040    
041      protected ComparatorBase(boolean reverse) {
042        this.reverse = reverse;
043      }
044    
045      protected ComparatorBase(Comparator comparator) {
046        this.comparator = comparator;
047      }
048    
049      protected int internalCompare(Object obj1, Object obj2) {
050    
051        if (obj1 == null || obj2 == null) {
052          if (obj1 == null && obj2 == null) {
053            return 0;
054          }
055          if (obj1 == null) {
056            return reverse ? 1 : -1;
057          } else {
058            return reverse ? -1 : 1;
059          }
060        }
061    
062        if (!obj1.getClass().isInstance(obj2)) {
063          throw new ClassCastException(obj1.getClass().getName() + " != "
064              + obj2.getClass().getName());
065        }
066    
067        int result;
068    
069    
070        if (comparator instanceof Collator) {
071          CollationKey collationKey1
072              = ((Collator) comparator).getCollationKey(obj1.toString());
073          CollationKey collationKey2
074              = ((Collator) comparator).getCollationKey(obj2.toString());
075          result = collationKey1.compareTo(collationKey2);
076    
077        } else if (comparator != null) {
078          result = comparator.compare(obj1, obj2);
079        } else {
080          if (obj1 instanceof String) {
081            result = ((String) obj1).compareToIgnoreCase((String) obj2);
082          } else if (obj1 instanceof Comparable) {
083            result = ((Comparable) obj1).compareTo(obj2);
084          } else {
085            result = obj1.toString().compareTo(obj2.toString());
086          }
087        }
088        return reverse ? -result : result;
089      }
090    
091      /*
092    
093      // TODO use this??
094      public boolean equals(Object o) {
095        if (this == o) {
096          return true;
097        }
098        if (o == null || getClass() != o.getClass()) {
099          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    }