Java Comparable compare(Comparable left, Comparable right)

Here you can find the source of compare(Comparable left, Comparable right)

Description

compare

License

Open Source License

Declaration

public static int compare(Comparable left, Comparable right) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2003, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w  w w . ja va  2  s  .c om
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.List;

public class Main {
    public static int compare(boolean left, boolean right) {
        return left == false ? (right == true ? -1 : 0) : 1;
    }

    public static int compare(Comparable left, Comparable right) {
        if (left == null && right == null)
            return 0;
        else if (left == null)
            return -1;
        else if (right == null)
            return 1;
        else
            return left.compareTo(right);
    }

    public static int compare(Comparable[] left, Comparable[] right) {
        if (left == null && right == null)
            return 0;
        else if (left == null)
            return -1;
        else if (right == null)
            return 1;
        else {
            int l = left.length;
            int r = right.length;

            if (l != r)
                return l - r;
            for (int i = 0; i < l; i++) {
                int compareTo = compare(left[i], right[i]);

                if (compareTo != 0)
                    return compareTo;
            }
            return 0;
        }
    }

    public static int compare(int left, int right) {
        return left - right;
    }

    public static int compare(List left, List right) {
        if (left == null && right == null)
            return 0;
        else if (left == null)
            return -1;
        else if (right == null)
            return 1;
        else {
            int l = left.size();
            int r = right.size();

            if (l != r)
                return l - r;
            for (int i = 0; i < l; i++) {
                int compareTo = compare((Comparable) left.get(i),
                        (Comparable) right.get(i));

                if (compareTo != 0)
                    return compareTo;
            }
            return 0;
        }
    }
}

Related

  1. compare(Comparable obj1, T obj2)
  2. compare(final Comparable left, final Comparable right)