Example usage for java.util.concurrent.atomic AtomicLong AtomicLong

List of usage examples for java.util.concurrent.atomic AtomicLong AtomicLong

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicLong AtomicLong.

Prototype

public AtomicLong(long initialValue) 

Source Link

Document

Creates a new AtomicLong with the given initial value.

Usage

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

private <E, R> AtomicLong json2AtomicLong(FieldData objectDTO) {
    if (objectDTO == null || !objectDTO.json2Java) {
        return null;
    }/*from  w  w w.ja v a  2  s .c  o m*/

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

    if (value != null && value.toString().trim().length() > 0) {
        String valueToProcess = value.toString().trim();
        AtomicLong 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 Json2AtomicLongFunction) {
                        return ((Json2AtomicLongFunction) 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 AtomicLong) {
                            valueToReturn = (AtomicLong) returnedValue;
                        } else if (returnedValue instanceof String) {
                            valueToReturn = new AtomicLong(
                                    Long.parseLong(NumberUtil.removeTrailingDecimalZeros(returnedValue)));

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

                    } else if (returnedValue instanceof Character) {
                        valueToReturn = new AtomicLong(((Character) returnedValue));

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

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

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

                    } else {
                        valueToReturn = new AtomicLong(
                                Long.parseLong(NumberUtil.removeTrailingDecimalZeros(returnedValue)));
                    }

                    return valueToReturn;

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

            } else {
                valueToReturn = new AtomicLong(
                        Long.parseLong(NumberUtil.removeTrailingDecimalZeros(valueToProcess)));
            }

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

                if (min != null && min > valueToReturn.longValue()) {
                    return new AtomicLong(min);
                }

                if (max != null && valueToReturn.longValue() > max) {
                    valueToReturn = new AtomicLong(max);
                }

                return valueToReturn;
            }

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

    }

    return json2AtomicLongDefault(objectDTO);
}

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

private <E, R> String atomicLong2Json(FieldData objectDTO) {
    if (objectDTO == null || objectDTO.json2Java) {
        return null;
    }// ww  w .java  2 s  .c o  m

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

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

        if (value instanceof AtomicLong) {
            valueToProcess = (AtomicLong) value;
        } else {
            try {
                valueToProcess = new AtomicLong(Long.parseLong(NumberUtil.removeTrailingDecimalZeros(value)));
            } catch (Exception ex) {
            }
        }

        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 AtomicLong2JsonFunction) {
                            return ((AtomicLong2JsonFunction) function).apply(valueToProcess);

                        } else {
                            Object returnedValue = null;
                            if (function instanceof FieldData2JsonFunction) {
                                FieldData2JsonFunction f = (FieldData2JsonFunction) function;
                                FieldData fieldData = objectDTO.clone();
                                returnedValue = f.apply(fieldData);
                            } else {
                                returnedValue = function.apply(value);
                            }

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

                            if (returnedValue == null) {
                                return null;

                            } else if (returnedValue instanceof AtomicLong) {
                                valueToProcess = (AtomicLong) 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 = new AtomicLong(min);
                    }

                    if (max != null && max < valueToProcess.longValue()) {
                        valueToProcess = new AtomicLong(max);
                    }

                    Integer precision = objectDTO.getPrecision();
                    if (precision != null) {
                        valueToProcess = (AtomicLong) NumberUtil.setPrecision(valueToProcess, precision,
                                getRoundingMode());
                    }

                    return NumberUtil.toPlainString(valueToProcess);
                }

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

    return atomicLong2JsonDefault(objectDTO);
}

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

private <E, R> AtomicLong json2AtomicLongDefault(FieldData objectDTO) {
    E value = (E) objectDTO.valueToProcess;
    Class<R> returnType = objectDTO.returnType;
    boolean required = objectDTO.required();

    Long min = objectDTO.getMin();
    Long max = objectDTO.getMax();

    if (getDefaultType() == JSON_INCLUDE.DEFAULT || required) {
        AtomicLong defaultValue = (AtomicLong) objectDTO.getDefaultValue();
        if (defaultValue != null) {
            if (min != null && min > defaultValue.longValue()) {
                return new AtomicLong(min);
            }/*w  w w  .j  av a  2s.c  o  m*/

            if (max != null && max < defaultValue.longValue()) {
                return new AtomicLong(max);
            }

            return defaultValue;
        }

        if (min != null && min > DefaultValue.atomicLong.longValue()) {
            return new AtomicLong(min);
        }

        return DefaultValue.atomicLong;
    }

    return null;
}