Example usage for java.math BigDecimal byteValue

List of usage examples for java.math BigDecimal byteValue

Introduction

In this page you can find the example usage for java.math BigDecimal byteValue.

Prototype

public byte byteValue() 

Source Link

Document

Returns the value of the specified number as a byte .

Usage

From source file:com.willetinc.hadoop.mapreduce.dynamodb.BinarySplitter.java

/**
 * Return the string encoded in a BigDecimal. Repeatedly multiply the input
 * value by 16; the integer portion after such a multiplication represents a
 * single character in base 16. Convert that back into a char and create a
 * string out of these until we have no data left.
 * /* w ww  . j  a va2 s.  com*/
 * @throws IOException
 */
static byte[] bigDecimalToByteArray(BigDecimal bd, int maxBytes) {
    BigDecimal cur = bd.stripTrailingZeros();
    ByteArrayOutputStream sb = new ByteArrayOutputStream();

    try {
        byte[] curCodePoint = new byte[1];
        for (int numConverted = 0; numConverted < maxBytes; numConverted++) {
            cur = cur.multiply(ONE_PLACE);
            curCodePoint[0] = cur.byteValue();
            if (0x0 == curCodePoint[0]) {
                break;
            }

            cur = cur.subtract(new BigDecimal(new BigInteger(curCodePoint)));
            sb.write(curCodePoint);
        }
    } catch (IOException e) {
        LOG.error("Error writing byte array", e);
    }

    return sb.toByteArray();
}

From source file:com.github.jessemull.microflex.util.BigDecimalUtil.java

/**
 * Converts a list of BigDecimals to a list of bytes.
 * @param    List<BigDecimal>    list of BigDecimals
 * @return                       list of bytes
 *///from   w ww.j av a 2  s  .c  o  m
public static List<Byte> toByteList(List<BigDecimal> list) {

    List<Byte> byteList = new ArrayList<Byte>();

    for (BigDecimal val : list) {
        if (!OverFlowUtil.byteOverflow(val)) {
            OverFlowUtil.overflowError(val);
        }
        byteList.add(val.byteValue());
    }

    return byteList;

}

From source file:de.csdev.ebus.service.device.EBusDeviceTable.java

public void updateDevice(byte address, Map<String, Object> data) {

    boolean newDevice = false;
    boolean updatedDevice = false;

    if (address == EBusConsts.BROADCAST_ADDRESS) {
        return;//from w w  w  .ja va  2s  . c  o  m
    } else if (EBusUtils.isMasterAddress(address)) {
        address = EBusUtils.getSlaveAddress(address);
    }

    if (address == getOwnDevice().getSlaveAddress()) {
        // ignore own address
        return;
    }

    EBusDevice device = deviceTable.get(address);

    if (device == null) {
        device = new EBusDevice(address, this);
        device.setLastActivity(System.currentTimeMillis());
        deviceTable.put(address, device);
        newDevice = true;
    }

    device.setLastActivity(System.currentTimeMillis());

    if (data != null && !data.isEmpty()) {

        Object obj = data.get("device_id");
        if (obj != null && !obj.equals(device.getDeviceId())) {
            device.setDeviceId((byte[]) obj);
            updatedDevice = true;
        }

        BigDecimal obj2 = NumberUtils.toBigDecimal(data.get("hardware_version"));
        if (obj2 != null && !ObjectUtils.equals(obj2, device.getHardwareVersion())) {
            device.setHardwareVersion(obj2);
            updatedDevice = true;
        }

        obj2 = NumberUtils.toBigDecimal(data.get("software_version"));
        if (obj2 != null && !ObjectUtils.equals(obj2, device.getSoftwareVersion())) {
            device.setSoftwareVersion(obj2);
            updatedDevice = true;
        }

        obj2 = NumberUtils.toBigDecimal(data.get("vendor"));
        if (obj2 != null && !ObjectUtils.equals(obj2.byteValue(), device.getManufacturer())) {
            int intValue = obj2.intValue();
            device.setManufacturer((byte) intValue);
            updatedDevice = true;
        }
    }

    if (newDevice) {
        fireOnDeviceUpdate(IEBusDeviceTableListener.TYPE.NEW, device);
    } else if (updatedDevice) {
        fireOnDeviceUpdate(IEBusDeviceTableListener.TYPE.UPDATE, device);
    } else {
        fireOnDeviceUpdate(IEBusDeviceTableListener.TYPE.UPDATE_ACTIVITY, device);
    }
}

From source file:org.kalypso.model.wspm.tuhh.core.wprof.BCEShapeWPRofContentProvider.java

private <T> T toNumber(final BigDecimal in, final Class<T> outType) {
    if (Byte.class == outType)
        return outType.cast(in.byteValue());
    if (Short.class == outType)
        return outType.cast(in.shortValue());
    if (Integer.class == outType)
        return outType.cast(in.intValue());
    if (Long.class == outType)
        return outType.cast(in.longValue());
    if (Float.class == outType)
        return outType.cast(in.floatValue());
    if (Double.class == outType)
        return outType.cast(in.doubleValue());

    if (BigInteger.class == outType)
        return outType.cast(in.toBigInteger());

    return outType.cast(in);
}

From source file:org.openvpms.component.system.common.jxpath.OpenVPMSTypeConverter.java

/**
 * Convert a {@link BigDecimal} to another type
 * /*  ww w . jav a  2  s  .c om*/
 * @param type
 *            the class to convert too
 * @param value
 *            the value to convert
 * @return Number
 *            the converted number of null.
 *            
 */
protected Number allocateNumber(Class type, BigDecimal value) {
    if (type == Byte.class || type == byte.class) {
        return new Byte(value.byteValue());
    }
    if (type == Short.class || type == short.class) {
        return new Short(value.shortValue());
    }
    if (type == Integer.class || type == int.class) {
        return new Integer(value.intValue());
    }
    if (type == Long.class || type == long.class) {
        return new Long(value.longValue());
    }
    if (type == Float.class || type == float.class) {
        return new Float(value.floatValue());
    }
    if (type == Double.class || type == double.class) {
        return new Double(value.doubleValue());
    }
    if (type == BigDecimal.class) {
        return value;
    }
    if (type == BigInteger.class) {
        return BigInteger.valueOf(value.longValue());
    }

    if (type == Money.class) {
        return new Money((BigDecimal) value);
    }

    return null;
}