Example usage for java.lang Byte MIN_VALUE

List of usage examples for java.lang Byte MIN_VALUE

Introduction

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

Prototype

byte MIN_VALUE

To view the source code for java.lang Byte MIN_VALUE.

Click Source Link

Document

A constant holding the minimum value a byte can have, -27.

Usage

From source file:rascal.RandomTestDataUtils.java

public static byte[] createRandomData(int size) {
    byte[] data = new byte[size];
    for (int i = 0; i < data.length; i++) {
        data[i] = (byte) (RandomUtils.nextInt(Byte.MAX_VALUE * 2) + Byte.MIN_VALUE);
    }//w  ww.  j  a v  a  2 s .  c o  m
    return data;
}

From source file:BytesHelper.java

public static int toInt(byte[] bytes) {
    int result = 0;
    for (int i = 0; i < 4; i++) {
        result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
    }/*from   w w  w . j  a va 2  s  .c o m*/
    return result;
}

From source file: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  ww  w .j av  a  2s. co  m
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte
 * @see java.lang.Short
 * @see java.lang.Integer
 * @see java.lang.Long
 * @see java.math.BigInteger
 * @see java.lang.Float
 * @see java.lang.Double
 * @see java.math.BigDecimal
 */
public static Number convertNumberToTargetClass(Number number, Class targetClass)
        throws IllegalArgumentException {

    if (targetClass.isInstance(number)) {
        return number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.byteValue();
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.shortValue();
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.intValue();
    } else if (targetClass.equals(Long.class)) {
        return number.longValue();
    } else if (targetClass.equals(Float.class)) {
        return number.floatValue();
    } else if (targetClass.equals(Double.class)) {
        return number.doubleValue();
    } else if (targetClass.equals(BigInteger.class)) {
        return BigInteger.valueOf(number.longValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return 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:NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 * @param number the number to convert/*from   w w  w.j  a v a2  s . c o m*/
 * @param targetClass the target class to convert to
 * @return the converted number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte
 * @see java.lang.Short
 * @see java.lang.Integer
 * @see java.lang.Long
 * @see java.math.BigInteger
 * @see java.lang.Float
 * @see java.lang.Double
 * @see java.math.BigDecimal
 */
public static Number convertNumberToTargetClass(Number number, Class targetClass)
        throws IllegalArgumentException {

    if (targetClass.isInstance(number)) {
        return number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Byte(number.byteValue());
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Short(number.shortValue());
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Integer(number.intValue());
    } else if (targetClass.equals(Long.class)) {
        return new Long(number.longValue());
    } else if (targetClass.equals(Float.class)) {
        return new Float(number.floatValue());
    } else if (targetClass.equals(Double.class)) {
        return new Double(number.doubleValue());
    } else if (targetClass.equals(BigInteger.class)) {
        return BigInteger.valueOf(number.longValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return 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:StringUtils.java

/**
 * DOCUMENT ME!/*  ww w  .  j  ava  2s. co  m*/
 * 
 * @param buffer DOCUMENT ME!
 * @param startPos DOCUMENT ME!
 * @param length DOCUMENT ME!
 * @return DOCUMENT ME! 
 */
public static final String toAsciiString3(byte[] buffer, int startPos, int length) {

    char[] charArray = new char[length];
    int readpoint = startPos;

    for (int i = 0; i < length; i++) {
        charArray[i] = byteToChars[(int) buffer[readpoint] - Byte.MIN_VALUE];
        readpoint++;
    }

    return new String(charArray);
}

From source file:com.p5solutions.core.utils.NumberUtils.java

public static boolean isByte(String value) {
    if (isNatural(value)) {
        Long v = NumberUtils.valueOf(value.toString(), Long.class);
        if (v >= Byte.MIN_VALUE && v <= Byte.MAX_VALUE) {
            return true;
        }// w  w w.j a va2s  . c  om
    }
    return false;
}

From source file:io.github.benas.jpopulator.randomizers.validation.MaxValueRandomizer.java

/**
 * Generate a random value for the given type.
 *
 * @param type the type for which a random value will be generated
 * @param maxValue the maximum threshold for the generated value
 * @return a random value (lower than maxValue) for the given type or null if the type is not supported
 *///from   www .j  a va  2  s.c om
public static Object getRandomValue(final Class type, final long maxValue) {

    if (type.equals(Byte.TYPE) || type.equals(Byte.class)) {
        return (byte) randomDataGenerator.nextLong(Byte.MIN_VALUE, maxValue);
    }
    if (type.equals(Short.TYPE) || type.equals(Short.class)) {
        return (short) randomDataGenerator.nextLong(Short.MIN_VALUE, maxValue);
    }
    if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
        return (int) randomDataGenerator.nextLong(Integer.MIN_VALUE, maxValue);
    }
    if (type.equals(Long.TYPE) || type.equals(Long.class)) {
        return randomDataGenerator.nextLong(Long.MIN_VALUE, maxValue);
    }
    if (type.equals(BigInteger.class)) {
        return new BigInteger(String.valueOf(randomDataGenerator.nextLong(Long.MIN_VALUE, maxValue)));
    }
    if (type.equals(BigDecimal.class)) {
        return new BigDecimal(randomDataGenerator.nextLong(Long.MIN_VALUE, maxValue));
    }
    return null;
}

From source file:org.sakaiproject.content.impl.serialize.impl.test.ByteStorageConversionCheck.java

@Test
public void test256Conversion() {
    byte[] bin = new byte[256];
    char[] cin = new char[256];
    byte[] bout = new byte[256];

    {//from   w  w  w.j a v a2s .c om
        int i = 0;
        for (byte bx = Byte.MIN_VALUE; bx <= Byte.MAX_VALUE && i < bin.length; bx++) {
            bin[i++] = bx;
        }
    }

    ByteStorageConversion.toChar(bin, 0, cin, 0, bin.length);
    ByteStorageConversion.toByte(cin, 0, bout, 0, cin.length);

    StringBuilder sb = new StringBuilder();
    sb.append("\n");
    for (int i = 0; i < bin.length; i++) {
        sb.append("   Byte " + bin[i] + ": Stored as int[" + (int) cin[i] + "]  char[" + cin[i] + "]\n");
    }
    log.info(sb.toString());

    for (int i = 0; i < bin.length; i++) {
        Assert.assertEquals(
                "Internal Byte conversion failed at " + bin[i] + "=>" + (int) cin[i] + "=>" + bout[i], bin[i],
                bout[i]);
    }
    log.info("Internal Byte conversion test Passed Ok");

}

From source file:org.lightadmin.core.util.NumberUtils.java

@SuppressWarnings("unchecked")
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)
        throws IllegalArgumentException {
    Assert.notNull(number, "Number must not be null");
    Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return (T) number;
    } else if (targetClass.equals(byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }/*  w ww .  j a  v  a2s .  c o  m*/
        return (T) new Byte(number.byteValue());
    } else if (targetClass.equals(short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Short(number.shortValue());
    } else if (targetClass.equals(int.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Integer(number.intValue());
    } else if (targetClass.equals(long.class)) {
        return (T) new Long(number.longValue());
    } else if (targetClass.equals(float.class)) {
        return (T) Float.valueOf(number.toString());
    } else if (targetClass.equals(double.class)) {
        return (T) Double.valueOf(number.toString());
    } else {
        return org.springframework.util.NumberUtils.convertNumberToTargetClass(number, targetClass);
    }
}

From source file:org.joda.primitives.iterator.impl.TestArrayByteIterator.java

@Override
public Iterator<Byte> makeFullIterator() {
    byte[] data = new byte[] { new Byte((byte) 2), new Byte((byte) -2), new Byte((byte) 38), new Byte((byte) 0),
            new Byte((byte) 126), new Byte((byte) 202), new Byte(Byte.MIN_VALUE), new Byte(Byte.MAX_VALUE) };
    return new ArrayByteIterator(data);
}