Java Object Compare areEqualComparables(T object1, T object2)

Here you can find the source of areEqualComparables(T object1, T object2)

Description

are Equal Comparables

License

Apache License

Declaration

public static <T extends Comparable<T>> boolean areEqualComparables(T object1, T object2) 

Method Source Code

//package com.java2s;
/*/*from  www.ja va 2 s.  c o  m*/
   Copyright 2005 Strategic Gains, Inc.
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
  http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

public class Main {
    public static <T extends Comparable<T>> boolean areEqualComparables(T object1, T object2) {
        if (object1 == null) {
            return (object2 == null);
        } else if (object2 == null) {
            return false;
        }

        return (object1.compareTo(object2) == 0);
    }

    @SuppressWarnings("unchecked")
    public static int compareTo(Object object1, Object object2) {
        if (object1 == null)
            return (object2 == null) ? 0 : -1;
        else if (object2 == null)
            return (object1 == null) ? 0 : 1;
        else {
            if (areComparable(object1, object2)) {
                return ((Comparable) object1).compareTo(((Comparable) object2));
            } else {
                return (object1.toString().compareTo(object2.toString()));
            }
        }
    }

    public static boolean areComparable(Object o1, Object o2) {
        if (o1 == null || o2 == null) {
            return false;
        }

        if (isComparable(o1) && isComparable(o2)) {
            if (o1.getClass().isAssignableFrom(o2.getClass()) || o2.getClass().isAssignableFrom(o1.getClass())) {
                return true;
            } else {
                return false;
            }
        }

        return false;
    }

    public static boolean isComparable(Object object) {
        return object instanceof Comparable;
    }
}

Related

  1. areEqual(Object parameter, Object otherParameter)
  2. areEqual(Object value, Object otherValue)
  3. areEqual(Object value1, Object value2)
  4. areEqual(T obj1, T obj2)
  5. areEqual(T obj1, V obj2)
  6. areEqualEvenIfBothNull(Object first, Object second)
  7. areEquals(final Object o1, final Object o2)
  8. areEquals(Object a, Object b)
  9. areEquals(Object arg0, Object arg1)