Java Number Min Value min(Comparable c1, Comparable c2)

Here you can find the source of min(Comparable c1, Comparable c2)

Description

Null safe comparison of Comparables.

License

Open Source License

Parameter

Parameter Description
c1 the first comparable, may be null
c2 the second comparable, may be null

Return

  • If both objects are non-null and unequal, the lesser object.
  • If both objects are non-null and equal, c1.
  • If one of the comparables is null, the non-null object.
  • If both the comparables are null, null is returned.

Declaration

public static Object min(Comparable c1, Comparable c2) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*  w w w .j  a v  a 2s .  c  o m*/
     * Null safe comparison of Comparables.
     *
     * @param c1 the first comparable, may be null
     * @param c2 the second comparable, may be null
     * @return <ul>
     *         <li>If both objects are non-null and unequal, the lesser object.
     *         <li>If both objects are non-null and equal, c1.
     *         <li>If one of the comparables is null, the non-null object.
     *         <li>If both the comparables are null, null is returned.
     *         </ul>
     */
    public static Object min(Comparable c1, Comparable c2) {
        if (c1 != null && c2 != null) {
            return c1.compareTo(c2) < 1 ? c1 : c2;
        } else {
            return c1 != null ? c1 : c2;
        }
    }
}

Related

  1. findMin(double newNumber, double currentMin)
  2. findMin(int a, int b, int c, int d)
  3. findMinAngularDistance(double angle1, double angle2)
  4. min( final double p_first, final double p_second, final double p_third )
  5. min(Comparable c1, Comparable c2)
  6. min(Double a, Double b)
  7. min(double a, double b)
  8. min(double a, double b)