Java Number Compare compareTwoObject(Object o1, Object o2)

Here you can find the source of compareTwoObject(Object o1, Object o2)

Description

Compare two object

License

Open Source License

Parameter

Parameter Description
o1 object 1
o2 object 2

Declaration

public static int compareTwoObject(Object o1, Object o2) 

Method Source Code

//package com.java2s;
/*/*from  www.  j a  v a 2s . c om*/
 * File: $RCSfile$
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */

import java.math.BigDecimal;

public class Main {
    /**
     * Compare two object
     *
     * @param o1 object 1
     * @param o2 object 2
     * @return {@link Comparable#compareTo(Object)}
     */
    public static int compareTwoObject(Object o1, Object o2) {
        Class objClass;
        if (o1 == null && o2 == null) {
            return 0;
        } else if (o1 == null) {
            return -1;
        } else if (o2 == null) {
            return 1;
        }
        objClass = o1.getClass();

        if (o1 instanceof Comparable && o2 instanceof Comparable) {
            int result = ((Comparable) o1).compareTo(o2);
            return normalizeResult(result);
        } else if (objClass.getSuperclass() == Number.class) {
            Number n1 = (Number) o1;
            double d1 = n1.doubleValue();
            Number n2 = (Number) o2;
            double d2 = n2.doubleValue();
            if (d1 < d2) {
                return -1;
            } else if (d1 > d2) {
                return 1;
            } else {
                return 0;
            }
        } else if (objClass == Boolean.class) {
            Boolean bool1 = (Boolean) o1;
            boolean b1 = bool1.booleanValue();
            Boolean bool2 = (Boolean) o2;
            boolean b2 = bool2.booleanValue();
            if (b1 == b2) {
                return 0;
            } else if (b1) {
                return 1;
            } else {
                return -1;
            }
        } else if (objClass == BigDecimal.class) {
            BigDecimal n1 = (BigDecimal) o1;
            double d1 = n1.doubleValue();
            BigDecimal n2 = (BigDecimal) o2;
            double d2 = n2.doubleValue();
            if (d1 < d2) {
                return -1;
            } else if (d1 > d2) {
                return 1;
            } else {
                return 0;
            }
        } else {
            String s1 = o1.toString();
            String s2 = o2.toString();
            int result = s1.compareTo(s2);
            return normalizeResult(result);
        }
    }

    private static int normalizeResult(int compareResult) {
        if (compareResult < 0) {
            return -1;
        } else if (compareResult > 0) {
            return 1;
        } else {
            return 0;
        }
    }
}

Related

  1. compareDoublesWithTolerance(double aa, double bb, double tolerance)
  2. compareStringsAsNumbers(String s1, String s2, String op)
  3. compareTo(double v1, double v2)
  4. compareTo(Object val1, Object val2)
  5. compareTo(Object value1, Object value2)
  6. compareValue(Object left, Object right)
  7. compareValues(String value, String type, Object obj)
  8. getCrosstabRowColumnValue(String groupIdentifier, Map crosstabInfoMap, Object executorUnitId, String financesBankId, String companyNumber, String accountNumber)
  9. getDifficulty(long compactTarget)