List of usage examples for java.lang Byte valueOf
public static Byte valueOf(String s) throws NumberFormatException
From source file:com.isencia.passerelle.message.TokenHelper.java
/** * Tries to get a Byte from the token, by: * <ul>/* w w w . java 2 s . c o m*/ * <li>checking if the token is not an ObjectToken, containing a Byte * <li>checking if the token is not an ObjectToken, and converting the object.toString() into a Byte * <li>checking if the token is not a StringToken, and converting the string into a Byte * <li>checking if the token is not a ScalarToken, and reading its byte value * </ul> * * @param token * @return */ public static Byte getByteFromToken(Token token) throws PasserelleException { if (logger.isTraceEnabled()) { logger.trace(token.toString()); // TODO Check if correct converted } Byte res = null; if (token != null) { try { if (token instanceof ObjectToken) { Object obj = ((ObjectToken) token).getValue(); if (obj instanceof Byte) { res = (Byte) obj; } else if (obj != null) { try { res = Byte.valueOf(obj.toString()); } catch (NumberFormatException e) { throw new PasserelleException(ErrorCode.MSG_CONTENT_TYPE_ERROR, "Invalid Byte format in " + token, e); } } } else { if (token instanceof StringToken) { String tokenMessage = ((StringToken) token).stringValue(); if (tokenMessage != null) { try { res = Byte.valueOf(tokenMessage); } catch (NumberFormatException e) { throw new PasserelleException(ErrorCode.MSG_CONTENT_TYPE_ERROR, "Invalid Byte format in " + token, e); } } } else if (token instanceof ScalarToken) { res = new Byte(((ScalarToken) token).byteValue()); } else { throw new PasserelleException(ErrorCode.MSG_CONTENT_TYPE_ERROR, "Invalid token for obtaining Byte value in " + token, null); } } } catch (Exception e) { throw new PasserelleException(ErrorCode.MSG_CONTENT_TYPE_ERROR, "Error building Byte from token in " + token, e); } } if (logger.isTraceEnabled()) { logger.trace("exit :" + res); } return res; }
From source file:org.apache.hadoop.hbase.security.access.HbaseObjectWritableFor96Migration.java
/** * Read a {@link Writable}, {@link String}, primitive type, or an array of * the preceding./*from ww w . ja v a 2s . co m*/ * @param in * @param objectWritable * @param conf * @return the object * @throws IOException */ @SuppressWarnings("unchecked") static Object readObject(DataInput in, HbaseObjectWritableFor96Migration objectWritable, Configuration conf) throws IOException { Class<?> declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in)); Object instance; if (declaredClass.isPrimitive()) { // primitive types if (declaredClass == Boolean.TYPE) { // boolean instance = Boolean.valueOf(in.readBoolean()); } else if (declaredClass == Character.TYPE) { // char instance = Character.valueOf(in.readChar()); } else if (declaredClass == Byte.TYPE) { // byte instance = Byte.valueOf(in.readByte()); } else if (declaredClass == Short.TYPE) { // short instance = Short.valueOf(in.readShort()); } else if (declaredClass == Integer.TYPE) { // int instance = Integer.valueOf(in.readInt()); } else if (declaredClass == Long.TYPE) { // long instance = Long.valueOf(in.readLong()); } else if (declaredClass == Float.TYPE) { // float instance = Float.valueOf(in.readFloat()); } else if (declaredClass == Double.TYPE) { // double instance = Double.valueOf(in.readDouble()); } else if (declaredClass == Void.TYPE) { // void instance = null; } else { throw new IllegalArgumentException("Not a primitive: " + declaredClass); } } else if (declaredClass.isArray()) { // array if (declaredClass.equals(byte[].class)) { instance = Bytes.readByteArray(in); } else { int length = in.readInt(); instance = Array.newInstance(declaredClass.getComponentType(), length); for (int i = 0; i < length; i++) { Array.set(instance, i, readObject(in, conf)); } } } else if (declaredClass.equals(Array.class)) { //an array not declared in CLASS_TO_CODE Class<?> componentType = readClass(conf, in); int length = in.readInt(); instance = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Array.set(instance, i, readObject(in, conf)); } } else if (List.class.isAssignableFrom(declaredClass)) { // List int length = in.readInt(); instance = new ArrayList(length); for (int i = 0; i < length; i++) { ((ArrayList) instance).add(readObject(in, conf)); } } else if (declaredClass == String.class) { // String instance = Text.readString(in); } else if (declaredClass.isEnum()) { // enum instance = Enum.valueOf((Class<? extends Enum>) declaredClass, Text.readString(in)); } else if (declaredClass == Message.class) { String className = Text.readString(in); try { declaredClass = getClassByName(conf, className); instance = tryInstantiateProtobuf(declaredClass, in); } catch (ClassNotFoundException e) { LOG.error("Can't find class " + className, e); throw new IOException("Can't find class " + className, e); } } else if (Scan.class.isAssignableFrom(declaredClass)) { int length = in.readInt(); byte[] scanBytes = new byte[length]; in.readFully(scanBytes); ClientProtos.Scan.Builder scanProto = ClientProtos.Scan.newBuilder(); instance = ProtobufUtil.toScan(scanProto.mergeFrom(scanBytes).build()); } else { // Writable or Serializable Class instanceClass = null; int b = (byte) WritableUtils.readVInt(in); if (b == NOT_ENCODED) { String className = Text.readString(in); try { instanceClass = getClassByName(conf, className); } catch (ClassNotFoundException e) { LOG.error("Can't find class " + className, e); throw new IOException("Can't find class " + className, e); } } else { instanceClass = CODE_TO_CLASS.get(b); } if (Writable.class.isAssignableFrom(instanceClass)) { Writable writable = WritableFactories.newInstance(instanceClass, conf); try { writable.readFields(in); } catch (Exception e) { LOG.error("Error in readFields", e); throw new IOException("Error in readFields", e); } instance = writable; if (instanceClass == NullInstance.class) { // null declaredClass = ((NullInstance) instance).declaredClass; instance = null; } } else { int length = in.readInt(); byte[] objectBytes = new byte[length]; in.readFully(objectBytes); ByteArrayInputStream bis = null; ObjectInputStream ois = null; try { bis = new ByteArrayInputStream(objectBytes); ois = new ObjectInputStream(bis); instance = ois.readObject(); } catch (ClassNotFoundException e) { LOG.error("Class not found when attempting to deserialize object", e); throw new IOException("Class not found when attempting to " + "deserialize object", e); } finally { if (bis != null) bis.close(); if (ois != null) ois.close(); } } } if (objectWritable != null) { // store values objectWritable.declaredClass = declaredClass; objectWritable.instance = instance; } return instance; }
From source file:com.ebay.nest.io.sede.thrift.TCTLSeparatedProtocol.java
@Override public byte readByte() throws TException { String val = readString(); lastPrimitiveWasNullFlag = val == null; try {/* w w w . j a v a 2 s . c o m*/ return val == null || val.isEmpty() ? 0 : Byte.valueOf(val).byteValue(); } catch (NumberFormatException e) { lastPrimitiveWasNullFlag = true; return 0; } }
From source file:org.apache.jasper.compiler.JspUtil.java
public static String coerceToPrimitiveByte(String s, boolean isNamedAttribute) { if (isNamedAttribute) { return "org.apache.jasper.runtime.JspRuntimeLibrary.coerceToByte(" + s + ")"; } else {/*ww w . ja v a 2s .c o m*/ if (s == null || s.length() == 0) return "(byte) 0"; else return "((byte)" + Byte.valueOf(s).toString() + ")"; } }
From source file:org.dspace.servicemanager.config.DSpaceConfigurationService.java
/** * Convert the value of a given property to a specific object type. * <P>//from www .j a v a 2s . c o m * Note: in most cases we can just use Configuration get*() methods. * * @param name Key of the property to convert * @param <T> object type * @return converted value */ private <T> T convert(String name, Class<T> type) { // If this key doesn't exist, just return null if (!configuration.containsKey(name)) { // Special case. For booleans, return false if key doesn't exist if (Boolean.class.equals(type) || boolean.class.equals(type)) return (T) Boolean.FALSE; else return null; } // Based on the type of class, call the appropriate // method of the Configuration object if (type.isArray()) return (T) configuration.getStringArray(name); else if (String.class.equals(type) || type.isAssignableFrom(String.class)) return (T) configuration.getString(name); else if (BigDecimal.class.equals(type)) return (T) configuration.getBigDecimal(name); else if (BigInteger.class.equals(type)) return (T) configuration.getBigInteger(name); else if (Boolean.class.equals(type) || boolean.class.equals(type)) return (T) Boolean.valueOf(configuration.getBoolean(name)); else if (Byte.class.equals(type) || byte.class.equals(type)) return (T) Byte.valueOf(configuration.getByte(name)); else if (Double.class.equals(type) || double.class.equals(type)) return (T) Double.valueOf(configuration.getDouble(name)); else if (Float.class.equals(type) || float.class.equals(type)) return (T) Float.valueOf(configuration.getFloat(name)); else if (Integer.class.equals(type) || int.class.equals(type)) return (T) Integer.valueOf(configuration.getInt(name)); else if (List.class.equals(type)) return (T) configuration.getList(name); else if (Long.class.equals(type) || long.class.equals(type)) return (T) Long.valueOf(configuration.getLong(name)); else if (Short.class.equals(type) || short.class.equals(type)) return (T) Short.valueOf(configuration.getShort(name)); else { // If none of the above works, try to convert the value to the required type SimpleTypeConverter converter = new SimpleTypeConverter(); return (T) converter.convertIfNecessary(configuration.getProperty(name), type); } }
From source file:org.eclipse.dataset.ByteDataset.java
@Override protected Number fromDoubleToNumber(double x) { byte r = (byte) (long) x; // ADD_CAST // PRIM_TYPE_LONG return Byte.valueOf(r); // CLASS_TYPE // return Integer.valueOf((int) (long) x); // BOOLEAN_USE // return null; // OBJECT_USE }
From source file:org.apache.jasper.compiler.JspUtil.java
public static String coerceToByte(String s, boolean isNamedAttribute) { if (isNamedAttribute) { return "(Byte) org.apache.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Byte.class)"; } else {/* ww w .jav a2 s.c o m*/ if (s == null || s.length() == 0) { return "new Byte((byte) 0)"; } else { // Detect format error at translation time return "new Byte((byte)" + Byte.valueOf(s).toString() + ")"; } } }
From source file:bammerbom.ultimatecore.bukkit.resources.utils.BossbarUtil.java
public Object getWatcher() { Class<?> Entity = Util.getCraftClass("Entity"); Class<?> DataWatcher = Util.getCraftClass("DataWatcher"); Object watcher = null;/* w w w .j av a 2s . c o m*/ try { watcher = DataWatcher.getConstructor(new Class[] { Entity }).newInstance(new Object[] { this.dragon }); Method a = Util.getMethod(DataWatcher, "a", new Class[] { Integer.TYPE, Object.class }); a.invoke(watcher, new Object[] { Integer.valueOf(0), Byte.valueOf(isVisible() ? (byte) 0 : (byte) 32) }); a.invoke(watcher, new Object[] { Integer.valueOf(6), Float.valueOf(this.health) }); a.invoke(watcher, new Object[] { Integer.valueOf(7), Integer.valueOf(0) }); a.invoke(watcher, new Object[] { Integer.valueOf(8), Byte.valueOf((byte) 0) }); a.invoke(watcher, new Object[] { Integer.valueOf(10), this.name }); a.invoke(watcher, new Object[] { Integer.valueOf(11), Byte.valueOf((byte) 1) }); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } return watcher; }
From source file:com.complexible.pinto.RDFMapper.java
private Object valueToObject(final Value theValue, final Model theGraph, final PropertyDescriptor theDescriptor) { if (theValue instanceof Literal) { final Literal aLit = (Literal) theValue; final IRI aDatatype = aLit.getDatatype() != null ? aLit.getDatatype() : null; if (aDatatype == null || XMLSchema.STRING.equals(aDatatype) || RDFS.LITERAL.equals(aDatatype)) { String aStr = aLit.getLabel(); if (theDescriptor != null && Character.TYPE.isAssignableFrom(theDescriptor.getPropertyType())) { if (aStr.length() == 1) { return aStr.charAt(0); } else { throw new RDFMappingException("Bean type is char, but value is a a string."); }/*from w ww. jav a2 s . c o m*/ } else { return aStr; } } else if (XMLSchema.BOOLEAN.equals(aDatatype)) { return Boolean.valueOf(aLit.getLabel()); } else if (INTEGER_TYPES.contains(aDatatype)) { return Integer.parseInt(aLit.getLabel()); } else if (LONG_TYPES.contains(aDatatype)) { return Long.parseLong(aLit.getLabel()); } else if (XMLSchema.DOUBLE.equals(aDatatype)) { return Double.valueOf(aLit.getLabel()); } else if (FLOAT_TYPES.contains(aDatatype)) { return Float.valueOf(aLit.getLabel()); } else if (SHORT_TYPES.contains(aDatatype)) { return Short.valueOf(aLit.getLabel()); } else if (BYTE_TYPES.contains(aDatatype)) { return Byte.valueOf(aLit.getLabel()); } else if (XMLSchema.ANYURI.equals(aDatatype)) { try { return new java.net.URI(aLit.getLabel()); } catch (URISyntaxException e) { LOGGER.warn("URI syntax exception converting literal value which is not a valid URI {} ", aLit.getLabel()); return null; } } else if (XMLSchema.DATE.equals(aDatatype) || XMLSchema.DATETIME.equals(aDatatype)) { return Dates2.asDate(aLit.getLabel()); } else if (XMLSchema.TIME.equals(aDatatype)) { return new Date(Long.parseLong(aLit.getLabel())); } else { throw new RuntimeException("Unsupported or unknown literal datatype: " + aLit); } } else if (theDescriptor != null && Enum.class.isAssignableFrom(theDescriptor.getPropertyType())) { IRI aURI = (IRI) theValue; Object[] aEnums = theDescriptor.getPropertyType().getEnumConstants(); for (Object aObj : aEnums) { if (((Enum) aObj).name().equals(aURI.getLocalName())) { return aObj; } } for (Field aField : theDescriptor.getPropertyType().getFields()) { Iri aAnnotation = aField.getAnnotation(Iri.class); if (aAnnotation != null && aURI.equals(iri(aAnnotation.value()))) { for (Object aObj : aEnums) { if (((Enum) aObj).name().equals(aField.getName())) { return aObj; } } // if the uri in the Iri annotation equals the value we're converting, but there was no field // match, something bad has happened throw new RDFMappingException("Expected enum value not found"); } } LOGGER.info("{} maps to the enum {}, but does not correspond to any of the values of the enum.", aURI, theDescriptor.getPropertyType()); return null; } else { Resource aResource = (Resource) theValue; final Class aClass = pinpointClass(theGraph, aResource, theDescriptor); RDFCodec aCodec = mCodecs.get(aClass); if (aCodec != null) { return aCodec.readValue(theGraph, aResource); } else { return readValue(theGraph, aClass, aResource); } } }