Example usage for java.lang Byte valueOf

List of usage examples for java.lang Byte valueOf

Introduction

In this page you can find the example usage for java.lang Byte valueOf.

Prototype

public static Byte valueOf(String s) throws NumberFormatException 

Source Link

Document

Returns a Byte object holding the value given by the specified String .

Usage

From source file:com.datatorrent.lib.util.PojoUtilsTest.java

@Test
@SuppressWarnings({ "unchecked", "UnnecessaryBoxing" })
public void testSetterOrFieldExpression() {
    TestPojo testPojo = new TestPojo(0);
    final Class<?> testPojoClass = testPojo.getClass();

    SetterInt<Object> setterInt = createSetterInt(testPojoClass, TestPojo.INT_FIELD_NAME);
    setterInt.set(testPojo, 1);//from w w  w  .  ja  v a 2 s . c o  m
    assertEquals(1, testPojo.intField);

    setterInt = createSetterInt(testPojoClass, TestPojo.INT_METHOD_NAME);
    setterInt.set(testPojo, 2);
    assertEquals(2, testPojo.getIntVal());

    setterInt = (SetterInt<Object>) constructSetter(testPojoClass, TestPojo.INT_FIELD_NAME, int.class);
    setterInt.set(testPojo, 3);
    assertEquals(3, testPojo.intField);

    setterInt = (SetterInt<Object>) constructSetter(testPojoClass, TestPojo.INT_METHOD_NAME, int.class);
    setterInt.set(testPojo, 4);
    assertEquals(4, testPojo.getIntVal());

    Setter<Object, Integer> setterInteger = createSetter(testPojoClass, TestPojo.INT_FIELD_NAME, Integer.class);
    setterInteger.set(testPojo, Integer.valueOf(5));
    assertEquals(5, testPojo.intField);

    setterInteger = (Setter<Object, Integer>) constructSetter(testPojoClass, TestPojo.INT_FIELD_NAME,
            Integer.class);
    setterInteger.set(testPojo, 6);
    assertEquals(6, testPojo.intField);

    setterInteger = createSetter(testPojoClass, TestPojo.INT_METHOD_NAME, Integer.class);
    setterInteger.set(testPojo, 7);
    assertEquals(7, testPojo.getIntVal());

    setterInteger = (Setter<Object, Integer>) constructSetter(testPojoClass, TestPojo.INT_METHOD_NAME,
            Integer.class);
    setterInteger.set(testPojo, 8);
    assertEquals(8, testPojo.getIntVal());

    SetterByte<Object> setterByte = createSetterByte(testPojoClass, TestPojo.INT_FIELD_NAME);
    setterByte.set(testPojo, (byte) 9);
    assertEquals(9, testPojo.intField);

    setterByte = (SetterByte<Object>) constructSetter(testPojoClass, TestPojo.INT_FIELD_NAME, byte.class);
    setterByte.set(testPojo, (byte) 10);
    assertEquals(10, testPojo.intField);

    setterByte = createSetterByte(testPojoClass, TestPojo.INT_METHOD_NAME);
    setterByte.set(testPojo, (byte) 11);
    assertEquals(11, testPojo.getIntVal());

    setterByte = ((SetterByte<Object>) constructSetter(testPojoClass, TestPojo.INT_METHOD_NAME, byte.class));
    setterByte.set(testPojo, (byte) 12);
    assertEquals(12, testPojo.getIntVal());

    createSetter(testPojoClass, TestPojo.INT_FIELD_NAME, Byte.class).set(testPojo, Byte.valueOf((byte) 13));
    assertEquals(13, testPojo.intField);

    ((Setter<Object, Byte>) constructSetter(testPojoClass, TestPojo.INT_FIELD_NAME, Byte.class)).set(testPojo,
            Byte.valueOf((byte) 14));
    assertEquals(14, testPojo.getIntVal());

    createSetter(testPojoClass, TestPojo.INT_METHOD_NAME, Byte.class).set(testPojo, Byte.valueOf((byte) 15));
    assertEquals(15, testPojo.getIntVal());

    ((Setter<Object, Byte>) constructSetter(testPojoClass, TestPojo.INT_METHOD_NAME, Byte.class)).set(testPojo,
            Byte.valueOf((byte) 16));
    assertEquals(16, testPojo.getIntVal());

    SetterShort<Object> setterShort = createSetterShort(testPojoClass, TestPojo.INT_FIELD_NAME);
    setterShort.set(testPojo, (short) 17);
    assertEquals(17, testPojo.intField);

    setterShort = createSetterShort(testPojoClass, TestPojo.INT_METHOD_NAME);
    setterShort.set(testPojo, (short) 18);
    assertEquals(18, testPojo.getIntVal());

    try {
        @SuppressWarnings("unused")
        SetterLong<Object> setterLong = createSetterLong(testPojoClass, TestPojo.INT_FIELD_NAME);
        fail("long can't be assigned to the int field");
    } catch (Exception ignored) {
        //ignored
    }

}

From source file:ddf.common.test.ServiceManager.java

private Object getAttributeValue(String value, int type) {
    switch (type) {
    case AttributeDefinition.BOOLEAN:
        return Boolean.valueOf(value);
    case AttributeDefinition.BYTE:
        return Byte.valueOf(value);
    case AttributeDefinition.DOUBLE:
        return Double.valueOf(value);
    case AttributeDefinition.CHARACTER:
        return value.toCharArray()[0];
    case AttributeDefinition.FLOAT:
        return Float.valueOf(value);
    case AttributeDefinition.INTEGER:
        return Integer.valueOf(value);
    case AttributeDefinition.LONG:
        return Long.valueOf(value);
    case AttributeDefinition.SHORT:
        return Short.valueOf(value);
    case AttributeDefinition.PASSWORD:
    case AttributeDefinition.STRING:
    default:/*  w w w. ja v a2 s .  co  m*/
        return value;
    }
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

private Object fromSpecialType(DBObject value) {
    String type = (String) value.get(Constants.TYPE);
    String val = (String) value.get(Constants.VALUE);

    if (BigInteger.class.getName().equals(type))
        return new BigInteger(val);
    else if (BigDecimal.class.getName().equals(type))
        return new BigDecimal(val);
    else if (Byte.class.getName().equals(type))
        return Byte.valueOf(val);
    else if (Float.class.getName().equals(type))
        return Float.valueOf(val);
    else if (Character.class.getName().equals(type))
        return toCharacter(val);
    else if (Class.class.getName().equals(type))
        return toClass(val);
    else if (Locale.class.getName().equals(type))
        return toLocale(val);
    else if (URI.class.getName().equals(type))
        return URI.create(val);
    else if (Timestamp.class.getName().equals(type))
        return Timestamp.valueOf(val);

    throw new IllegalArgumentException("unkown value: " + value);
}

From source file:com.sun.faces.el.impl.Coercions.java

/**
 * Coerces a String to the given primitive number class
 *///from   www  . j a  v a2  s  .com
static Number coerceToPrimitiveNumber(String pValue, Class pClass) throws ElException {
    if (pClass == Byte.class || pClass == Byte.TYPE) {
        return Byte.valueOf(pValue);
    } else if (pClass == Short.class || pClass == Short.TYPE) {
        return Short.valueOf(pValue);
    } else if (pClass == Integer.class || pClass == Integer.TYPE) {
        return Integer.valueOf(pValue);
    } else if (pClass == Long.class || pClass == Long.TYPE) {
        return Long.valueOf(pValue);
    } else if (pClass == Float.class || pClass == Float.TYPE) {
        return Float.valueOf(pValue);
    } else if (pClass == Double.class || pClass == Double.TYPE) {
        return Double.valueOf(pValue);
    } else if (pClass == BigInteger.class) {
        return new BigInteger(pValue);
    } else if (pClass == BigDecimal.class) {
        return new BigDecimal(pValue);
    } else {
        return PrimitiveObjects.getInteger(0);
    }
}

From source file:de.blizzy.backup.backup.BackupRun.java

private int backupFileContents(final IFile file, final File backupFile, String backupFilePath)
        throws IOException {

    FileUtils.forceMkdir(backupFile.getParentFile());

    final MessageDigest[] digest = new MessageDigest[1];
    IOutputStreamProvider outputStreamProvider = new IOutputStreamProvider() {
        @Override// w w  w. java 2  s .  c o  m
        public OutputStream getOutputStream() throws IOException {
            try {
                digest[0] = MessageDigest.getInstance("SHA-256"); //$NON-NLS-1$
                OutputStream fileOut = new BufferedOutputStream(new FileOutputStream(backupFile));
                OutputStream interceptOut = fileOut;
                for (IStorageInterceptor interceptor : storageInterceptors) {
                    interceptOut = interceptor.interceptOutputStream(interceptOut, file.getLength());
                }
                OutputStream compressOut = Compression.BZIP2.getOutputStream(interceptOut);
                OutputStream digestOut = new DigestOutputStream(compressOut, digest[0]);
                return digestOut;
            } catch (GeneralSecurityException e) {
                throw new RuntimeException(e);
            }
        }
    };
    boolean fileCopied = false;
    try {
        file.copy(outputStreamProvider);
        fileCopied = true;
    } finally {
        if (!fileCopied) {
            try {
                Files.delete(backupFile.toPath());
            } catch (IOException e) {
                BackupPlugin.getDefault().logError("error while deleting file: " + //$NON-NLS-1$
                        backupFile.getAbsolutePath(), e);
                fireBackupErrorOccurred(e, BackupErrorEvent.Severity.WARNING);
            }
            removeFoldersIfEmpty(backupFile.getParentFile());
        }
    }

    String checksum = toHexString(digest[0]);

    database.factory().insertInto(Tables.FILES).set(Tables.FILES.BACKUP_PATH, backupFilePath)
            .set(Tables.FILES.CHECKSUM, checksum).set(Tables.FILES.LENGTH, Long.valueOf(file.getLength()))
            .set(Tables.FILES.COMPRESSION, Byte.valueOf((byte) Compression.BZIP2.getValue())).execute();
    return database.factory().lastID().intValue();
}

From source file:org.apache.struts2.jasper.compiler.JspUtil.java

public static String coerceToPrimitiveByte(String s, boolean isNamedAttribute) {
    if (isNamedAttribute) {
        return "org.apache.struts2.jasper.runtime.JspRuntimeLibrary.coerceToByte(" + s + ")";
    } else {/* w w  w .  j av  a  2 s. c  o  m*/
        if (s == null || s.length() == 0)
            return "(byte) 0";
        else
            return "((byte)" + Byte.valueOf(s).toString() + ")";
    }
}

From source file:org.op4j.functions.FnObject.java

/**
 * <p>// www .ja va 2  s.com
 * Determines whether the target object is less than the specified object
 * in value, this is, whether <tt>target.compareTo(object) &lt; 0</tt>. Both
 * the target and the specified object have to implement {@link Comparable}.
 * </p>
 * 
 * @param object the object to compare to the target
 * @return true if target is less than the specified object, false if not
 */
public static final Function<Object, Boolean> lessThan(final byte object) {
    return new LessThan(Byte.valueOf(object));
}

From source file:org.apache.struts2.jasper.compiler.JspUtil.java

public static String coerceToByte(String s, boolean isNamedAttribute) {
    if (isNamedAttribute) {
        return "(Byte) org.apache.struts2.jasper.runtime.JspRuntimeLibrary.coerce(" + s + ", Byte.class)";
    } else {//from  w  w w  .j av a  2  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 getTeleportPacket(Location loc) {
    Class<?> PacketPlayOutEntityTeleport = Util.getCraftClass("PacketPlayOutEntityTeleport");

    Object packet = null;// w ww .  j a v a2  s. com
    try {
        packet = PacketPlayOutEntityTeleport
                .getConstructor(new Class[] { Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Byte.TYPE,
                        Byte.TYPE, Boolean.TYPE })
                .newInstance(new Object[] { Integer.valueOf(this.id), Integer.valueOf(loc.getBlockX() * 32),
                        Integer.valueOf(loc.getBlockY() * 32), Integer.valueOf(loc.getBlockZ() * 32),
                        Byte.valueOf((byte) ((int) loc.getYaw() * 256 / 360)),
                        Byte.valueOf((byte) ((int) loc.getPitch() * 256 / 360)), Boolean.valueOf(false) });
    } 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 packet;
}

From source file:org.apache.hadoop.hbase.io.HbaseObjectWritable.java

/**
 * Read a {@link Writable}, {@link String}, primitive type, or an array of
 * the preceding.//  www . ja v a2 s  .com
 * @param in
 * @param objectWritable
 * @param conf
 * @return the object
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static Object readObject(DataInput in, HbaseObjectWritable 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 if (declaredClass.equals(Result[].class)) {
            instance = Result.readArray(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 { // 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;
}