Java Number Max Value max(Object o1, Object o2)

Here you can find the source of max(Object o1, Object o2)

Description

max

License

Apache License

Declaration

public static Number max(Object o1, Object o2) 

Method Source Code

//package com.java2s;
/*/*  www.j a  v  a2  s .  c  o m*/
 * Copyright 2011 Future Systems
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static Number max(Object o1, Object o2) {
        Number n1 = getValue(o1);
        Number n2 = getValue(o2);

        if (n1 == null)
            return n2;
        if (n2 == null)
            return n1;

        if (n1.doubleValue() > n2.doubleValue())
            return n1;
        else
            return n2;
    }

    public static Number getValue(Object obj) {
        return getValue(obj, null);
    }

    public static Number getValue(Object obj, Number defaultValue) {
        if (obj == null)
            return defaultValue;
        try {
            return Long.parseLong(obj.toString());
        } catch (NumberFormatException e) {
        }
        try {
            return Double.parseDouble(obj.toString());
        } catch (NumberFormatException e) {
        }
        if (obj.toString().startsWith("0x")) {
            try {
                return Long.parseLong(obj.toString().substring(2), 16);
            } catch (NumberFormatException e) {
            }
        }
        return defaultValue;
    }
}

Related

  1. max(long a, long b)
  2. max(Number a, Number b)
  3. max(Number n0, Number n1)
  4. max(Number num1, Number num2)
  5. Max(Object in)
  6. max(String a, String b)
  7. max(String content, int max, String dotDotDot)
  8. max(String left, String right)
  9. max(String name)