Java Number Min Value min(Object o1, Object o2)

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

Description

min

License

Apache License

Declaration

public static Number min(Object o1, Object o2) 

Method Source Code

//package com.java2s;
/*/*w w  w.  j  a v a2  s. com*/
 * 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 min(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. min(long x, long y)
  2. min(Number a, Number b)
  3. min(Number n0, Number n1)
  4. min(Number num1, Number num2)
  5. Min(Object in)
  6. min(String a, String b)
  7. min(String a, String b)
  8. min(String in, int len)
  9. min(String name)