Java Comparable compare(final Comparable left, final Comparable right)

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

Description

Compares to comparable objects -- defending against null.

License

Open Source License

Parameter

Parameter Description
left The left object to compare; may be <code>null</code>.
right The right object to compare; may be <code>null</code>.

Return

The result of the comparison. null is considered to be the least possible value.

Declaration

public static final int compare(final Comparable left,
        final Comparable right) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2009 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 www  .jav  a  2  s. co m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.List;

public class Main {
    /**
     * Compares two boolean values. <code>false</code> is considered to be
     * "less than" <code>true</code>.
     * 
     * @param left
     *            The left value to compare
     * @param right
     *            The right value to compare
     * @return <code>-1</code> if the left is <code>false</code> and the
     *         right is <code>true</code>. <code>1</code> if the opposite
     *         is true. If they are equal, then it returns <code>0</code>.
     */
    public static final int compare(final boolean left, final boolean right) {
        return left == false ? (right == true ? -1 : 0) : 1;
    }

    /**
     * Compares two integer values.
     * 
     * @param left
     *            The left value to compare
     * @param right
     *            The right value to compare
     * @return <code>left - right</code>
     */
    public static final int compare(final int left, final int right) {
        return left - right;
    }

    /**
     * Compares to comparable objects -- defending against <code>null</code>.
     * 
     * @param left
     *            The left object to compare; may be <code>null</code>.
     * @param right
     *            The right object to compare; may be <code>null</code>.
     * @return The result of the comparison. <code>null</code> is considered
     *         to be the least possible value.
     */
    public static final int compare(final Comparable left,
            final 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);
        }
    }

    /**
     * Compares two arrays of comparable objects -- accounting for
     * <code>null</code>.
     * 
     * @param left
     *            The left array to be compared; may be <code>null</code>.
     * @param right
     *            The right array to be compared; may be <code>null</code>.
     * @return The result of the comparison. <code>null</code> is considered
     *         to be the least possible value. A shorter array is considered
     *         less than a longer array.
     */
    public static final int compare(final Comparable[] left,
            final 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;
        }
    }

    /**
     * Compares two lists -- account for <code>null</code>. The lists must
     * contain comparable objects.
     * 
     * @param left
     *            The left list to compare; may be <code>null</code>. This
     *            list must only contain instances of <code>Comparable</code>.
     * @param right
     *            The right list to compare; may be <code>null</code>. This
     *            list must only contain instances of <code>Comparable</code>.
     * @return The result of the comparison. <code>null</code> is considered
     *         to be the least possible value. A shorter list is considered less
     *         than a longer list.
     */
    public static final int compare(final List left, final 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 left, Comparable right)
  2. compare(Comparable obj1, T obj2)