Example usage for java.lang Integer longValue

List of usage examples for java.lang Integer longValue

Introduction

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

Prototype

public long longValue() 

Source Link

Document

Returns the value of this Integer as a long after a widening primitive conversion.

Usage

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

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

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

    if (value != null && returnType != null && (returnType == int.class || returnType == Integer.class)) {
        Integer valueToProcess = null;
        String valueToReturn = null;

        if (returnType == int.class) {
            valueToProcess = Integer.valueOf((int) value);
        } else {
            valueToProcess = (Integer) value;
        }

        if (valueToProcess != null) {
            try {
                Function function = objectDTO.getSerializer();
                if (function != null) {
                    try {
                        if (function instanceof DataMapper2JsonFunction) {
                            DataMapper classData = new DataMapper(returnType, value, objectDTO.classMapper,
                                    objectDTO.level, getPrettyIndentation());
                            return ((DataMapper2JsonFunction) function).apply(classData);

                        } else if (function instanceof Integer2JsonFunction) {
                            return ((Integer2JsonFunction) function).apply(valueToProcess);

                        } else {

                            Object returnedValue = function.apply(valueToProcess);

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

                            if (returnedValue == null) {
                                return integer2JsonDefault(objectDTO);

                            } else if (returnedValue instanceof Integer) {
                                valueToProcess = (Integer) returnedValue;

                            } else {
                                objectDTO.valueToProcess = returnedValue;
                                return object2String(objectDTO);
                            }

                        }

                    } catch (Exception e) {
                    }
                }

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

                    if (min != null && min > valueToProcess.longValue()) {
                        valueToProcess = min.intValue();
                    }

                    if (max != null && valueToProcess > max.longValue()) {
                        valueToProcess = max.intValue();
                    }

                    return NumberUtil.toPlainString(valueToProcess);
                }

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

    return integer2JsonDefault(objectDTO);
}

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

private <E, R> R json2Integer(FieldData objectDTO) {
    if (objectDTO == null || !objectDTO.json2Java) {
        return null;
    }// w  ww .  j  a v a2s.c om

    E value = (E) objectDTO.valueToProcess;
    Class<R> returnType = objectDTO.returnType;

    if (value != null && value.toString().trim().length() > 0) {
        String valueToProcess = value.toString().trim();
        Integer 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 Json2IntegerFunction) {
                        return (R) ((Json2IntegerFunction) 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 Integer) {
                            valueToReturn = (Integer) returnedValue;
                        } else if (returnedValue instanceof String) {
                            valueToReturn = Integer
                                    .parseInt(NumberUtil.removeTrailingDecimalZeros(returnedValue));
                            //  byte, double, float, int, long, and short.
                        } else if (returnedValue instanceof Long) {
                            valueToReturn = ((Long) returnedValue).intValue();
                        } else if (returnedValue instanceof Byte) {
                            valueToReturn = ((Byte) returnedValue).intValue();
                        } else if (returnedValue instanceof Short) {
                            valueToReturn = ((Short) returnedValue).intValue();
                        } else if (returnedValue instanceof Float) {
                            valueToReturn = ((Float) returnedValue).intValue();
                        } else if (returnedValue instanceof Double) {
                            valueToReturn = ((Double) returnedValue).intValue();
                        } else if (returnedValue instanceof BigInteger) {
                            valueToReturn = ((BigInteger) returnedValue).intValue();
                        } else if (returnedValue instanceof BigDecimal) {
                            valueToReturn = ((BigDecimal) returnedValue).intValue();
                        } else if (returnedValue instanceof AtomicInteger) {
                            valueToReturn = ((AtomicInteger) returnedValue).intValue();
                        } else if (returnedValue instanceof AtomicLong) {
                            valueToReturn = ((AtomicLong) returnedValue).intValue();
                        } else {
                            valueToReturn = ((Number) returnedValue).intValue();
                        }

                    } else if (returnedValue instanceof Character) {
                        valueToReturn = Character.getNumericValue((Character) returnedValue);

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

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

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

                    } else {
                        valueToReturn = Integer.parseInt(NumberUtil.removeTrailingDecimalZeros(returnedValue));
                    }

                    return (R) valueToReturn;

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

            } else {
                valueToReturn = Integer.parseInt(NumberUtil.removeTrailingDecimalZeros(valueToProcess));
            }

            if (valueToReturn != null) {
                Long min = objectDTO.getMin();
                Long max = objectDTO.getMax();
                if (min != null && min > valueToReturn.longValue()) {
                    return (R) (Integer) min.intValue();
                }

                if (max != null && valueToReturn.longValue() > max) {
                    valueToReturn = Integer.valueOf(max.intValue());
                }

                return (R) valueToReturn;
            }

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

    }

    return json2IntegerDefault(objectDTO);
}