List of usage examples for java.lang Number longValue
public abstract long longValue();
From source file:org.arkhamnetwork.playersync.utils.SerializationUtils.java
private static final NBTBase javaTypeToNBTTag(Object object) throws IOException { // Handle compounds if (object instanceof Map) { @SuppressWarnings("unchecked") Map<String, ?> map = (Map<String, ?>) object; NBTTagCompound tag = new NBTTagCompound(); for (Entry<String, ?> entry : map.entrySet()) { tag.set(entry.getKey(), javaTypeToNBTTag(entry.getValue())); }/*w ww.j a v a2s. c o m*/ return tag; } // Handle numbers if (object instanceof Number) { Number number = (Number) object; if (number.longValue() == number.doubleValue()) { // Whole number if (number.intValue() == number.longValue()) { // Fits in integer return new NBTTagInt(number.intValue()); } return new NBTTagLong(number.longValue()); } else { return new NBTTagDouble(number.doubleValue()); } } // Handle strings if (object instanceof String) { return new NBTTagString((String) object); } // Handle lists if (object instanceof List) { List<?> list = (List<?>) object; NBTTagList listTag = new NBTTagList(); // Handle int arrays if (list.size() > 0) { Object firstElement = list.get(0); if (firstElement instanceof Number) { @SuppressWarnings("unchecked") List<Number> intList = (List<Number>) list; return new NBTTagIntArray(unboxIntegers(intList)); } } for (Object entry : list) { listTag.add(javaTypeToNBTTag(entry)); } return listTag; } throw new IOException("Unknown object: (" + object.getClass() + ") " + object + ""); }
From source file:gedi.util.MathUtils.java
/** * Throws an exception if n is either a real or to big to be represented by a byte. * @param n/*from w w w . ja v a 2s .c om*/ * @return */ public static int intValueExact(Number n) { if (n instanceof Integer || n instanceof Short || n instanceof Byte) return n.intValue(); double d = n.doubleValue(); long l = n.longValue(); if (d == (double) l) { if (l >= Integer.MIN_VALUE && l <= Integer.MAX_VALUE) return (int) l; } throw new NumberFormatException(); }
From source file:com.paladin.sys.db.QueryHelper.java
/** * ???//w w w. j a v a 2 s .c om * * @param sql * @param params * @return */ public static long stat(String sql, Object... params) { try { Number num = (Number) RUNNER.query(getConnection(), sql, scalar_handler, params); return (num != null) ? num.longValue() : -1; } catch (SQLException e) { e.printStackTrace(); return -1; } finally { DBManager.closeConnection(); } }
From source file:org.javersion.path.NodeId.java
public static NodeId index(Number number) { return index(number.longValue()); }
From source file:Main.java
static Object convert(Object[] objects, Object var) { Object newVar = getObject(objects); if (newVar instanceof Number) { Number newNum = (Number) var; if (newVar instanceof Integer) { return new Integer(newNum.intValue()); } else if (newVar instanceof Long) { return new Long(newNum.longValue()); } else if (newVar instanceof Float) { return new Float(newNum.floatValue()); } else if (newVar instanceof Double) { return new Double(newNum.doubleValue()); } else {// w w w . j a v a 2s .c om return null; } } else if (newVar instanceof String) { return new String((String) newVar); } else { //TODO: add other classes return null; } }
From source file:Main.java
/** * Casts an object to the specified type * * @param var/*ww w.j av a2 s . c o m*/ * @param type * */ static Object convert(Object var, Class type) { if (var instanceof Number) { //use number conversion Number newNum = (Number) var; if (type == Integer.class) { return new Integer(newNum.intValue()); } else if (type == Long.class) { return new Long(newNum.longValue()); } else if (type == Float.class) { return new Float(newNum.floatValue()); } else if (type == Double.class) { return new Double(newNum.doubleValue()); } else if (type == String.class) { return new String(newNum.toString()); } } else { //direct cast if (type == Integer.class) { return new Integer(((Integer) var).intValue()); } else if (type == Long.class) { return new Long(((Long) var).longValue()); } else if (type == Float.class) { return new Float(((Float) var).floatValue()); } else if (type == Double.class) { return new Double(((Double) var).doubleValue()); } else if (type == String.class) { return new String(var.toString()); } } return null; }
From source file:NumberInRange.java
public static boolean isInRange(Number number, BigDecimal min, BigDecimal max) { try {// w ww . ja va 2 s . c om BigDecimal bigDecimal = null; if (number instanceof Byte || number instanceof Short || number instanceof Integer || number instanceof Long) { bigDecimal = new BigDecimal(number.longValue()); } else if (number instanceof Float || number instanceof Double) { bigDecimal = new BigDecimal(number.doubleValue()); } else if (number instanceof BigInteger) { bigDecimal = new BigDecimal((BigInteger) number); } else if (number instanceof BigDecimal) { bigDecimal = (BigDecimal) number; } else { bigDecimal = new BigDecimal(number.doubleValue()); } return max.compareTo(bigDecimal) >= 0 && min.compareTo(bigDecimal) <= 0; } catch (NumberFormatException e) { return false; } }
From source file:NumberInRange.java
public static boolean isInRange(Number number, BigInteger min, BigInteger max) { try {// w ww . ja va 2 s.c om BigInteger bigInteger = null; if (number instanceof Byte || number instanceof Short || number instanceof Integer || number instanceof Long) { bigInteger = BigInteger.valueOf(number.longValue()); } else if (number instanceof Float || number instanceof Double) { bigInteger = new BigDecimal(number.doubleValue()).toBigInteger(); } else if (number instanceof BigInteger) { bigInteger = (BigInteger) number; } else if (number instanceof BigDecimal) { bigInteger = ((BigDecimal) number).toBigInteger(); } else { // not a standard number bigInteger = new BigDecimal(number.doubleValue()).toBigInteger(); } return max.compareTo(bigInteger) >= 0 && min.compareTo(bigInteger) <= 0; } catch (NumberFormatException e) { return false; } }
From source file:Main.java
/** * This method converts a given number into a target class. This method does not change the value (except when * explicitly casting to a more general type, e.g. from double to int), just the internal type representation. While * this is unnecessary while using normal java code, reflection based access to method parameters is a bit more * difficult. As far as possible, this method will prevent the ArgumentMismatch error when passing numbers as * parameters./*from w ww .ja va 2 s . c o m*/ * <p/> * If the value can not be converted to the given target class, it will be returned unchanged. * * @param targetClass Class to which the number should be converted, if possible. * @param value Number value to convert. * @return 'value' converted to an instance of 'targetClass'. */ public static Object convertNumber(final Class targetClass, final Number value) { if (targetClass.equals(Double.class) || targetClass.equals(Double.TYPE)) return value.doubleValue(); if (targetClass.equals(Integer.class) || targetClass.equals(Integer.TYPE)) return value.intValue(); if (targetClass.equals(Long.class) || targetClass.equals(Long.TYPE)) return value.longValue(); if (targetClass.equals(Short.class) || targetClass.equals(Short.TYPE)) return value.shortValue(); if (targetClass.equals(Byte.class) || targetClass.equals(Byte.TYPE)) return value.byteValue(); if (targetClass.equals(Character.class) || targetClass.equals(Character.TYPE)) return value.intValue(); if (targetClass.equals(Float.class) || targetClass.equals(Float.TYPE)) return value.floatValue(); return value; }
From source file:com.redhat.rhn.domain.server.ServerGroupFactory.java
/** * Returns the value listed by current members column * on the rhnServerGroup table.. This was made as a query * instead of mapping because this column is only updated * by the stored procedures dealing with entitlements.. * @param sg the server group to get the current members of * @return the value of the the currentmemebers column. *//*from w w w . j a va2s.com*/ public static Long getCurrentMembers(ServerGroup sg) { Map<String, Object> params = new HashMap<String, Object>(); params.put("sgid", sg.getId()); Object obj = SINGLETON.lookupObjectByNamedQuery("ServerGroup.lookupCurrentMembersValue", params); Number members = (Number) obj; if (members == null) { return new Long(0); } return new Long(members.longValue()); }