Example usage for java.lang Number getClass

List of usage examples for java.lang Number getClass

Introduction

In this page you can find the example usage for java.lang Number getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:de.unioninvestment.eai.portal.portlet.crud.scripting.domain.container.rest.ValueConverter.java

private Object convertNumberToNumber(Class<?> targetClass, Number number) {
    if (targetClass.isAssignableFrom(number.getClass())) {
        return number;
    } else if (targetClass == Integer.class) {
        return number.intValue();
    } else if (targetClass == Long.class) {
        return number.longValue();
    } else if (targetClass == Double.class) {
        return number.doubleValue();
    } else if (targetClass == BigDecimal.class) {
        return new BigDecimal(number.toString());
    }/*from  www  .  j a va2  s  . com*/
    throw new IllegalArgumentException("Cannot convert to " + targetClass.getSimpleName() + ": " + number
            + " of type " + number.getClass().getName());
}

From source file:jp.furplag.util.commons.NumberUtils.java

/**
 * {@code (type) (o % divisor)}.//from   www .ja  va 2 s. co  m
 *
 * @param o the object, number or string.
 * @param divisor value by which 'o' is to be divided.
 * @param type return type.
 * @return {@code (type) (o % divisor)}.
 */
public static <T extends Number> T remainder(final Object o, final Number divisor, Class<T> type) {
    if (type == null)
        return null;
    NumberObject nO = NumberObject.of(type);
    Number n = valueOf(o);
    if (n == null)
        return nO.valueOf(null);
    if (divisor == null)
        return nO.valueOf(n);
    if (ClassUtils.isPrimitiveWrapper(n.getClass()) && ClassUtils.isPrimitiveWrapper(divisor.getClass())) {
        return nO.valueOf(valueOf(n, double.class) % valueOf(divisor, double.class));
    }
    if (isInfiniteOrNaN(n) || isInfiniteOrNaN(divisor))
        return nO.valueOf(Double.NaN);

    return nO.valueOf(
            valueOf(n, BigDecimal.class).remainder(valueOf(divisor, BigDecimal.class), MathContext.UNLIMITED));
}

From source file:jp.furplag.util.commons.NumberUtils.java

/**
 * {@code (type) (o + augend)}.//from   w  ww  . ja  va  2s .co m
 *
 * @param o the object. represent the zero if not convertible to number.
 * @param augend value to be added to 'o'. null means zero.
 * @param type return type.
 * @return {@code (type) (o + augend)}.
 */
public static <T extends Number> T add(final Object o, final Number augend, final Class<T> type) {
    if (type == null)
        return null;
    Number n = valueOf(o);
    if (n == null && augend == null)
        return NumberObject.of(type).valueOf(null);
    if (n == null)
        return NumberObject.of(type == null ? augend.getClass() : type).valueOf(augend);
    if (augend == null)
        return NumberObject.of(type == null ? n.getClass() : type).valueOf(n);
    if ((isNaN(n) && isNaN(augend)) || (isInfinite(n) && isInfinite(augend))) {
        float f = valueOf(n, float.class) + valueOf(augend, float.class);

        return NumberObject.of(type == null ? n.getClass() : type).valueOf(f);
    }

    return NumberObject.of(type == null ? n.getClass() : type)
            .valueOf(valueOf(n, BigDecimal.class, true).add(valueOf(augend, BigDecimal.class, true)));
}

From source file:com.fengduo.bee.commons.core.lang.CollectionUtils.java

private static <K extends Number, V extends Object> Map<K, V> toNumberMap(Collection<V> values, String property,
        Number keyValue) {
    if (values == null) {
        return Collections.emptyMap();
    }//from  w w w .ja va  2s.  c  o  m
    Map<K, V> valueMap = new HashMap<K, V>(values.size());
    for (V value : values) {
        try {
            String keyValueStr = BeanUtils.getProperty(value, property);
            if (NumberUtils.isNumber(keyValueStr)) {
                try {
                    // K valueTypeInstance = keyType.newInstance();
                    Object key = MethodUtils.invokeExactMethod(keyValue, "valueOf", keyValueStr);
                    valueMap.put((K) key, value);
                } catch (Exception e) {
                    throw new IllegalArgumentException("Unsupport key Type" + keyValue.getClass().getName(),
                            e);
                }
            } else {
                throw new IllegalArgumentException(
                        "Expect" + keyValue.getClass().getName() + ",Value Actul is " + keyValueStr);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
    return valueMap;
}

From source file:com.examples.with.different.packagename.testcarver.NumberConverter.java

/**
 * Convert any Number object to the specified type for this
 * <i>Converter</i>./* w  w w.  j a  va2 s.c  om*/
 * <p>
 * This method handles conversion to the following types:
 * <ul>
 *     <li><code>java.lang.Byte</code></li>
 *     <li><code>java.lang.Short</code></li>
 *     <li><code>java.lang.Integer</code></li>
 *     <li><code>java.lang.Long</code></li>
 *     <li><code>java.lang.Float</code></li>
 *     <li><code>java.lang.Double</code></li>
 *     <li><code>java.math.BigDecimal</code></li>
 *     <li><code>java.math.BigInteger</code></li>
 * </ul>
 * @param sourceType The type being converted from
 * @param targetType The Number type to convert to
 * @param value The Number to convert.
 *
 * @return The converted value.
 */
private Number toNumber(Class sourceType, Class targetType, Number value) {

    // Correct Number type already
    if (targetType.equals(value.getClass())) {
        return value;
    }

    // Byte
    if (targetType.equals(Byte.class)) {
        long longValue = value.longValue();
        if (longValue > Byte.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        if (longValue < Byte.MIN_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too small " + toString(targetType));
        }
        return new Byte(value.byteValue());
    }

    // Short
    if (targetType.equals(Short.class)) {
        long longValue = value.longValue();
        if (longValue > Short.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        if (longValue < Short.MIN_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too small " + toString(targetType));
        }
        return new Short(value.shortValue());
    }

    // Integer
    if (targetType.equals(Integer.class)) {
        long longValue = value.longValue();
        if (longValue > Integer.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        if (longValue < Integer.MIN_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too small " + toString(targetType));
        }
        return new Integer(value.intValue());
    }

    // Long
    if (targetType.equals(Long.class)) {
        return new Long(value.longValue());
    }

    // Float
    if (targetType.equals(Float.class)) {
        if (value.doubleValue() > Float.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        return new Float(value.floatValue());
    }

    // Double
    if (targetType.equals(Double.class)) {
        return new Double(value.doubleValue());
    }

    // BigDecimal
    if (targetType.equals(BigDecimal.class)) {
        if (value instanceof Float || value instanceof Double) {
            return new BigDecimal(value.toString());
        } else if (value instanceof BigInteger) {
            return new BigDecimal((BigInteger) value);
        } else {
            return BigDecimal.valueOf(value.longValue());
        }
    }

    // BigInteger
    if (targetType.equals(BigInteger.class)) {
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).toBigInteger();
        } else {
            return BigInteger.valueOf(value.longValue());
        }
    }

    String msg = toString(getClass()) + " cannot handle conversion to '" + toString(targetType) + "'";
    throw new ConversionException(msg);

}

From source file:javadz.beanutils.converters.NumberConverter.java

/**
 * Convert any Number object to the specified type for this
 * <i>Converter</i>./*from ww  w  . j a  v  a  2s. c om*/
 * <p>
 * This method handles conversion to the following types:
 * <ul>
 *     <li><code>java.lang.Byte</code></li>
 *     <li><code>java.lang.Short</code></li>
 *     <li><code>java.lang.Integer</code></li>
 *     <li><code>java.lang.Long</code></li>
 *     <li><code>java.lang.Float</code></li>
 *     <li><code>java.lang.Double</code></li>
 *     <li><code>java.math.BigDecimal</code></li>
 *     <li><code>java.math.BigInteger</code></li>
 * </ul>
 * @param sourceType The type being converted from
 * @param targetType The Number type to convert to
 * @param value The Number to convert.
 *
 * @return The converted value.
 */
private Number toNumber(Class sourceType, Class targetType, Number value) {

    // Correct Number type already
    if (targetType.equals(value.getClass())) {
        return value;
    }

    // Byte
    if (targetType.equals(Byte.class)) {
        long longValue = value.longValue();
        if (longValue > Byte.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        if (longValue < Byte.MIN_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too small " + toString(targetType));
        }
        return new Byte(value.byteValue());
    }

    // Short
    if (targetType.equals(Short.class)) {
        long longValue = value.longValue();
        if (longValue > Short.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        if (longValue < Short.MIN_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too small " + toString(targetType));
        }
        return new Short(value.shortValue());
    }

    // Integer
    if (targetType.equals(Integer.class)) {
        long longValue = value.longValue();
        if (longValue > Integer.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        if (longValue < Integer.MIN_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too small " + toString(targetType));
        }
        return new Integer(value.intValue());
    }

    // Long
    if (targetType.equals(Long.class)) {
        return new Long(value.longValue());
    }

    // Float
    if (targetType.equals(Float.class)) {
        if (value.doubleValue() > Float.MAX_VALUE) {
            throw new ConversionException(
                    toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType));
        }
        return new Float(value.floatValue());
    }

    // Double
    if (targetType.equals(Double.class)) {
        return new Double(value.doubleValue());
    }

    // BigDecimal
    if (targetType.equals(BigDecimal.class)) {
        if (value instanceof Float || value instanceof Double) {
            return new BigDecimal(value.toString());
        } else if (value instanceof BigInteger) {
            return new BigDecimal((BigInteger) value);
        } else {
            return BigDecimal.valueOf(value.longValue());
        }
    }

    // BigInteger
    if (targetType.equals(BigInteger.class)) {
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).toBigInteger();
        } else {
            return BigInteger.valueOf(value.longValue());
        }
    }

    String msg = toString(getClass()) + " cannot handle conversion to '" + toString(targetType) + "'";
    if (log().isWarnEnabled()) {
        log().warn("    " + msg);
    }
    throw new ConversionException(msg);

}

From source file:com.arpnetworking.metrics.impl.ApacheHttpSinkTest.java

private static void assertSample(final List<ClientV2.MetricEntry> samples, final String name,
        final Number value, @Nullable final ClientV2.CompoundUnit compoundUnit) {

    final ClientV2.Quantity.Builder protobufSample = ClientV2.Quantity.newBuilder();
    if (value instanceof Double || value instanceof Float) {
        protobufSample.setDoubleValue(value.doubleValue());
    } else {/* w w  w  . j  a v  a  2  s.c  om*/
        protobufSample.setLongValue(value.longValue());
    }
    if (compoundUnit != null) {
        protobufSample.setUnit(compoundUnit);
    } else {
        protobufSample.setUnit(ClientV2.CompoundUnit.newBuilder().build());
    }

    Assert.assertTrue(
            String.format("Missing sample: name=%s, value=%s (%s)", name, value, value.getClass().getName()),
            samples.contains(ClientV2.MetricEntry.newBuilder().setName(name)
                    .addSamples(0, protobufSample.build()).build()));
}

From source file:com.prowidesoftware.swift.model.field.Field.java

/**
 * Gets a BigDecimal from a generic Number argument
 * @param number/*from   ww  w  .  ja  v a  2 s. c om*/
 * @return BigDecimal value of number parameter
 */
static public BigDecimal getAsBigDecimal(final Number number) {
    if (number instanceof BigDecimal) {
        return (BigDecimal) number;
    } else if (number instanceof Long) {
        return new BigDecimal(((Long) number).longValue());
    } else if (number instanceof Integer) {
        return new BigDecimal(((Integer) number).intValue());
    } else if (number instanceof Short) {
        return new BigDecimal(((Short) number).intValue());
    } else if (number instanceof Double) {
        return new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("class " + number.getClass().getName() + " is not supported");
    }
}

From source file:NumberRange.java

/**
 * <p>Tests whether the specified <code>number</code> occurs within
 * this range.</p>/*from w w w  . j  a v a  2 s  .  co m*/
 * 
 * <p><code>null</code> is handled and returns <code>false</code>.</p>
 *
 * @param number  the number to test, may be <code>null</code>
 * @return <code>true</code> if the specified number occurs within this range
 * @throws IllegalArgumentException if the number is of a different type to the range
 */
public boolean containsNumber(Number number) {
    if (number == null) {
        return false;
    }
    if (number.getClass() != min.getClass()) {
        throw new IllegalArgumentException("The number must be of the same type as the range numbers");
    }
    int compareMin = ((Comparable) min).compareTo(number);
    int compareMax = ((Comparable) max).compareTo(number);
    return compareMin <= 0 && compareMax >= 0;
}

From source file:com.effektif.workflow.impl.json.JsonStreamWriter.java

@Override
public void writeNumber(Number n) {
    try {//  w ww.  j av  a  2  s.com
        if (n instanceof Long) {
            jgen.writeNumber((Long) n);
        } else if (n instanceof Integer) {
            jgen.writeNumber((Integer) n);
        } else if (n instanceof Double) {
            jgen.writeNumber((Double) n);
        } else if (n instanceof Float) {
            jgen.writeNumber((Float) n);
        } else if (n instanceof Short) {
            jgen.writeNumber((Short) n);
        } else if (n instanceof BigDecimal) {
            jgen.writeNumber((BigDecimal) n);
        } else if (n instanceof BigInteger) {
            jgen.writeNumber((BigInteger) n);
        } else {
            throw new RuntimeException("Couldn't write number of type " + n.getClass());
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}