Example usage for java.lang Byte Byte

List of usage examples for java.lang Byte Byte

Introduction

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

Prototype

@Deprecated(since = "9")
public Byte(String s) throws NumberFormatException 

Source Link

Document

Constructs a newly allocated Byte object that represents the byte value indicated by the String parameter.

Usage

From source file:org.crazydog.util.spring.NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 *
 * @param number      the number to convert
 * @param targetClass the target class to convert to
 * @return the converted number/*from  www .j  av a 2 s . co m*/
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see Byte
 * @see Short
 * @see Integer
 * @see Long
 * @see BigInteger
 * @see Float
 * @see Double
 * @see BigDecimal
 */
@SuppressWarnings("unchecked")
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)
        throws IllegalArgumentException {

    org.springframework.util.Assert.notNull(number, "Number must not be null");
    org.springframework.util.Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return (T) number;
    } else if (Byte.class == targetClass) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Byte(number.byteValue());
    } else if (Short.class == targetClass) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Short(number.shortValue());
    } else if (Integer.class == targetClass) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Integer(number.intValue());
    } else if (Long.class == targetClass) {
        BigInteger bigInt = null;
        if (number instanceof BigInteger) {
            bigInt = (BigInteger) number;
        } else if (number instanceof BigDecimal) {
            bigInt = ((BigDecimal) number).toBigInteger();
        }
        // Effectively analogous to JDK 8's BigInteger.longValueExact()
        if (bigInt != null && (bigInt.compareTo(LONG_MIN) < 0 || bigInt.compareTo(LONG_MAX) > 0)) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Long(number.longValue());
    } else if (BigInteger.class == targetClass) {
        if (number instanceof BigDecimal) {
            // do not lose precision - use BigDecimal's own conversion
            return (T) ((BigDecimal) number).toBigInteger();
        } else {
            // original value is not a Big* number - use standard long conversion
            return (T) BigInteger.valueOf(number.longValue());
        }
    } else if (Float.class == targetClass) {
        return (T) new Float(number.floatValue());
    } else if (Double.class == targetClass) {
        return (T) new Double(number.doubleValue());
    } else if (BigDecimal.class == targetClass) {
        // always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return (T) new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:adams.data.statistics.StatUtils.java

/**
 * Turns the byte array into a Byte array.
 *
 * @param array   the array to convert/* ww w  .ja  v a2 s.co  m*/
 * @return      the converted array
 */
public static Number[] toNumberArray(byte[] array) {
    Byte[] result;
    int i;

    result = new Byte[array.length];
    for (i = 0; i < array.length; i++)
        result[i] = new Byte(array[i]);

    return result;
}

From source file:org.joda.primitives.collection.impl.AbstractTestByteCollection.java

public Byte[] getOtherNonNullElements() {
    return new Byte[] { new Byte((byte) -33), new Byte((byte) 66), new Byte((byte) -99) };
}

From source file:org.paxle.dbus.impl.notification.NotificationManager.java

protected void activate(ComponentContext context)
        throws DBusException, InvalidSyntaxException, IllegalArgumentException, IOException {
    try {/*from ww  w.  j  a  va2s  .  c  o  m*/
        // connecting to dbus
        this.logger.info("Connecting to dbus ...");
        this.conn = DBusConnection.getConnection(DBusConnection.SESSION);

        // getting the network-manager via dbus
        this.logger.info(String.format("Getting reference to %s ...", BUSNAME));
        final Notifications n = conn.getRemoteObject(BUSNAME, OBJECTPATH, Notifications.class);

        ServerInfo<String, String, String, String> info = n.GetServerInformation();
        List<String> capabilities = n.GetCapabilities();

        HashMap<String, Variant> hints = new HashMap<String, Variant>();
        hints.put("urgency", new Variant<Byte>(new Byte((byte) 0)));
        hints.put("category", new Variant<String>(""));
        hints.put("icon_data", new Variant<byte[]>(this.getIconData()));
        hints.put("image_data", new Variant<byte[]>(this.getIconData()));

        UInt32 id = n.Notify("HelloWorld", new UInt32(0), "", "Info", "Paxle started.", new String[0], hints,
                new Integer(-1));
        System.out.println(id);
    } catch (DBusExecutionException e) {
        if (e instanceof NoReply) {
            this.logger.error(String.format("'%s' did not reply within specified time.", BUSNAME));
        } else {
            this.logger.warn(String.format("Unexpected '%s' while trying to connect to '%s'.",
                    e.getClass().getName(), BUSNAME), e);
        }

        // disabling this component
        context.disableComponent(this.getClass().getName());
        // XXX: should we re-throw the exceptoin here? 
        // throw e;
    }
}

From source file:com.appleframework.jmx.core.util.CoreUtils.java

public static Number valueOf(String value, String dataType) {
    if (dataType.equals("java.lang.Integer") || dataType.equals("int")) {
        return new Integer(value);
    }//from w w  w  . j  a  v  a 2  s .c o  m
    if (dataType.equals("java.lang.Double") || dataType.equals("double")) {
        return new Double(value);
    }
    if (dataType.equals("java.lang.Long") || dataType.equals("long")) {
        return new Long(value);
    }
    if (dataType.equals("java.lang.Float") || dataType.equals("float")) {
        return new Double(value);
    }
    if (dataType.equals("java.lang.Short") || dataType.equals("short")) {
        return new Short(value);
    }
    if (dataType.equals("java.lang.Byte") || dataType.equals("byte")) {
        return new Byte(value);
    }
    if (dataType.equals("java.math.BigInteger")) {
        return new BigInteger(value);
    }
    if (dataType.equals("java.math.BigDecimal")) {
        return new BigDecimal(value);
    }
    return null;
}

From source file:org.jboss.dashboard.factory.PropertyAddProcessingInstruction.java

public Object getValueAfterChange(Object originalValue, Class expectedClass) throws Exception {
    if (log.isDebugEnabled())
        log.debug("Processing instruction " + this + " on value " + originalValue + " (" + expectedClass + ")");
    Object valueToAdd = null;/*from  w w w. j av a 2  s .  c  om*/
    Object currentValue = originalValue;
    currentValue = currentValue == null ? getNewInstanceForClass(expectedClass) : currentValue;
    valueToAdd = getValueForParameter(getPropertyValue(), expectedClass);
    if (valueToAdd != null) {
        if (expectedClass.isArray()) {
            Object newValue = getBaseArray(expectedClass.getComponentType(),
                    ((Object[]) currentValue).length + ((Object[]) valueToAdd).length);
            System.arraycopy(currentValue, 0, newValue, 0, ((Object[]) currentValue).length);
            System.arraycopy(valueToAdd, 0, newValue, ((Object[]) currentValue).length,
                    ((Object[]) valueToAdd).length);
            return newValue;
        } else if (String.class.equals(expectedClass)) {
            return (String) currentValue + (String) valueToAdd;
        } else if (expectedClass.equals(int.class)) {
            return new Integer(((Integer) currentValue).intValue() + ((Integer) valueToAdd).intValue());
        } else if (expectedClass.equals(long.class)) {
            return new Long(((Long) currentValue).longValue() + ((Long) valueToAdd).longValue());
        } else if (expectedClass.equals(double.class)) {
            return new Double(((Double) currentValue).doubleValue() + ((Double) valueToAdd).doubleValue());
        } else if (expectedClass.equals(float.class)) {
            return new Float(((Float) currentValue).floatValue() + ((Float) valueToAdd).floatValue());
        } else if (expectedClass.equals(byte.class)) {
            return new Byte((byte) (((Byte) currentValue).byteValue() + ((Byte) valueToAdd).byteValue()));
        } else if (expectedClass.equals(short.class)) {
            return new Short((short) (((Short) currentValue).shortValue() + ((Short) valueToAdd).shortValue()));
        } else if (expectedClass.equals(Integer.class)) {
            return new Integer(((Integer) currentValue).intValue() + ((Integer) valueToAdd).intValue());
        } else if (expectedClass.equals(Long.class)) {
            return new Long(((Long) currentValue).longValue() + ((Long) valueToAdd).longValue());
        } else if (expectedClass.equals(Double.class)) {
            return new Double(((Double) currentValue).doubleValue() + ((Double) valueToAdd).doubleValue());
        } else if (expectedClass.equals(Float.class)) {
            return new Float(((Float) currentValue).floatValue() + ((Float) valueToAdd).floatValue());
        } else if (expectedClass.equals(Byte.class)) {
            return new Byte((byte) (((Byte) currentValue).byteValue() + ((Byte) valueToAdd).byteValue()));
        } else if (expectedClass.equals(Short.class)) {
            return new Short((short) (((Short) currentValue).shortValue() + ((Short) valueToAdd).shortValue()));
        } else {
            log.error("Addition not supported for class " + expectedClass + ". Ignoring property "
                    + getPropertyName() + "+=" + getPropertyValue());
        }
    }
    return originalValue;
}

From source file:com.judoscript.jamaica.MyUtils.java

public static Object number2object(long val, String typeHint) {
    if (typeHint != null) {
        if (typeHint.equals("int"))
            return new Integer((int) val);
        if (typeHint.equals("long"))
            return new Long(val);
        if (typeHint.equals("short"))
            return new Short((short) val);
        if (typeHint.equals("char"))
            return new Character((char) val);
        if (typeHint.equals("byte"))
            return new Byte((byte) val);
        if (typeHint.equals("double"))
            return new Double(val);
        if (typeHint.equals("float"))
            return new Float(val);
    }/*from   w w w .j  a  va 2s  .co  m*/
    return new Long(val);
}

From source file:de.unibayreuth.bayeos.goat.table.MassenTableModel.java

public Object getValueAt(int aRow, int aColumn) {
    switch (aColumn) {
    case 0://from  w w w . j  a va  2 s  . co m
        return new java.util.Date(vonList.get(aRow) * 1000L);
    case 1:
        return new Double(wertList.get(aRow));
    case 2:
        return new Byte(statusList.get(aRow));
    default:
        return null;
    }
}

From source file:com.couchbase.sqoop.mapreduce.db.CouchbaseRecordReadSerializeTest.java

@Before
@Override/*  ww w  . ja va 2s . c  o  m*/
public void setUp() throws Exception {
    super.setUp();

    tappedStuff = new HashMap<String, ResponseMessage>();

    URI uri = new URI(CouchbaseUtils.CONNECT_STRING);
    String user = CouchbaseUtils.COUCHBASE_USER_NAME;
    String pass = CouchbaseUtils.COUCHBASE_USER_PASS;

    try {
        cb = new CouchbaseClient(Arrays.asList(uri), user, pass);
    } catch (IOException e) {
        LOG.error("Couldn't connect to server" + e.getMessage());
        fail(e.toString());
    }
    this.client = new TapClient(Arrays.asList(uri), user, pass);

    cb.flush();
    Thread.sleep(500);

    // set up the items we're going to deserialize
    Integer anint = new Integer(Integer.MIN_VALUE);
    cb.set(anint.toString(), 0x300, anint).get();

    Long along = new Long(Long.MAX_VALUE);
    cb.set(along.toString(), 0, along).get();

    Float afloat = new Float(Float.MAX_VALUE);
    cb.set(afloat.toString(), 0, afloat).get();

    Double doubleBase = new Double(Double.NEGATIVE_INFINITY);
    cb.set(doubleBase.toString(), 0, doubleBase).get();

    Boolean booleanBase = true;
    cb.set(booleanBase.toString(), 0, booleanBase).get();

    rightnow = new Date(); // instance, needed later
    dateText = rightnow.toString().replaceAll(" ", "_");
    cb.set(dateText, 0, rightnow).get();

    Byte byteMeSix = new Byte("6");
    cb.set(byteMeSix.toString(), 0, byteMeSix).get();

    String ourString = "hi,there";
    cb.set(ourString.toString(), 0, ourString).get();

    client.tapDump("tester");
    while (client.hasMoreMessages()) {
        ResponseMessage m = client.getNextMessage();
        if (m == null) {
            continue;
        }
        tappedStuff.put(m.getKey(), m);
    }
}

From source file:org.floggy.synchronization.jme.core.impl.JSONSerializationManagerTest.java

/**
* DOCUMENT ME!//  w ww  .  j a v a2s. c o m
*
* @throws Exception DOCUMENT ME!
*/
public void testReceiveByteNotNull() throws Exception {
    JSONObject jsonObject = new JSONObject();
    String name = "firstName";
    Byte value = new Byte((byte) 12);

    jsonObject.put(name, value);

    Byte actual = JSONSerializationManager.receiveByte(name, jsonObject);

    assertEquals(value, actual);
}