Java Number Max Value max(T c1, T c2)

Here you can find the source of max(T c1, T 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 greater 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 <T extends Comparable<? super T>> T max(T c1, T c2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 MadRobot./* ww w.ja v a2  s. c  om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/

public class Main {
    /**
     * 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 greater 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 <T extends Comparable<? super T>> T max(T c1, T c2) {
        if (c1 != null && c2 != null) {
            return c1.compareTo(c2) >= 0 ? c1 : c2;
        } else {
            return c1 != null ? c1 : c2;
        }
    }
}

Related

  1. max(String left, String right)
  2. max(String name)
  3. max(String s)
  4. max(T a, T b)
  5. max(T a, T b)
  6. max(T c1, T c2)
  7. max(T v1, T v2)
  8. max(T v1, T v2, int nullSupport)
  9. max2(double a, double b)