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