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:org.mule.util.ObjectUtils.java

/**
 * Gets a byte from a value in a null-safe manner.
 * <p/>/*ww w  . ja  v  a 2  s.co m*/
 *
 * @param answer       the object value
 * @param defaultValue the default to use if null or of incorrect type
 * @return the value as a byte, or the defaultValue
 */
public static byte getByte(final Object answer, Byte defaultValue) {
    if (answer == null) {
        return defaultValue;
    } else if (answer instanceof Number) {
        return ((Number) answer).byteValue();
    } else if (answer instanceof String) {
        try {
            return Byte.valueOf((String) answer);
        } catch (NumberFormatException e) {
            //handled below
        }
    }
    if (logger.isWarnEnabled()) {
        logger.warn("Value exists but cannot be converted to byte: " + answer + ", returning default value: "
                + defaultValue);
    }
    return defaultValue;
}

From source file:org.red5.io.mock.Output.java

/** {@inheritDoc} */
public void writeReference(Object obj) {
    list.add(Byte.valueOf(DataTypes.OPT_REFERENCE));
    list.add(Short.valueOf(getReferenceId(obj)));
}

From source file:org.apache.beam.sdk.extensions.sql.impl.schema.BeamTableUtils.java

public static Object autoCastField(int fieldType, Object rawObj) {
    if (rawObj == null) {
        return null;
    }/*from  w  ww.  j a  va  2  s  . c om*/

    SqlTypeName columnType = CalciteUtils.toCalciteType(fieldType);
    // auto-casting for numberics
    if ((rawObj instanceof String && SqlTypeName.NUMERIC_TYPES.contains(columnType))
            || (rawObj instanceof BigDecimal && columnType != SqlTypeName.DECIMAL)) {
        String raw = rawObj.toString();
        switch (columnType) {
        case TINYINT:
            return Byte.valueOf(raw);
        case SMALLINT:
            return Short.valueOf(raw);
        case INTEGER:
            return Integer.valueOf(raw);
        case BIGINT:
            return Long.valueOf(raw);
        case FLOAT:
            return Float.valueOf(raw);
        case DOUBLE:
            return Double.valueOf(raw);
        default:
            throw new UnsupportedOperationException(
                    String.format("Column type %s is not supported yet!", columnType));
        }
    } else if (SqlTypeName.CHAR_TYPES.contains(columnType)) {
        // convert NlsString to String
        if (rawObj instanceof NlsString) {
            return ((NlsString) rawObj).getValue();
        } else {
            return rawObj;
        }
    } else {
        return rawObj;
    }
}

From source file:org.wings.template.DefaultPropertyValueConverter.java

/**
 * Describe <code>convertPropertyValue</code> method here.
 *
 * @param value       an <code>Object</code> value
 * @param targetClass a <code>Class</code> value
 * @return <description>//from   www  . ja va2 s  .c om
 * @throws UnsupportedOperationException if an error occurs
 * @throws java.lang.UnsupportedOperationException
 *                                       <description>
 */
public Object convertPropertyValue(String value, Class targetClass) throws UnsupportedOperationException {
    if (value == null || "null".equals(value)) {
        return null;
    } // end of if ()

    if (targetClass == String.class) {
        return value;
    } // end of if ()

    if (targetClass == Boolean.TYPE || targetClass == Boolean.class) {
        return Boolean.valueOf(value);
    }

    if (targetClass == Integer.TYPE || targetClass == Integer.class) {
        return Integer.valueOf(value);
    }
    if (targetClass == Long.TYPE || targetClass == Long.class) {
        return Long.valueOf(value);
    }
    if (targetClass == Short.TYPE || targetClass == Short.class) {
        return Short.valueOf(value);
    }
    if (targetClass == Byte.TYPE || targetClass == Byte.class) {
        return Byte.valueOf(value);
    }
    if (targetClass == Float.TYPE || targetClass == Float.class) {
        return Float.valueOf(value);
    }
    if (targetClass == Double.TYPE || targetClass == Double.class) {
        return Double.valueOf(value);
    }
    if (targetClass == Character.TYPE || targetClass == Character.class) {
        return new Character(value.charAt(0));
    }

    if (targetClass == StringBuffer.class) {
        return new StringBuffer(value);
    } // end of if ()

    if (SIcon.class.isAssignableFrom(targetClass)) {
        return ResourceFactory.makeIcon(value);
    }

    if (targetClass == Color.class) {
        return ResourceFactory.makeColor(value);
    }

    if (targetClass == SDimension.class) {
        return ResourceFactory.makeDimension(value);
    }

    if (targetClass == SFont.class) {
        return TemplateUtil.parseFont(value);
    }

    if (Resource.class.isAssignableFrom(targetClass)) {
        return new ClassPathResource(value);
    }

    if (CSSAttributeSet.class.isAssignableFrom(targetClass)) {
        return ResourceFactory.makeAttributeSet(value);
    }

    if (StyleSheet.class.isAssignableFrom(targetClass)) {
        StyleSheet result;
        try {
            CSSStyleSheet styleSheet = new CSSStyleSheet();
            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(value);
            styleSheet.read(in);
            in.close();
            result = styleSheet;
        } catch (Exception e) {
            log.warn("Exception", e);
            result = null;
        }
        return result;
    }

    if (ComponentCG.class.isAssignableFrom(targetClass)) {
        return ResourceFactory.makeComponentCG(value);
    }

    throw new UnsupportedOperationException("cannot create object of type " + targetClass.getName());
}

From source file:org.brekka.stillingar.example.FieldTypesDOMTest.java

/**
 * //from  w w w .  j a  va 2 s.  c o m
 */
private void verify() throws Exception {
    ConfiguredFieldTypes t = configuredFieldTypes;
    assertEquals(new URI(testing.getAnyURI()), t.getUri());
    assertEquals(testing.getBoolean(), t.isBooleanPrimitive());
    assertEquals(Byte.valueOf(testing.getByte()), t.getByteValue());
    assertEquals(testing.getByte(), t.getBytePrimitive());
    assertEquals(testing.getDate().getTime(), t.getDateAsCalendar().getTime());
    assertEquals(testing.getDate().getTime(), t.getDateAsDate());
    assertEquals(testing.getDateTime().getTime(), t.getDateTimeAsCalendar().getTime());
    assertEquals(testing.getDateTime().getTime(), t.getDateTimeAsDate());
    assertEquals(testing.getDecimal(), t.getDecimal());
    assertEquals(Double.valueOf(testing.getDouble()), t.getDoubleValue());
    assertEquals(testing.getDouble(), t.getDoublePrimitive(), 1d);
    assertEquals(testing.getFloat(), t.getFloatPrimitive(), 1f);
    assertEquals(Float.valueOf(testing.getFloat()), t.getFloatValue());
    assertEquals(testing.getInt(), t.getIntPrimitive());
    assertEquals(Integer.valueOf(testing.getInt()), t.getIntValue());
    assertEquals(testing.getLanguage(), t.getLanguage().toString());
    assertEquals(testing.getLong(), t.getLongPrimitive());
    assertEquals(Long.valueOf(testing.getLong()), t.getLongValue());
    assertEquals(testing.getShort(), t.getShortPrimitive());
    assertEquals(Short.valueOf(testing.getShort()), t.getShortValue());
    assertEquals(testing.getString(), t.getString());

    assertEquals(testing.getPeriod().toString(), t.getPeriod().toString());
    assertEquals(new DateTime(testing.getDateTime()).toString(), t.getDateTime().toString());
    assertEquals(new LocalDate(testing.getDate()).toString(), t.getLocalDate().toString());
    assertEquals(new LocalTime(testing.getTime()).toString(), t.getLocalTime().toString());

    assertEquals(String.format("%tT", testing.getTime()),
            String.format("%tT", t.getTimeAsCalendar().getTime()));
    assertTrue(Arrays.equals(t.getBinary(), testing.getBinary()));
    assertEquals(UUID.fromString(testing.getUUID()), t.getUuid());
    assertNotNull(t.getTestingElement());
    assertNotNull(t.getRoot());
}

From source file:org.brekka.stillingar.example.FieldTypesJAXBTest.java

/**
 * //w  w w . ja  va2 s .c  om
 */
private void verify() throws Exception {
    ConfiguredFieldTypes t = configuredFieldTypes;
    assertEquals(new URI(testing.getAnyURI()), t.getUri());
    assertEquals(testing.getBoolean(), t.isBooleanPrimitive());
    assertEquals(Byte.valueOf(testing.getByte()), t.getByteValue());
    assertEquals(testing.getByte(), t.getBytePrimitive());
    assertEquals(testing.getDate().getTime(), t.getDateAsCalendar().getTime());
    assertEquals(testing.getDate().getTime(), t.getDateAsDate());
    assertEquals(testing.getDateTime().getTime(), t.getDateTimeAsCalendar().getTime());
    assertEquals(testing.getDateTime().getTime(), t.getDateTimeAsDate());
    assertEquals(testing.getDecimal(), t.getDecimal());
    assertEquals(Double.valueOf(testing.getDouble()), t.getDoubleValue());
    assertEquals(testing.getDouble(), t.getDoublePrimitive(), 1d);
    assertEquals(testing.getFloat(), t.getFloatPrimitive(), 1f);
    assertEquals(Float.valueOf(testing.getFloat()), t.getFloatValue());
    assertEquals(testing.getInt(), t.getIntPrimitive());
    assertEquals(Integer.valueOf(testing.getInt()), t.getIntValue());
    assertEquals(testing.getLanguage(), t.getLanguage().toString());
    assertEquals(testing.getLong(), t.getLongPrimitive());
    assertEquals(Long.valueOf(testing.getLong()), t.getLongValue());
    assertEquals(testing.getShort(), t.getShortPrimitive());
    assertEquals(Short.valueOf(testing.getShort()), t.getShortValue());
    assertEquals(testing.getString(), t.getString());

    assertEquals(testing.getPeriod().toString(), t.getPeriod().toString());
    assertEquals(new DateTime(testing.getDateTime()).toString(), t.getDateTime().toString());
    assertEquals(new LocalDate(testing.getDate()).toString(), t.getLocalDate().toString());
    assertEquals(new LocalTime(testing.getTime()).toString(), t.getLocalTime().toString());

    assertEquals(String.format("%tT", testing.getTime()),
            String.format("%tT", t.getTimeAsCalendar().getTime()));
    assertTrue(Arrays.equals(t.getBinary(), testing.getBinary()));
    assertEquals(UUID.fromString(testing.getUUID()), t.getUuid());
    assertNotNull(t.getTestingObject() instanceof org.brekka.stillingar.example.Configuration.Testing);
    assertNotNull(t.getTestingElement());
    assertNotNull(t.getRoot());
}

From source file:org.brekka.stillingar.example.FieldTypesXmlBeansTest.java

/**
 * /*from   ww w. j a v a 2s  .c o m*/
 */
private void verify() throws Exception {
    ConfiguredFieldTypes t = configuredFieldTypes;
    assertEquals(new URI(testing.getAnyURI()), t.getUri());
    assertEquals(testing.getBoolean(), t.isBooleanPrimitive());
    assertEquals(Byte.valueOf(testing.getByte()), t.getByteValue());
    assertEquals(testing.getByte(), t.getBytePrimitive());
    assertEquals(testing.getDate(), t.getDateAsCalendar());
    assertEquals(testing.getDate().getTime(), t.getDateAsDate());
    assertEquals(testing.getDateTime(), t.getDateTimeAsCalendar());
    assertEquals(testing.getDateTime().getTime(), t.getDateTimeAsDate());
    assertEquals(testing.getDecimal(), t.getDecimal());
    assertEquals(Double.valueOf(testing.getDouble()), t.getDoubleValue());
    assertEquals(testing.getDouble(), t.getDoublePrimitive(), 1d);
    assertEquals(testing.getFloat(), t.getFloatPrimitive(), 1f);
    assertEquals(Float.valueOf(testing.getFloat()), t.getFloatValue());
    assertEquals(testing.getInt(), t.getIntPrimitive());
    assertEquals(Integer.valueOf(testing.getInt()), t.getIntValue());
    assertEquals(testing.getLanguage(), t.getLanguage().toString());
    assertEquals(testing.getLong(), t.getLongPrimitive());
    assertEquals(Long.valueOf(testing.getLong()), t.getLongValue());
    assertEquals(testing.getShort(), t.getShortPrimitive());
    assertEquals(Short.valueOf(testing.getShort()), t.getShortValue());
    assertEquals(testing.getString(), t.getString());
    assertEquals(testing.getTime(), t.getTimeAsCalendar());

    assertEquals(testing.getPeriod().toString(), t.getPeriod().toString());
    assertEquals(new DateTime(testing.getDateTime(), ISOChronology.getInstance()), t.getDateTime());
    assertEquals(new LocalDate(testing.getDate(), ISOChronology.getInstance()), t.getLocalDate());
    assertEquals(new LocalTime(testing.getTime(), ISOChronology.getInstance()), t.getLocalTime());

    assertTrue(Arrays.equals(t.getBinary(), testing.getBinary()));
    assertEquals(UUID.fromString(testing.getUUID()), t.getUuid());
    assertTrue(t.getTestingObject() instanceof Testing);
    assertNotNull(t.getTestingElement());
    assertNotNull(t.getRoot());
}

From source file:tv.acfun.video.player.resolver.SinaResolver.java

private static String getKey(String str) {
    try {/*w ww  .  ja  v a 2  s . c o  m*/
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(str.getBytes());
        byte[] bs = digest.digest();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < bs.length; i++) {
            Byte b = Byte.valueOf(bs[i]);
            builder.append(String.format("%02x", b));
        }
        return builder.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:org.red5.io.mock.Output.java

/** {@inheritDoc} */
public void writeString(String string) {
    list.add(Byte.valueOf(DataTypes.CORE_STRING));
    list.add(string);
}

From source file:uk.co.infinityapps.rehpic.key.generator.HashChainingBitShiftingKeyGenerator.java

public ShiftDirection getShiftDirection(final byte[] bytes) {
    byte lastByte = bytes[bytes.length - 1];
    if (Byte.valueOf(lastByte).intValue() > 0) {
        return ShiftDirection.LEFT;
    } else {/*from   www.ja  v a 2 s  .com*/
        return ShiftDirection.RIGHT;
    }
}