Java examples for java.lang:Number
Checks if the given number value lies between an lower and an upper bound value.
//package com.java2s; import java.math.BigDecimal; import java.math.BigInteger; public class Main { public static void main(String[] argv) throws Exception { Number value = new Integer(2); Number lowerBound = new Integer(2); Number upperBound = new Integer(2); System.out.println(isBetween(value, lowerBound, upperBound)); }//from www. j a va 2s. c om /** * Checks if the given number value lies between an lower and an upper bound * value. * @param value The value to check * @param lowerBound The lowerbound value. Value must be > lowerBound. If * lowerbound is null, negative infinity is assumed. * @param upperBound The upperbound value. Value must be < upperBound. If * upperbound is null, infinity is assumed. * @return True is value is not null and its integer value < lowerBound and > * upperBound. */ public static boolean isBetween(Number value, Number lowerBound, Number upperBound) { boolean isBetween = false; if (value != null) { if (BigDecimal.class.isAssignableFrom(value.getClass())) { isBetween = ((lowerBound == null) || (asBigDecimal( lowerBound).compareTo((BigDecimal) value) < 0)) && ((upperBound == null) || (asBigDecimal( upperBound).compareTo((BigDecimal) value) > 0)); } else if (BigInteger.class.isAssignableFrom(value.getClass())) { isBetween = ((lowerBound == null) || (asBigInteger( lowerBound).compareTo((BigInteger) value) < 0)) && ((upperBound == null) || (asBigInteger( upperBound).compareTo((BigInteger) value) > 0)); } else { isBetween = ((lowerBound == null) || (lowerBound .doubleValue() < value.doubleValue())) && ((upperBound == null) || (value.doubleValue() < upperBound .doubleValue())); } } return isBetween; } /** * Checks if the Class of the given object is assignable from a reference * Class. * @param value The object to check. * @param clazz The reference Class. * @return True if value, it's Class and clazz are not null and value is * assignable from clazz; false otherwise. */ public static boolean isAssignableFrom(Object value, Class clazz) { return isAssignableFrom((value != null) ? value.getClass() : (Class) null, clazz); } /** * Checks if the given Class is assignable from a reference Class. * @param value The Class to check. * @param clazz The reference Class. * @return True if value and clazz are not null and value is assignable from * clazz; false otherwise. */ @SuppressWarnings("unchecked") public static boolean isAssignableFrom(Class value, Class clazz) { return (clazz != null) && (value != null) && clazz.isAssignableFrom(value); // unchecked } private static BigDecimal asBigDecimal(Number value) { BigDecimal decimalValue = null; if (value != null) { if (BigDecimal.class.isAssignableFrom(value.getClass())) { decimalValue = (BigDecimal) value; } else if (BigInteger.class.isAssignableFrom(value.getClass())) { decimalValue = new BigDecimal((BigInteger) value); } else { decimalValue = new BigDecimal(value.doubleValue()); } } return decimalValue; } private static BigInteger asBigInteger(Number value) { BigInteger integerValue = null; if (value != null) { if (BigInteger.class.isAssignableFrom(value.getClass())) { integerValue = (BigInteger) value; } else if (BigDecimal.class.isAssignableFrom(value.getClass())) { integerValue = ((BigDecimal) value).toBigInteger(); } else { integerValue = BigInteger.valueOf(value.longValue()); } } return integerValue; } }