Add range validation and normalization
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / type / NumberUtil.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.model.util.type;
9
10 import com.google.common.base.Function;
11 import com.google.common.collect.ImmutableMap;
12 import java.math.BigDecimal;
13 import java.math.BigInteger;
14 import java.util.Map;
15
16 final class NumberUtil {
17     private static final Map<Class<? extends Number>, Function<Number, Number>> CONVERTERS;
18     static {
19         final ImmutableMap.Builder<Class<? extends Number>, Function<Number, Number>> b = ImmutableMap.builder();
20         b.put(Byte.class, new Function<Number, Number>() {
21             @Override
22             public Number apply(final Number input) {
23                 if (input instanceof Byte) {
24                     return input;
25                 }
26
27                 return Byte.valueOf(input.toString());
28             }
29         });
30         b.put(Short.class, new Function<Number, Number>() {
31             @Override
32             public Number apply(final Number input) {
33                 if (input instanceof Short) {
34                     return input;
35                 }
36                 if (input instanceof Byte) {
37                     return input.shortValue();
38                 }
39
40                 return Short.valueOf(input.toString());
41             }
42         });
43         b.put(Integer.class, new Function<Number, Number>() {
44             @Override
45             public Number apply(final Number input) {
46                 if (input instanceof Integer) {
47                     return input;
48                 }
49                 if (input instanceof Byte || input instanceof Short) {
50                     return input.intValue();
51                 }
52
53                 return Integer.valueOf(input.toString());
54             }
55         });
56         b.put(Long.class, new Function<Number, Number>() {
57             @Override
58             public Number apply(final Number input) {
59                 if (input instanceof Long) {
60                     return input;
61                 }
62                 if (input instanceof Byte || input instanceof Short || input instanceof Integer) {
63                     return input.longValue();
64                 }
65
66                 return Long.valueOf(input.toString());
67             }
68         });
69         b.put(BigDecimal.class, new Function<Number, Number>() {
70             @Override
71             public Number apply(final Number input) {
72                 if (input instanceof BigDecimal) {
73                     return input;
74                 }
75                 if (input instanceof Byte || input instanceof Short ||
76                         input instanceof Integer || input instanceof Long) {
77                     return BigDecimal.valueOf(input.longValue());
78                 }
79
80                 return new BigDecimal(input.toString());
81             }
82         });
83         b.put(BigInteger.class, new Function<Number, Number>() {
84             @Override
85             public Number apply(final Number input) {
86                 if (input instanceof BigInteger) {
87                     return input;
88                 }
89                 if (input instanceof Byte || input instanceof Short ||
90                         input instanceof Integer || input instanceof Long) {
91                     return BigInteger.valueOf(input.longValue());
92                 }
93
94                 return new BigInteger(input.toString());
95             }
96         });
97         CONVERTERS = b.build();
98     }
99
100     private NumberUtil() {
101         throw new UnsupportedOperationException();
102     }
103
104     static Function<Number, Number> converterTo(final Class<? extends Number> clazz) {
105         return CONVERTERS.get(clazz);
106     }
107 }