Example usage for java.lang Byte MAX_VALUE

List of usage examples for java.lang Byte MAX_VALUE

Introduction

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

Prototype

byte MAX_VALUE

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

Click Source Link

Document

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

Usage

From source file:com.cohort.util.String2.java

/**
 * This converts a string to a boolean and then a byte.
 * // w  ww .j  a  va  2 s  . co  m
 * @param s the string
 * @return Byte.MAX_VALUE (i.e., missing value) if s is null or s is "". 
 *   Return 0 if s is "false", "f", or "0".   
 *   Return 1 if for all other values.
 *   Case and leading/trailing spaces don't matter.
 */
public static byte parseBooleanToByte(String s) {
    if (s == null)
        return Byte.MAX_VALUE;
    s = s.toLowerCase().trim();
    if (s.length() == 0)
        return Byte.MAX_VALUE;
    return (s.equals("false") || s.equals("f") || s.equals("0")) ? (byte) 0 : (byte) 1;
}

From source file:ca.oson.json.Oson.java

private <E> Byte json2Byte(FieldData objectDTO) {
    if (objectDTO == null || !objectDTO.json2Java) {
        return null;
    }//from  w  ww .j  a  va  2 s .co  m

    Object value = objectDTO.valueToProcess;
    Class<E> returnType = objectDTO.returnType;

    if (value != null && value.toString().trim().length() > 0) {
        String valueToProcess = value.toString().trim();
        Byte valueToReturn = null;

        try {
            Function function = objectDTO.getDeserializer();

            if (function != null) {
                try {
                    Object returnedValue = null;

                    if (function instanceof Json2DataMapperFunction) {
                        DataMapper classData = new DataMapper(returnType, value, objectDTO.classMapper,
                                objectDTO.level, getPrettyIndentation());
                        returnedValue = ((Json2DataMapperFunction) function).apply(classData);

                    } else if (function instanceof Json2FieldDataFunction) {
                        Json2FieldDataFunction f = (Json2FieldDataFunction) function;
                        FieldData fieldData = objectDTO.clone();

                        returnedValue = f.apply(fieldData);

                    } else if (function instanceof Json2ByteFunction) {
                        return ((Json2ByteFunction) function).apply(valueToProcess);
                    } else {
                        returnedValue = function.apply(valueToProcess);
                    }

                    if (returnedValue instanceof Optional) {
                        returnedValue = ObjectUtil.unwrap(returnedValue);
                    }

                    if (returnedValue == null) {
                        return null;

                    } else if (Number.class.isAssignableFrom(returnedValue.getClass())
                            || returnedValue.getClass().isPrimitive()) {

                        if (returnedValue instanceof Byte) {
                            valueToReturn = (Byte) returnedValue;
                        } else if (returnedValue instanceof String) {
                            valueToReturn = Byte
                                    .parseByte(NumberUtil.removeTrailingDecimalZeros(returnedValue));

                        } else if (returnedValue instanceof Integer) {
                            valueToReturn = ((Integer) returnedValue).byteValue();
                        } else if (returnedValue instanceof Long) {
                            valueToReturn = ((Long) returnedValue).byteValue();
                        } else if (returnedValue instanceof Short) {
                            valueToReturn = ((Short) returnedValue).byteValue();
                        } else if (returnedValue instanceof Double) {
                            valueToReturn = ((Double) returnedValue).byteValue();
                        } else if (returnedValue instanceof Float) {
                            valueToReturn = ((Float) returnedValue).byteValue();
                        } else if (returnedValue instanceof BigInteger) {
                            valueToReturn = ((BigInteger) returnedValue).byteValue();
                        } else if (returnedValue instanceof BigDecimal) {
                            valueToReturn = ((BigDecimal) returnedValue).byteValue();
                        } else if (returnedValue instanceof AtomicInteger) {
                            valueToReturn = ((AtomicInteger) returnedValue).byteValue();
                        } else if (returnedValue instanceof AtomicLong) {
                            valueToReturn = ((AtomicLong) returnedValue).byteValue();
                        } else {
                            valueToReturn = ((Number) returnedValue).byteValue();
                        }

                    } else if (returnedValue instanceof Character) {
                        char c = (Character) returnedValue;
                        valueToReturn = (byte) c;

                    } else if (returnedValue instanceof Boolean) {
                        if ((Boolean) returnedValue)
                            valueToReturn = 1;
                        else
                            valueToReturn = 0;

                    } else if (Enum.class.isAssignableFrom(returnedValue.getClass())) {
                        valueToReturn = ((Integer) ((Enum) returnedValue).ordinal()).byteValue();

                    } else if (Date.class.isAssignableFrom(returnedValue.getClass())) {
                        valueToReturn = (byte) ((Date) returnedValue).getTime();

                    } else {
                        valueToReturn = Byte.parseByte(NumberUtil.removeTrailingDecimalZeros(returnedValue));
                    }

                    return valueToReturn;

                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                long longValue = Long.parseLong(NumberUtil.removeTrailingDecimalZeros(valueToProcess));

                if (longValue > Byte.MAX_VALUE) {
                    valueToReturn = Byte.MAX_VALUE;
                } else {
                    valueToReturn = (byte) longValue;
                }

                // valueToReturn = Byte.parseByte(valueToProcess);
            }

            if (valueToReturn != null) {
                Long min = objectDTO.getMin();
                Long max = objectDTO.getMax();

                if (min != null && min.byteValue() > valueToReturn) {
                    return min.byteValue();
                }

                if (max != null && max.byteValue() < valueToReturn) {
                    valueToReturn = max.byteValue();
                }

                return valueToReturn;
            }

        } catch (Exception ex) {
            //ex.printStackTrace();
        }

    }

    return json2ByteDefault(objectDTO);
}