001package org.unix4j.convert;
002
003import java.util.concurrent.atomic.AtomicInteger;
004import java.util.concurrent.atomic.AtomicLong;
005
006public class LongConverters {
007        public static final ValueConverter<Long> NUMBER_TO_LONG = new ValueConverter<Long>() {
008                @Override
009                public Long convert(Object value) throws IllegalArgumentException {
010                        if (value instanceof Number) {
011                                return ((Number)value).longValue();
012                        }
013                        return null;
014                }
015        };
016        public static final ValueConverter<Long> NUMBER_ROUNDED_TO_LONG = new ValueConverter<Long>() {
017                @Override
018                public Long convert(Object value) throws IllegalArgumentException {
019                        if (value instanceof Long || value instanceof Integer || value instanceof Short || value instanceof Byte || value instanceof AtomicInteger || value instanceof AtomicLong) {
020                                return ((Number)value).longValue();
021                        }
022                        if (value instanceof Number) {
023                                return Math.round(((Number)value).doubleValue());
024                        }
025                        return null;
026                }
027        };
028        public static final ValueConverter<Long> STRING_TO_LONG = new ValueConverter<Long>() {
029                @Override
030                public Long convert(Object value) throws IllegalArgumentException {
031                        if (value != null) {
032                                try {
033                                        return Long.parseLong(value.toString());
034                                } catch (NumberFormatException e) {
035                                        return null;
036                                }
037                        }
038                        return null;
039                }
040        };
041        public static final ValueConverter<Long> DEFAULT = new CompositeValueConverter<Long>().add(NUMBER_TO_LONG).add(STRING_TO_LONG);
042        public static final ValueConverter<Long> ROUND = new CompositeValueConverter<Long>().add(NUMBER_ROUNDED_TO_LONG).add(STRING_TO_LONG);
043}