2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util.type;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import java.math.BigDecimal;
13 import java.math.BigInteger;
14 import java.util.Comparator;
16 import java.util.function.Function;
18 final class NumberUtil {
19 private static final Comparator<Number> NUMBER_COMPARATOR = (o1, o2) -> {
20 Preconditions.checkArgument(o1.getClass().equals(o2.getClass()), "Incompatible Number classes %s and %s",
21 o1.getClass(), o2.getClass());
23 if (o1 instanceof Byte) {
24 return ((Byte)o1).compareTo((Byte) o2);
25 } else if (o1 instanceof Short) {
26 return ((Short)o1).compareTo((Short) o2);
27 } else if (o1 instanceof Integer) {
28 return ((Integer)o1).compareTo((Integer) o2);
29 } else if (o1 instanceof Long) {
30 return ((Long)o1).compareTo((Long) o2);
31 } else if (o1 instanceof BigDecimal) {
32 return ((BigDecimal)o1).compareTo((BigDecimal) o2);
33 } else if (o1 instanceof BigInteger) {
34 return ((BigInteger)o1).compareTo((BigInteger) o2);
36 throw new IllegalArgumentException("Unsupported Number class " + o1.getClass());
40 private static final Map<Class<? extends Number>, Function<Number, Number>> CONVERTERS;
43 final ImmutableMap.Builder<Class<? extends Number>, Function<Number, Number>> b = ImmutableMap.builder();
44 b.put(Byte.class, input -> {
45 if (input instanceof Byte) {
49 return Byte.valueOf(input.toString());
51 b.put(Short.class, input -> {
52 if (input instanceof Short) {
55 if (input instanceof Byte) {
56 return input.shortValue();
59 return Short.valueOf(input.toString());
61 b.put(Integer.class, input -> {
62 if (input instanceof Integer) {
65 if (input instanceof Byte || input instanceof Short) {
66 return input.intValue();
69 return Integer.valueOf(input.toString());
71 b.put(Long.class, input -> {
72 if (input instanceof Long) {
75 if (input instanceof Byte || input instanceof Short || input instanceof Integer) {
76 return input.longValue();
79 return Long.valueOf(input.toString());
81 b.put(BigDecimal.class, input -> {
82 if (input instanceof BigDecimal) {
85 if (input instanceof Byte || input instanceof Short || input instanceof Integer || input instanceof Long) {
86 return BigDecimal.valueOf(input.longValue());
89 return new BigDecimal(input.toString());
91 b.put(BigInteger.class, input -> {
92 if (input instanceof BigInteger) {
95 if (input instanceof Byte || input instanceof Short || input instanceof Integer || input instanceof Long) {
96 return BigInteger.valueOf(input.longValue());
99 return new BigInteger(input.toString());
101 CONVERTERS = b.build();
104 private NumberUtil() {
105 throw new UnsupportedOperationException();
108 static Function<Number, Number> converterTo(final Class<? extends Number> clazz) {
109 return CONVERTERS.get(clazz);
112 static boolean isRangeCovered(final Number min, final Number max, final Number superMin, final Number superMax) {
113 return NumberUtil.NUMBER_COMPARATOR.compare(min, superMin) >= 0
114 && NumberUtil.NUMBER_COMPARATOR.compare(max, superMax) <= 0;