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 protected ComparatorBase(boolean reverse) { 041 this.reverse = reverse; 042 } 043 044 protected ComparatorBase(Comparator comparator) { 045 this.comparator = comparator; 046 } 047 048 protected int internalCompare(Object obj1, Object obj2) { 049 050 if (obj1 == null || obj2 == null) { 051 if (obj1 == null && obj2 == null) { 052 return 0; 053 } 054 if (obj1 == null) { 055 return reverse ? 1 : -1; 056 } else { 057 return reverse ? -1 : 1; 058 } 059 } 060 061 if (!obj1.getClass().isInstance(obj2)) { 062 throw new ClassCastException(obj1.getClass().getName() + " != " 063 + obj2.getClass().getName()); 064 } 065 066 int result; 067 068 069 if (comparator instanceof Collator) { 070 CollationKey collationKey1 071 = ((Collator) comparator).getCollationKey(obj1.toString()); 072 CollationKey collationKey2 073 = ((Collator) comparator).getCollationKey(obj2.toString()); 074 result = collationKey1.compareTo(collationKey2); 075 076 } else if (comparator != null) { 077 result = comparator.compare(obj1, obj2); 078 } else { 079 if (obj1 instanceof String) { 080 result = ((String) obj1).compareToIgnoreCase((String) obj2); 081 } else if (obj1 instanceof Comparable) { 082 result = ((Comparable) obj1).compareTo(obj2); 083 } else { 084 result = obj1.toString().compareTo(obj2.toString()); 085 } 086 } 087 return reverse ? -result : result; 088 } 089 090 /* 091 092 // TODO use this?? 093 public boolean equals(Object o) { 094 if (this == o) { 095 return true; 096 } 097 if (o == null || getClass() != o.getClass()) { 098 return false; 099 } 100 101 final ComparatorBase that = (ComparatorBase) o; 102 103 return !(comparator != null ? !comparator.equals(that.comparator) : that.comparator != null); 104 105 } 106 107 public int hashCode() { 108 return (comparator != null ? comparator.hashCode() : 0); 109 } */ 110 111 public boolean equals(Object o) { 112 if (o == null) { 113 return false; 114 } 115 return ((ComparatorBase) o).getComparator().equals(comparator); 116 } 117 118 public int hashCode() { 119 return (comparator != null ? comparator.hashCode() : 0); 120 } 121 122 public Comparator getComparator() { 123 return comparator; 124 } 125 }