Java Number Max Value max(final NUMBER_TYPE n1, final NUMBER_TYPE n2)

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

Description

max

License

Open Source License

Declaration

public static <NUMBER_TYPE extends Number> NUMBER_TYPE max(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 max(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   www  .ja v a2 s.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. max(final float a, final float b, final float c)
  2. max(final int a, final int b)
  3. max(final int a, final int b)
  4. max(final Iterable numbers)
  5. max(final Iterable values)
  6. max(final NUMBER_TYPE number1, final NUMBER_TYPE number2)
  7. max(final String string1, final String string2)
  8. Max(float a, float b)
  9. max(float a, float b)