List of usage examples for java.lang Number byteValue
public byte byteValue()
From source file:org.diorite.config.impl.ConfigPropertyValueImpl.java
@SuppressWarnings("unchecked") @Override// w w w. j a v a 2 s .c o m public void setRawValue(@Nullable T value) { Class<T> rawType = this.template.getRawType(); Class<?> primitiveRawType = DioriteReflectionUtils.getPrimitive(rawType); if (!primitiveRawType.isPrimitive()) { if (value != null) { if (!rawType.isInstance(value) && !DioriteReflectionUtils.getWrapperClass(rawType).isInstance(value)) { throw new IllegalArgumentException("Invalid object type: " + value + " in template property: " + this.template.getName() + " (" + this.template.getGenericType() + ")"); } } else { // keep collections not null if possible. try { if (Collection.class.isAssignableFrom(rawType) || (Map.class.isAssignableFrom(rawType) && !Config.class.isAssignableFrom(rawType))) { this.rawValue = YamlCollectionCreator.createCollection(rawType, 10); return; } } catch (Exception e) { e.printStackTrace(); } } } else if (value instanceof Number) { Number num = (Number) value; if (primitiveRawType == byte.class) { num = num.byteValue(); } if (primitiveRawType == short.class) { num = num.shortValue(); } if (primitiveRawType == int.class) { num = num.intValue(); } if (primitiveRawType == long.class) { num = num.longValue(); } if (primitiveRawType == float.class) { num = num.floatValue(); } if (primitiveRawType == double.class) { num = num.doubleValue(); } this.rawValue = (T) num; return; } else { throw new IllegalArgumentException("Invalid object type: " + value + " in template property: " + this.template.getName() + " (" + this.template.getGenericType() + ")"); } this.rawValue = value; }
From source file:com.krawler.br.nodes.Activity.java
/** * converts the given object value to the given primitive type's wrapper class * if possible (if it is a <tt>Number</tt>) * * @param type the premitive type name, may be: * <ul>/* w ww . j a va 2 s . c o m*/ * <li><tt>byte</tt> * <li><tt>char</tt> * <li><tt>short</tt> * <li><tt>int</tt> * <li><tt>long</tt> * <li><tt>float</tt> * <li><tt>double</tt> * </ul> * @param value the original value * @return the converted value */ private Object convert(String type, Object value) { Object val = value; if (value instanceof Number) { Number num = (Number) value; if (Byte.class.getCanonicalName().equals(type)) val = new Byte(num.byteValue()); else if (Character.class.getCanonicalName().equals(type)) val = new Character((char) num.intValue()); else if (Short.class.getCanonicalName().equals(type)) val = new Short(num.shortValue()); else if (Integer.class.getCanonicalName().equals(type)) val = new Integer(num.intValue()); else if (Long.class.getCanonicalName().equals(type)) val = new Long(num.longValue()); else if (Float.class.getCanonicalName().equals(type)) val = new Float(num.floatValue()); else if (Double.class.getCanonicalName().equals(type)) val = new Double(num.doubleValue()); } return val; }
From source file:javadz.beanutils.locale.converters.ByteLocaleConverter.java
/** * Convert the specified locale-sensitive input object into an output object of the * specified type. This method will return values of type Byte. * * @param value The input object to be converted * @param pattern The pattern is used for the convertion * @return The converted value/*from w ww . j a va 2 s . co m*/ * * @exception org.apache.commons.beanutils.ConversionException if conversion cannot be performed * successfully * @throws ParseException if an error occurs parsing a String to a Number */ protected Object parse(Object value, String pattern) throws ParseException { final Number parsed = (Number) super.parse(value, pattern); if (parsed.longValue() != parsed.byteValue()) { throw new ConversionException("Supplied number is not of type Byte: " + parsed.longValue()); } // now returns property Byte return new Byte(parsed.byteValue()); }
From source file:com.scit.sling.test.MockValueMap.java
@SuppressWarnings("unchecked") private <T> T convertType(Object o, Class<T> type) { if (o == null) { return null; }//from w w w.j a v a2s . c om if (o.getClass().isArray() && type.isArray()) { if (type.getComponentType().isAssignableFrom(o.getClass().getComponentType())) { return (T) o; } else { // we need to convert the elements in the array Object array = Array.newInstance(type.getComponentType(), Array.getLength(o)); for (int i = 0; i < Array.getLength(o); i++) { Array.set(array, i, convertType(Array.get(o, i), type.getComponentType())); } return (T) array; } } if (o.getClass().isAssignableFrom(type)) { return (T) o; } if (String.class.isAssignableFrom(type)) { // Format dates if (o instanceof Calendar) { return (T) formatDate((Calendar) o); } else if (o instanceof Date) { return (T) formatDate((Date) o); } else if (o instanceof DateTime) { return (T) formatDate((DateTime) o); } return (T) String.valueOf(o); } else if (o instanceof DateTime) { DateTime dt = (DateTime) o; if (Calendar.class.isAssignableFrom(type)) { return (T) dt.toCalendar(Locale.getDefault()); } else if (Date.class.isAssignableFrom(type)) { return (T) dt.toDate(); } else if (Number.class.isAssignableFrom(type)) { return convertType(dt.getMillis(), type); } } else if (o instanceof Number && Number.class.isAssignableFrom(type)) { if (Byte.class.isAssignableFrom(type)) { return (T) (Byte) ((Number) o).byteValue(); } else if (Double.class.isAssignableFrom(type)) { return (T) (Double) ((Number) o).doubleValue(); } else if (Float.class.isAssignableFrom(type)) { return (T) (Float) ((Number) o).floatValue(); } else if (Integer.class.isAssignableFrom(type)) { return (T) (Integer) ((Number) o).intValue(); } else if (Long.class.isAssignableFrom(type)) { return (T) (Long) ((Number) o).longValue(); } else if (Short.class.isAssignableFrom(type)) { return (T) (Short) ((Number) o).shortValue(); } else if (BigDecimal.class.isAssignableFrom(type)) { return (T) new BigDecimal(o.toString()); } } else if (o instanceof Number && type.isPrimitive()) { final Number num = (Number) o; if (type == byte.class) { return (T) new Byte(num.byteValue()); } else if (type == double.class) { return (T) new Double(num.doubleValue()); } else if (type == float.class) { return (T) new Float(num.floatValue()); } else if (type == int.class) { return (T) new Integer(num.intValue()); } else if (type == long.class) { return (T) new Long(num.longValue()); } else if (type == short.class) { return (T) new Short(num.shortValue()); } } else if (o instanceof String && Number.class.isAssignableFrom(type)) { if (Byte.class.isAssignableFrom(type)) { return (T) new Byte((String) o); } else if (Double.class.isAssignableFrom(type)) { return (T) new Double((String) o); } else if (Float.class.isAssignableFrom(type)) { return (T) new Float((String) o); } else if (Integer.class.isAssignableFrom(type)) { return (T) new Integer((String) o); } else if (Long.class.isAssignableFrom(type)) { return (T) new Long((String) o); } else if (Short.class.isAssignableFrom(type)) { return (T) new Short((String) o); } else if (BigDecimal.class.isAssignableFrom(type)) { return (T) new BigDecimal((String) o); } } throw new NotImplementedException( "Can't handle conversion from " + o.getClass().getName() + " to " + type.getName()); }
From source file:net.sf.jasperreports.engine.data.JRAbstractTextDataSource.java
protected Object convertNumber(Number number, Class<?> valueClass) throws JRException { Number value = null;// ww w . j a v a2 s . c o m if (valueClass.equals(Byte.class)) { value = number.byteValue(); } else if (valueClass.equals(Short.class)) { value = number.shortValue(); } else if (valueClass.equals(Integer.class)) { value = number.intValue(); } else if (valueClass.equals(Long.class)) { value = number.longValue(); } else if (valueClass.equals(Float.class)) { value = number.floatValue(); } else if (valueClass.equals(Double.class)) { value = number.doubleValue(); } else if (valueClass.equals(BigInteger.class)) { value = BigInteger.valueOf(number.longValue()); } else if (valueClass.equals(BigDecimal.class)) { value = new BigDecimal(Double.toString(number.doubleValue())); } else { throw new JRException(EXCEPTION_MESSAGE_KEY_UNKNOWN_NUMBER_TYPE, new Object[] { valueClass.getName() }); } return value; }
From source file:MutableByte.java
/** * Adds a value./* ww w. j av a2 s . c o m*/ * * @param operand * the value to add * @throws NullPointerException * if the object is null * * @since Commons Lang 2.2 */ public void add(Number operand) { this.value += operand.byteValue(); }
From source file:MutableByte.java
/** * Subtracts a value./*from ww w . j av a 2s .c o m*/ * * @param operand * the value to add * @throws NullPointerException * if the object is null * * @since Commons Lang 2.2 */ public void subtract(Number operand) { this.value -= operand.byteValue(); }
From source file:MutableByte.java
/** * Constructs a new MutableByte with the specified value. * /*from w ww .j ava 2s.co m*/ * @param value * a value. * @throws NullPointerException * if the object is null */ public MutableByte(Number value) { super(); this.value = value.byteValue(); }
From source file:org.red5.io.utils.ConversionUtils.java
/** * Convert number to primitive wrapper like Boolean or Float * //from w w w.j a v a2s .c o m * @param num * Number to conver * @param wrapper * Primitive wrapper type * @return Converted object */ public static Object convertNumberToWrapper(Number num, Class<?> wrapper) { //XXX Paul: Using valueOf will reduce object creation if (wrapper.equals(String.class)) { return num.toString(); } else if (wrapper.equals(Boolean.class)) { return Boolean.valueOf(num.intValue() == 1); } else if (wrapper.equals(Double.class)) { return Double.valueOf(num.doubleValue()); } else if (wrapper.equals(Long.class)) { return Long.valueOf(num.longValue()); } else if (wrapper.equals(Float.class)) { return Float.valueOf(num.floatValue()); } else if (wrapper.equals(Integer.class)) { return Integer.valueOf(num.intValue()); } else if (wrapper.equals(Short.class)) { return Short.valueOf(num.shortValue()); } else if (wrapper.equals(Byte.class)) { return Byte.valueOf(num.byteValue()); } throw new ConversionException(String.format("Unable to convert number to: %s", wrapper)); }
From source file:org.apache.sqoop.mapreduce.hcat.SqoopHCatExportHelper.java
private Object convertNumberTypes(Object val, String javaColType) { Number n = (Number) val; if (javaColType.equals(BYTE_TYPE)) { return n.byteValue(); } else if (javaColType.equals(SHORT_TYPE)) { return n.shortValue(); } else if (javaColType.equals(INTEGER_TYPE)) { return n.intValue(); } else if (javaColType.equals(LONG_TYPE)) { return n.longValue(); } else if (javaColType.equals(FLOAT_TYPE)) { return n.floatValue(); } else if (javaColType.equals(DOUBLE_TYPE)) { return n.doubleValue(); } else if (javaColType.equals(BIG_DECIMAL_TYPE)) { return new BigDecimal(n.doubleValue()); } else if (javaColType.equals(BOOLEAN_TYPE)) { return n.byteValue() == 0 ? Boolean.FALSE : Boolean.TRUE; } else if (javaColType.equals(STRING_TYPE)) { return n.toString(); }/* w ww. j a v a 2s.c o m*/ return null; }