BUG-3399: fix BaseYangTypes' range constraints 72/21272/2
authorRobert Varga <rovarga@cisco.com>
Wed, 27 May 2015 23:06:14 +0000 (01:06 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 28 May 2015 07:23:52 +0000 (07:23 +0000)
Constraints Number type should match the Java type a particular type is
bound to. Users dealing with values trying to correlate it to the
metadata available can then rely on things like
value.getClass.cast(rangeMin) just working instead of concocting
conversions to deal with all possible combinations of types.

Also changes BaseConstraints to enforce the two components of a
constraint being the same type, preventing things like min being Integer
while max is a Long.

Change-Id: I38e646b4c08b032248349f3e618354aa2114efd3
Signed-off-by: Robert Varga <rovarga@cisco.com>
code-generator/binding-type-provider/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/BaseYangTypes.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/BaseConstraints.java

index d0b02e6d2f5fe0a6a37c85e77482cc2911d51a8c..7aeaf1d9c52a21b2e81d21515fe4c833d8997068 100644 (file)
@@ -81,7 +81,7 @@ public final class BaseYangTypes {
     /**
      * <code>Type</code> representation of <code>uint8</code> YANG type
      */
-    public static final Type UINT8_TYPE = Types.typeForClass(Short.class, singleRangeRestrictions(0, 255));
+    public static final Type UINT8_TYPE = Types.typeForClass(Short.class, singleRangeRestrictions((short)0, (short)255));
 
     /**
      * <code>Type</code> representation of <code>uint16</code> YANG type
@@ -91,13 +91,13 @@ public final class BaseYangTypes {
     /**
      * <code>Type</code> representation of <code>uint32</code> YANG type
      */
-    public static final Type UINT32_TYPE = Types.typeForClass(Long.class, singleRangeRestrictions(0, 4294967295L));
+    public static final Type UINT32_TYPE = Types.typeForClass(Long.class, singleRangeRestrictions(0L, 4294967295L));
 
     /**
      * <code>Type</code> representation of <code>uint64</code> YANG type
      */
     public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class,
-            singleRangeRestrictions(0, new BigInteger("18446744073709551615")));
+            singleRangeRestrictions(BigInteger.ZERO, new BigInteger("18446744073709551615")));
 
     public static final Type UNION_TYPE = new UnionType();
 
@@ -219,7 +219,7 @@ public final class BaseYangTypes {
         }
     };
 
-    private static Restrictions singleRangeRestrictions(final Number min, final Number max) {
+    private static <T extends Number> Restrictions singleRangeRestrictions(final T min, final T max) {
         return new Restrictions() {
             @Override
             public boolean isEmpty() {
index 9fe6805c815a3abfd42dca8e452c8f00d8d00bfb..8a186ce8530921f5d0d9853ecd2da6b1214990e4 100644 (file)
@@ -7,12 +7,11 @@
  */
 package org.opendaylight.yangtools.yang.model.util;
 
+import com.google.common.base.Optional;
 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
 
-import com.google.common.base.Optional;
-
 /**
  * Utility class which provides factory methods to construct Constraints.
  *
@@ -67,13 +66,14 @@ public final class BaseConstraints {
      *
      * @see RangeConstraint
      *
+     * @param <T> Type of constraint
      * @param min value-restricting lower bound value. The value MUST NOT Be null.
      * @param max value-restricting upper bound value. The value MUST NOT Be null.
      * @param description Description associated with constraint. {@link Optional#absent()} if description is undefined.
      * @param reference Reference associated with constraint. {@link Optional#absent()} if reference is undefined.
      * @return Instance of {@link RangeConstraint}
      */
-    public static RangeConstraint newRangeConstraint(final Number min, final Number max, final Optional<String> description,
+    public static <T extends Number> RangeConstraint newRangeConstraint(final T min, final T max, final Optional<String> description,
             final Optional<String> reference) {
         return new RangeConstraintImpl(min, max, description, reference);
     }