List of usage examples for java.lang Number byteValue
public byte byteValue()
From source file:cc.aileron.accessor.TypeConvertorImpl.java
/** * default constractor/*from w w w.j a v a 2 s .c om*/ */ @Inject public TypeConvertorImpl() { map.put(Boolean.TYPE, new C() { @Override public Boolean convert(final java.lang.Number number) { return number.intValue() != 0; } }); map.put(Byte.TYPE, new C() { @Override public Byte convert(final java.lang.Number number) { return number.byteValue(); } }); map.put(Short.TYPE, new C() { @Override public Short convert(final java.lang.Number number) { return number.shortValue(); } }); map.put(Integer.TYPE, new C() { @Override public Integer convert(final java.lang.Number number) { return number.intValue(); } }); map.put(Long.TYPE, new C() { @Override public Long convert(final java.lang.Number number) { return number.longValue(); } }); map.put(Float.TYPE, new C() { @Override public Float convert(final java.lang.Number number) { return number.floatValue(); } }); map.put(Double.TYPE, new C() { @Override public Double convert(final java.lang.Number number) { return number.doubleValue(); } }); }
From source file:org.apache.sqoop.mapreduce.hcat.SqoopHCatImportHelper.java
private Object convertNumberTypes(Object val, HCatFieldSchema hfs) { HCatFieldSchema.Type hfsType = hfs.getType(); if (!(val instanceof Number)) { return null; }//from w w w. java 2 s .c om if (val instanceof BigDecimal && hfsType == HCatFieldSchema.Type.STRING || hfsType == HCatFieldSchema.Type.VARCHAR || hfsType == HCatFieldSchema.Type.CHAR) { BigDecimal bd = (BigDecimal) val; String bdStr = null; if (bigDecimalFormatString) { bdStr = bd.toPlainString(); } else { bdStr = bd.toString(); } if (hfsType == HCatFieldSchema.Type.VARCHAR) { VarcharTypeInfo vti = (VarcharTypeInfo) hfs.getTypeInfo(); HiveVarchar hvc = new HiveVarchar(bdStr, vti.getLength()); return hvc; } else if (hfsType == HCatFieldSchema.Type.VARCHAR) { CharTypeInfo cti = (CharTypeInfo) hfs.getTypeInfo(); HiveChar hChar = new HiveChar(bdStr, cti.getLength()); return hChar; } else { return bdStr; } } Number n = (Number) val; if (hfsType == HCatFieldSchema.Type.TINYINT) { return n.byteValue(); } else if (hfsType == HCatFieldSchema.Type.SMALLINT) { return n.shortValue(); } else if (hfsType == HCatFieldSchema.Type.INT) { return n.intValue(); } else if (hfsType == HCatFieldSchema.Type.BIGINT) { return n.longValue(); } else if (hfsType == HCatFieldSchema.Type.FLOAT) { return n.floatValue(); } else if (hfsType == HCatFieldSchema.Type.DOUBLE) { return n.doubleValue(); } else if (hfsType == HCatFieldSchema.Type.BOOLEAN) { return n.byteValue() == 0 ? Boolean.FALSE : Boolean.TRUE; } else if (hfsType == HCatFieldSchema.Type.STRING) { return n.toString(); } else if (hfsType == HCatFieldSchema.Type.VARCHAR) { VarcharTypeInfo vti = (VarcharTypeInfo) hfs.getTypeInfo(); HiveVarchar hvc = new HiveVarchar(val.toString(), vti.getLength()); return hvc; } else if (hfsType == HCatFieldSchema.Type.CHAR) { CharTypeInfo cti = (CharTypeInfo) hfs.getTypeInfo(); HiveChar hChar = new HiveChar(val.toString(), cti.getLength()); return hChar; } else if (hfsType == HCatFieldSchema.Type.DECIMAL) { BigDecimal bd = new BigDecimal(n.doubleValue(), MathContext.DECIMAL128); return HiveDecimal.create(bd); } return null; }
From source file:de.instantouch.model.io.SnakeJSONReader.java
public void read(SnakeType type, Object value) throws SnakeModelException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { if (type.hasReference()) { SnakeReference ref = type.getReference(); if (ref == null) { throw new SnakeNotInitializedException("reference is not initialized"); }/*from w ww .j ava 2 s.com*/ SnakeType key = ref.getKey(); if (key == null) { throw new SnakeNotInitializedException("no reference key container os given"); } read(key, value); } else { if (type instanceof SnakeEntity) { readEntity((SnakeEntity) type, value); } else if (type instanceof SnakeList) { readList((SnakeList) type, value); } else if (type instanceof SnakeMap) { readMap((SnakeMap) type, value); } else if (value == null) { type.setNull(); } else if (value instanceof String) { type.set(value.toString()); } else if (value instanceof Boolean) { type.set(value.toString()); } else if (value instanceof Number) { Number number = (Number) value; if (value instanceof Double) { type.set(number.doubleValue()); } else if (value instanceof Float) { type.set(number.floatValue()); } else if (value instanceof Byte) { type.set(number.byteValue()); } else if (value instanceof Short) { type.set(number.shortValue()); } else if (value instanceof Integer) { type.set(number.intValue()); } else if (value instanceof Long) { type.set(number.longValue()); } } } }
From source file:org.qi4j.runtime.types.NumberType.java
public Object fromJSON(Object json, Module module) { if (json == JSONObject.NULL) { return null; }/*w w w . j a va 2s . com*/ if (type.isClass(BigDecimal.class)) { return new BigDecimal("" + json); } else if (type.isClass(BigInteger.class)) { return new BigInteger("" + json); } Number number = (Number) json; if (type.isClass(Integer.class)) { return number.intValue(); } else if (type.isClass(Long.class)) { return number.longValue(); } else if (type.isClass(Double.class)) { return number.doubleValue(); } else if (type.isClass(Float.class)) { return number.floatValue(); } else if (type.isClass(Short.class)) { return number.shortValue(); } else if (type.isClass(Byte.class)) { return number.byteValue(); } throw new IllegalStateException("Unknown number type:" + type); }
From source file:com.amazonaws.hal.client.ConversionUtil.java
private static Object convertFromNumber(Class<?> clazz, Number value) { if (String.class.isAssignableFrom(clazz)) { return value.toString(); } else if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz)) { return value.intValue(); } else if (long.class.isAssignableFrom(clazz) || Long.class.isAssignableFrom(clazz)) { return value.longValue(); } else if (short.class.isAssignableFrom(clazz) || Short.class.isAssignableFrom(clazz)) { return value.shortValue(); } else if (double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz)) { return value.doubleValue(); } else if (float.class.isAssignableFrom(clazz) || Float.class.isAssignableFrom(clazz)) { return value.floatValue(); } else if (boolean.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz)) { return Boolean.valueOf(value.toString()); } else if (char.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz)) { if (value.longValue() <= 255) { return (char) value.longValue(); } else {//from w ww .j a v a2s. c o m throw new RuntimeException("Not sure how to convert " + value + " to a " + clazz.getSimpleName()); } } else if (byte.class.isAssignableFrom(clazz) || Byte.class.isAssignableFrom(clazz)) { return value.byteValue(); } else if (BigDecimal.class.isAssignableFrom(clazz)) { return new BigDecimal(value.toString()); } else if (BigInteger.class.isAssignableFrom(clazz)) { // Necessary because BigInteger(long) is a private method and we need to convert the Number to a long to // prevent the constructor from throwing a NumberFormatException Example: BigInteger(1.2) return new BigInteger(String.valueOf(value.longValue())); } else if (Date.class.isAssignableFrom(clazz)) { return new Date(value.longValue()); } else if (clazz.isEnum()) { try { //noinspection unchecked return Enum.valueOf((Class<Enum>) clazz, value.toString()); } catch (IllegalArgumentException e) { log.error(String.format( "'%s' is not a recognized enum value for %s. Returning default of %s instead.", value, clazz.getName(), clazz.getEnumConstants()[0])); return clazz.getEnumConstants()[0]; } } else { throw new RuntimeException("Not sure how to convert " + value + " to a " + clazz.getSimpleName()); } }
From source file:org.jasper.collectionspace.smk.datasource.JsonCSDataSource.java
protected Object convertNumber(Number number, Class<?> valueClass) throws JRException { Number value = null;// ww w. j a v a 2s .com if (valueClass.equals(Byte.class)) { value = new Byte(number.byteValue()); } else if (valueClass.equals(Short.class)) { value = new Short(number.shortValue()); } else if (valueClass.equals(Integer.class)) { value = Integer.valueOf(number.intValue()); } else if (valueClass.equals(Long.class)) { value = new Long(number.longValue()); } else if (valueClass.equals(Float.class)) { value = new Float(number.floatValue()); } else if (valueClass.equals(Double.class)) { value = new Double(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("Unknown number class " + valueClass.getName()); } return value; }
From source file:com.itelis.worker.dev.template.service.JRXmlDataSource.java
protected Object convertNumber(Number number, Class valueClass) throws JRException { Number value = null;/* w w w . ja va 2 s . c o m*/ if (valueClass.equals(Byte.class)) { value = new Byte(number.byteValue()); } else if (valueClass.equals(Short.class)) { value = new Short(number.shortValue()); } else if (valueClass.equals(Integer.class)) { value = new Integer(number.intValue()); } else if (valueClass.equals(Long.class)) { value = new Long(number.longValue()); } else if (valueClass.equals(Float.class)) { value = new Float(number.floatValue()); } else if (valueClass.equals(Double.class)) { value = new Double(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("Unknown number class " + valueClass.getName()); } return value; }
From source file:moe.encode.airblock.commands.arguments.types.PrimitiveParser.java
@Override public Object convert(Executor executor, ArgumentConverter parser, Type type, String value) { Class<?> cls = ReflectionUtils.toClass(type); if (ClassUtils.isPrimitiveWrapper(cls)) cls = ClassUtils.wrapperToPrimitive(cls); if (cls.equals(boolean.class)) return this.isTrue(executor, value); else if (cls.equals(char.class)) { if (value.length() > 0) throw new NumberFormatException("Character arguments cannot be longer than one characters"); return value.charAt(0); }/*from ww w .j av a 2s.com*/ // Get the locale of the user and get a number-format according to it. LocaleResolver resolver = TranslationManager.getResolver(executor); Locale locale; if (resolver != null) locale = resolver.getLocale(); else locale = Locale.ENGLISH; NumberFormat nf = NumberFormat.getNumberInstance(locale); nf.setGroupingUsed(true); // Parse the value. Number result; try { result = nf.parse(value); } catch (ParseException e) { NumberFormatException nfe = new NumberFormatException("Invalid number"); nfe.initCause(e); throw nfe; } // Returns the value in the correct type. if (cls.equals(int.class)) return result.intValue(); else if (cls.equals(float.class)) return result.floatValue(); else if (cls.equals(double.class)) return result.doubleValue(); else if (cls.equals(byte.class)) return result.byteValue(); else if (cls.equals(short.class)) return result.shortValue(); else if (cls.equals(long.class)) return result.longValue(); throw new NumberFormatException("Unknown primitive type."); }
From source file:Unsigned.java
/** * Parse a binary number into a Number object. If up to 8 bits are parsed, * returns a Byte. If more than 8 and up to 16 bits are parsed, return a * Short. If more than 16 and up to 32 bits are parsed, return an Integer. * If more than 32 and up to 64 bits are parsed, return a Long. * /* www .j a va 2 s . c o m*/ * @param text * a binary number * @param parsePosition * position to start parsing from * @return return an integer form of Number object if parse is successful; * <CODE>null</CODE> otherwise * * @since 1.0 */ public Number parse(String text, ParsePosition parsePosition) { boolean skipWhitespace = true; int startIndex, bits; // remove whitespace StringCharacterIterator iter = new StringCharacterIterator(text, parsePosition.getIndex()); for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) { if (skipWhitespace && Character.isWhitespace(c)) { // skip whitespace continue; } } parsePosition.setIndex(iter.getIndex()); startIndex = parsePosition.getIndex(); Number result = (Number) parseObject(text, parsePosition); if (result == null) { return (result); } bits = parsePosition.getIndex() - startIndex; if (bits <= 8) { result = new Byte(result.byteValue()); } else if (bits <= 16) { result = new Short(result.shortValue()); } else if (bits <= 32) { result = new Integer(result.intValue()); } else if (bits <= 64) { result = new Long(result.longValue()); } return (result); }
From source file:HexFormat.java
/** * Parse a hex number into a Number object. Hexadecimal numbers may be * indicated with a leading character designation of '0x'. If up to 1 byte * is parsed, returns a Byte. If more than 1 and up to 2 bytes are parsed, * return a Short. If more than 2 and up to 4 bytes are parsed, return an * Integer. If more than 4 and up to 8 bytes are parsed, return a Long. * /* w ww . j a va2 s . c o m*/ * @param text * a hexadecimal number * @param parsePosition * position to start parsing from * @return return an integer form of Number object if parse is successful; * <CODE>null</CODE> otherwise * * @since 1.0 */ public Number parse(String text, ParsePosition parsePosition) { boolean skipWhitespace = true; int startIndex, nibbles; // remove whitespace StringCharacterIterator iter = new StringCharacterIterator(text, parsePosition.getIndex()); for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) { if (skipWhitespace && Character.isWhitespace(c)) { // skip whitespace continue; } break; } // skip a leading hex designation of the characters '0x' if (text.regionMatches(iter.getIndex(), "0x", 0, 2)) { parsePosition.setIndex(iter.getIndex() + 2); } else { parsePosition.setIndex(iter.getIndex()); } startIndex = parsePosition.getIndex(); Number result = (Number) parseObject(text, parsePosition); if (result == null) { return (result); } nibbles = parsePosition.getIndex() - startIndex; if (nibbles <= 2) { result = new Byte(result.byteValue()); } else if (nibbles <= 4) { result = new Short(result.shortValue()); } else if (nibbles <= 8) { result = new Integer(result.intValue()); } else if (nibbles <= 16) { result = new Long(result.longValue()); } return (result); }