Example usage for java.lang Number longValue

List of usage examples for java.lang Number longValue

Introduction

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

Prototype

public abstract long longValue();

Source Link

Document

Returns the value of the specified number as a long .

Usage

From source file:de.cebitec.readXplorer.util.GeneralUtils.java

/**
 * Converts a given number into a number of the given classType. If this is
 * not possible, it throws a ClassCastException
 * @param <T> one of the classes derived from Number
 * @param classType the type to convert the number into
 * @param number the number to convert/* www  .ja v  a  2s .c  o  m*/
 * @return The converted number
 */
public static <T extends Number> T convertNumber(Class<T> classType, Number number) throws ClassCastException {
    T convertedValue = null;
    if (classType.equals(Integer.class)) {
        convertedValue = classType.cast(number.intValue());
    } else if (classType.equals(Double.class)) {
        convertedValue = classType.cast(number.doubleValue());
    } else if (classType.equals(Long.class)) {
        convertedValue = classType.cast(number.longValue());
    } else if (classType.equals(Float.class)) {
        convertedValue = classType.cast(number.floatValue());
    } else if (classType.equals(Short.class)) {
        convertedValue = classType.cast(number.shortValue());
    } else if (classType.equals(Byte.class)) {
        convertedValue = classType.cast(number.byteValue());
    }

    if (convertedValue == null) {
        throw new ClassCastException("Cannot cast the given number into the given format.");
    }

    return convertedValue;
}

From source file:NumberUtils.java

/**
 * Converts the given number to a <code>java.math.BigDecimal</code>.
 *
 * @param number//w ww.  ja  va2 s.c o m
 * @return java.math.BigDecimal
 * @throws IllegalArgumentException The given number is 'not a number' or infinite.
 */
public static BigDecimal toBigDecimal(Number number) throws IllegalArgumentException {
    if (number == null || number instanceof BigDecimal)
        return (BigDecimal) number;
    if (number instanceof BigInteger)
        return new BigDecimal((BigInteger) number);
    if (isDoubleCompatible(number)) {
        if (isNaN(number) || isInfinite(number))
            throw new IllegalArgumentException("Argument must not be NaN or infinite.");
        return new BigDecimal(number.toString());
    }
    if (isLongCompatible(number))
        return BigDecimal.valueOf(number.longValue());
    // => unknown Number type
    return new BigDecimal(String.valueOf(number.doubleValue()));
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the value of dividing the first number by the second.  If the
 * second argument is {@code 0} and function protection is enabled, this
 * function returns {@code 1} regardless of the first argument's value.
 * /*from   w  w w  .ja  v  a 2 s  . c o m*/
 * @param a the first number
 * @param b the second number
 * @return the value of dividing the first number by the second
 */
public static Number div(Number a, Number b) {
    if (isFloatingPoint(a) || isFloatingPoint(b)) {
        if ((Math.abs(b.doubleValue()) < Settings.EPS) && Settings.isProtectedFunctions()) {
            return 1.0;
        } else {
            return a.doubleValue() / b.doubleValue();
        }
    } else {
        if ((b.longValue() == 0) && Settings.isProtectedFunctions()) {
            return 1L;
        } else {
            return a.longValue() / b.longValue();
        }
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the remainder from dividing the first number by the second.  If
 * the second argument is {@code 0} and function protection is enabled, 
 * this function returns {@code 0} regardless of the first argument's
 * value.//from w w  w.j a  va 2  s .c om
 * 
 * @param a the first number
 * @param b the second number
 * @return the remainder from dividing the first number by the second
 */
public static Number mod(Number a, Number b) {
    if (isFloatingPoint(a) || isFloatingPoint(b)) {
        if ((Math.abs(b.doubleValue()) < Settings.EPS) && Settings.isProtectedFunctions()) {
            return 0.0;
        } else {
            return a.doubleValue() % b.doubleValue();
        }
    } else {
        if ((b.longValue() == 0) && Settings.isProtectedFunctions()) {
            return 0L;
        } else {
            return a.longValue() % b.longValue();
        }
    }
}

From source file:eu.geopaparazzi.library.util.Utilities.java

/**
 * Tries to adapt a value to the supplied type.
 *
 * @param value   the value to adapt.//from w  w w  .j  a  va2 s .  c om
 * @param adaptee the class to adapt to.
 * @return the adapted object or <code>null</code>, if it fails.
 */
public static <T> T adapt(Object value, Class<T> adaptee) {
    if (value instanceof Number) {
        Number num = (Number) value;
        if (adaptee.isAssignableFrom(Double.class)) {
            return adaptee.cast(num.doubleValue());
        } else if (adaptee.isAssignableFrom(Float.class)) {
            return adaptee.cast(num.floatValue());
        } else if (adaptee.isAssignableFrom(Integer.class)) {
            return adaptee.cast(num.intValue());
        } else if (adaptee.isAssignableFrom(Long.class)) {
            return adaptee.cast(num.longValue());
        } else if (adaptee.isAssignableFrom(String.class)) {
            return adaptee.cast(num.toString());
        } else {
            throw new IllegalArgumentException();
        }
    } else if (value instanceof String) {
        if (adaptee.isAssignableFrom(Double.class)) {
            try {
                Double parsed = Double.parseDouble((String) value);
                return adaptee.cast(parsed);
            } catch (Exception e) {
                return null;
            }
        } else if (adaptee.isAssignableFrom(Float.class)) {
            try {
                Float parsed = Float.parseFloat((String) value);
                return adaptee.cast(parsed);
            } catch (Exception e) {
                return null;
            }
        } else if (adaptee.isAssignableFrom(Integer.class)) {
            try {
                Integer parsed = Integer.parseInt((String) value);
                return adaptee.cast(parsed);
            } catch (Exception e) {
                return null;
            }
        } else if (adaptee.isAssignableFrom(String.class)) {
            return adaptee.cast(value);
        } else {
            throw new IllegalArgumentException();
        }
    } else {
        throw new IllegalArgumentException(
                "Can't adapt attribute of type: " + value.getClass().getCanonicalName()); //$NON-NLS-1$
    }
}

From source file:NumberUtils.java

/**
 * Converts the given number to a <code>java.math.BigInteger</code>.
 *
 * @param number/*  w ww . j a  va2 s.co m*/
 * @return java.math.BigInteger
 * @throws IllegalArgumentException The given number is 'not a number' or infinite.
 */
public static BigInteger toBigInteger(Number number) throws IllegalArgumentException {
    if (number == null || number instanceof BigInteger)
        return (BigInteger) number;
    if (number instanceof BigDecimal)
        return ((BigDecimal) number).toBigInteger();
    if (isDoubleCompatible(number)) {
        if (isNaN(number) || isInfinite(number))
            throw new IllegalArgumentException("Argument must not be NaN or infinite.");
        return new BigDecimal(number.toString()).toBigInteger();
    } // => isLongCompatible(number) or unknown Number type
    return BigInteger.valueOf(number.longValue());
}

From source file:org.paxle.se.index.lucene.impl.Converter.java

private static Fieldable number2field(org.paxle.core.doc.Field<?> field, Number data) {
    /* ===========================================================
     * Numbers//from   www  .ja  v  a  2 s .c  o m
     * - may be stored (if so, not compressed)
     * - may be indexed (no tokenization)
     * - no term vectors
     * =========================================================== */
    final long num;
    if (data instanceof Double) {
        num = Double.doubleToLongBits(data.doubleValue());
    } else if (data instanceof Float) {
        num = Float.floatToIntBits(data.floatValue());
    } else {
        num = data.longValue();
    }

    if (field.isIndex()) {
        return new Field(field.getName(), PaxleNumberTools.longToString(num), store(field, false),
                index(field));
    } else {
        return new Field(field.getName(), PaxleNumberTools.toBytes(num), store(field, false));
    }
}

From source file:com.redhat.rhn.domain.server.ServerFactory.java

/**
 * List all proxies for a given org//from w  w  w  . j  av  a  2 s. c om
 * @param user the user, who's accessible proxies will be returned.
 * @return a list of Proxy Server objects
 */
public static List<Server> lookupProxiesByOrg(User user) {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("userId", user.getId());
    params.put("orgId", user.getOrg().getId());
    List<Number> ids = singleton.listObjectsByNamedQuery("Server.listProxies", params);
    List<Server> servers = new ArrayList(ids.size());
    for (Number id : ids) {
        servers.add(lookupById(id.longValue()));
    }
    return servers;
}

From source file:com.redhat.rhn.domain.server.ServerFactory.java

/**
 * transforms a result set of id,name, count to a HostAndGuestCountView
 * object/*from   w ww . j a v  a  2 s.c o m*/
 * @param result a list of Object array of id,name, count
 * @return list of HostAndGuestCountView objects
 */
private static List convertToCountView(List out) {
    List ret = new ArrayList(out.size());
    for (Iterator itr = out.iterator(); itr.hasNext();) {
        Object[] row = (Object[]) itr.next();

        Number hostId = (Number) row[0];
        Long theHostId = new Long(hostId.longValue());
        String theHostName = (String) row[1];
        int guestCount = ((Number) row[2]).intValue();

        HostAndGuestCountView view = new HostAndGuestCountView(theHostId, theHostName, guestCount);
        ret.add(view);
    }
    return ret;
}

From source file:org.apache.jackrabbit.oak.plugins.document.util.Utils.java

/**
 * Returns the given number instance as a {@code Long}.
 *
 * @param n a number or {@code null}./*from   w ww  .  j av a 2  s  . c om*/
 * @return the number converted to a {@code Long} or {@code null}
 *      if {@code n} is {@code null}.
 */
public static Long asLong(@Nullable Number n) {
    if (n == null) {
        return null;
    } else if (n instanceof Long) {
        return (Long) n;
    } else {
        return n.longValue();
    }
}