1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.functor.core.comparator; 18 19 import java.util.Comparator; 20 21 /** 22 * A {@link Comparator Comparator} that compares {@link Comparable Comparable} 23 * objects. 24 * <p> 25 * This class was created based on commons-collection's ComparableComparator. 26 * 27 * @param <E> the comparable type 28 * @version $Revision: 1537901 $ $Date: 2013-11-01 12:30:19 +0100 (Fr, 01 Nov 2013) $ 29 */ 30 final class ComparableComparator<E extends Comparable<? super E>> implements Comparator<E> { 31 32 /** Singleton. */ 33 @SuppressWarnings("rawtypes") 34 public static final ComparableComparator<?> INSTANCE = new ComparableComparator(); 35 36 /** 37 * Create a new ComparableComparator. 38 */ 39 public ComparableComparator() { 40 super(); 41 } 42 43 /** 44 * {@inheritDoc} 45 */ 46 public int compare(E o1, E o2) { 47 return o1.compareTo(o2); 48 } 49 50 /** 51 * {@inheritDoc} 52 */ 53 @Override 54 public boolean equals(Object obj) { 55 return (obj instanceof ComparableComparator); 56 } 57 58 /** 59 * {@inheritDoc} 60 */ 61 @Override 62 public int hashCode() { 63 return toString().hashCode(); 64 } 65 66 /** 67 * {@inheritDoc} 68 */ 69 @Override 70 public String toString() { 71 return "ComparableComparator"; 72 } 73 74 /** 75 * Get a ComparableComparator instance. 76 * @param <E> the comparable type 77 * @return ComparableComparator 78 */ 79 @SuppressWarnings("unchecked") 80 public static <E extends Comparable<? super E>> ComparableComparator<E> instance() { 81 return (ComparableComparator<E>) INSTANCE; 82 } 83 84 }