001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.comparators; 018 019import java.io.Serializable; 020import java.util.Comparator; 021 022/** 023 * A {@link Comparator Comparator} that compares {@link Comparable Comparable} 024 * objects. 025 * <p> 026 * This Comparator is useful, for example, for enforcing the natural order in 027 * custom implementations of {@link java.util.SortedSet SortedSet} and 028 * {@link java.util.SortedMap SortedMap}. 029 * <p> 030 * Note: In the 2.0 and 2.1 releases of Commons Collections, this class would 031 * throw a {@link ClassCastException} if either of the arguments to 032 * {@link #compare(Object, Object) compare} were <code>null</code>, not 033 * {@link Comparable Comparable}, or for which 034 * {@link Comparable#compareTo(Object) compareTo} gave inconsistent results. 035 * This is no longer the case. See {@link #compare(Object, Object) compare} for 036 * details. 037 * 038 * @param <E> the type of objects compared by this comparator 039 * 040 * @since 2.0 041 * @see java.util.Collections#reverseOrder() 042 */ 043public class ComparableComparator<E extends Comparable<? super E>> implements Comparator<E>, Serializable { 044 045 /** Serialization version. */ 046 private static final long serialVersionUID=-291439688585137865L; 047 048 /** The singleton instance. */ 049 @SuppressWarnings("rawtypes") 050 public static final ComparableComparator INSTANCE = new ComparableComparator(); 051 052 //----------------------------------------------------------------------- 053 /** 054 * Gets the singleton instance of a ComparableComparator. 055 * <p> 056 * Developers are encouraged to use the comparator returned from this method 057 * instead of constructing a new instance to reduce allocation and GC overhead 058 * when multiple comparable comparators may be used in the same VM. 059 * 060 * @param <E> the element type 061 * @return the singleton ComparableComparator 062 * @since 4.0 063 */ 064 @SuppressWarnings("unchecked") 065 public static <E extends Comparable<? super E>> ComparableComparator<E> comparableComparator() { 066 return INSTANCE; 067 } 068 069 //----------------------------------------------------------------------- 070 /** 071 * Constructor whose use should be avoided. 072 * <p> 073 * Please use the {@link #comparableComparator()} method whenever possible. 074 */ 075 public ComparableComparator() { 076 super(); 077 } 078 079 //----------------------------------------------------------------------- 080 /** 081 * Compare the two {@link Comparable Comparable} arguments. 082 * This method is equivalent to: 083 * <pre>((Comparable)obj1).compareTo(obj2)</pre> 084 * 085 * @param obj1 the first object to compare 086 * @param obj2 the second object to compare 087 * @return negative if obj1 is less, positive if greater, zero if equal 088 * @throws NullPointerException if <i>obj1</i> is <code>null</code>, 089 * or when <code>((Comparable)obj1).compareTo(obj2)</code> does 090 * @throws ClassCastException if <i>obj1</i> is not a <code>Comparable</code>, 091 * or when <code>((Comparable)obj1).compareTo(obj2)</code> does 092 */ 093 @Override 094 public int compare(final E obj1, final E obj2) { 095 return obj1.compareTo(obj2); 096 } 097 098 //----------------------------------------------------------------------- 099 /** 100 * Implement a hash code for this comparator that is consistent with 101 * {@link #equals(Object) equals}. 102 * 103 * @return a hash code for this comparator. 104 * @since 3.0 105 */ 106 @Override 107 public int hashCode() { 108 return "ComparableComparator".hashCode(); 109 } 110 111 /** 112 * Returns {@code true} iff <i>that</i> Object is is a {@link Comparator Comparator} 113 * whose ordering is known to be equivalent to mine. 114 * <p> 115 * This implementation returns {@code true} iff 116 * <code><i>object</i>.{@link Object#getClass() getClass()}</code> equals 117 * <code>this.getClass()</code>. Subclasses may want to override this behavior to remain 118 * consistent with the {@link Comparator#equals(Object)} contract. 119 * 120 * @param object the object to compare with 121 * @return {@code true} if equal 122 * @since 3.0 123 */ 124 @Override 125 public boolean equals(final Object object) { 126 return this == object || 127 null != object && object.getClass().equals(this.getClass()); 128 } 129 130}