Java Number Min Value min(final NUMBER_TYPE n1, final NUMBER_TYPE n2)

Here you can find the source of min(final NUMBER_TYPE n1, final NUMBER_TYPE n2)

Description

min

License

Open Source License

Declaration

public static <NUMBER_TYPE extends Number> NUMBER_TYPE min(final NUMBER_TYPE n1, final NUMBER_TYPE n2) 

Method Source Code

//package com.java2s;

public class Main {
    public static <NUMBER_TYPE extends Number> NUMBER_TYPE min(final NUMBER_TYPE n1, final NUMBER_TYPE n2) {
        if (n1 == null && n2 == null) {
            return null;
        } else if (n1 == null) {// only one of them is null
            return n2;
        } else if (n2 == null) {// only one of them is null
            return n1;
        }//from w w w .  j av a2s .com
        final int compared = compareTo(n1, n2);
        if (compared < 0) {
            return n1;
        } else if (compared > 0) {
            return n2;
        } else {
            return n1;
        }
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <NUMBER_TYPE extends Number> int compareTo(final NUMBER_TYPE n1, final NUMBER_TYPE n2) {
        if (n1 == null && n2 == null) {
            return 0;
        } else if (n1 == null) {// only one of them is null
            return -1;
        } else if (n2 == null) {// only one of them is null
            return 1;
        } else {
            if (!(n1 instanceof Comparable<?>) || !(n2 instanceof Comparable<?>)) {
                throw new IllegalArgumentException("The given numbers must be comparable");
            }
            return ((Comparable) n1).compareTo(n2);
        }
    }
}

Related

  1. min(final float a, final float b)
  2. min(final float a, final float b, final float c)
  3. min(final int a, final int b)
  4. min(final int a, final int b)
  5. min(final Iterable numbers)
  6. min(final NUMBER_TYPE number1, final NUMBER_TYPE number2)
  7. min(final String string1, final String string2)
  8. min(float a, float b)
  9. min(float a, float b)