package org.opendaylight.yangtools.yang.model.util.type;
import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.math.BigDecimal;
import java.math.BigInteger;
+import java.util.Comparator;
import java.util.Map;
final class NumberUtil {
+ private static final Comparator<Number> NUMBER_COMPARATOR = new Comparator<Number>() {
+ @Override
+ public int compare(final Number o1, final Number o2) {
+ Preconditions.checkArgument(o1.getClass().equals(o2.getClass()), "Incompatible Number classes %s and %s",
+ o1.getClass(), o2.getClass());
+
+ if (o1 instanceof Byte) {
+ return ((Byte)o1).compareTo((Byte) o2);
+ } else if (o1 instanceof Short) {
+ return ((Short)o1).compareTo((Short) o2);
+ } else if (o1 instanceof Integer) {
+ return ((Integer)o1).compareTo((Integer) o2);
+ } else if (o1 instanceof Long) {
+ return ((Long)o1).compareTo((Long) o2);
+ } else if (o1 instanceof BigDecimal) {
+ return ((BigDecimal)o1).compareTo((BigDecimal) o2);
+ } else if (o1 instanceof BigInteger) {
+ return ((BigInteger)o1).compareTo((BigInteger) o2);
+ } else {
+ throw new IllegalArgumentException("Unsupported Number class " + o1.getClass());
+ }
+ }
+ };
+
private static final Map<Class<? extends Number>, Function<Number, Number>> CONVERTERS;
static {
final ImmutableMap.Builder<Class<? extends Number>, Function<Number, Number>> b = ImmutableMap.builder();
static Function<Number, Number> converterTo(final Class<? extends Number> clazz) {
return CONVERTERS.get(clazz);
}
+
+ static boolean isRangeCovered(final Number min, final Number max, final Number superMin, final Number superMax) {
+ return NumberUtil.NUMBER_COMPARATOR.compare(min, superMin) >= 0 &&
+ NumberUtil.NUMBER_COMPARATOR.compare(max, superMax) <= 0;
+ }
}
private static boolean rangeCovered(final List<RangeConstraint> where,
final RangeConstraint what) {
- // We have ensured the types match, and we are sure each of those types implements comparable
- @SuppressWarnings("unchecked")
- final Comparable<Object> min = (Comparable<Object>) what.getMin();
- @SuppressWarnings("unchecked")
- final Comparable<Object> max = (Comparable<Object>) what.getMax();
-
for (RangeConstraint c : where) {
- if (min.compareTo(c.getMin()) >= 0 && max.compareTo(c.getMax()) <= 0) {
+ if (NumberUtil.isRangeCovered(what.getMin(), what.getMax(), c.getMin(), c.getMax())) {
return true;
}
}