Adjust to yangtools-2.0.0 changes
[mdsal.git] / binding2 / mdsal-binding2-generator-util / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / util / BindingGeneratorUtil.java
index ec86229a479082376d51264d6241c261a7ce0c94..67ed5a72f8a5ceb980ee93996cd729f5e5fef70d 100644 (file)
@@ -19,6 +19,7 @@ import com.google.common.collect.Iterables;
 import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
+import java.math.BigDecimal;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
@@ -27,6 +28,7 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Optional;
 import org.opendaylight.mdsal.binding.javav2.model.api.AccessModifier;
 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
@@ -36,18 +38,17 @@ import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.MethodSignat
 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.TypeMemberBuilder;
 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
 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 org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
 import org.opendaylight.yangtools.yang.model.util.type.DecimalTypeBuilder;
 
@@ -71,8 +72,8 @@ public final class BindingGeneratorUtil {
 
     private static final Restrictions EMPTY_RESTRICTIONS = new Restrictions() {
         @Override
-        public List<LengthConstraint> getLengthConstraints() {
-            return Collections.emptyList();
+        public Optional<LengthConstraint> getLengthConstraint() {
+            return Optional.empty();
         }
 
         @Override
@@ -81,8 +82,8 @@ public final class BindingGeneratorUtil {
         }
 
         @Override
-        public List<RangeConstraint> getRangeConstraints() {
-            return Collections.emptyList();
+        public Optional<RangeConstraint<?>> getRangeConstraint() {
+            return Optional.empty();
         }
 
         @Override
@@ -124,7 +125,7 @@ public final class BindingGeneratorUtil {
             }
 
             for (final MethodSignatureBuilder m : sortedCollection(SUID_MEMBER_COMPARATOR, to.getMethodDefinitions())) {
-                if (!(m.getAccessModifier().equals(AccessModifier.PRIVATE))) {
+                if (!m.getAccessModifier().equals(AccessModifier.PRIVATE)) {
                     dout.writeUTF(m.getName());
                     dout.write(m.getAccessModifier().ordinal());
                 }
@@ -138,7 +139,7 @@ public final class BindingGeneratorUtil {
         final byte[] hashBytes = SHA1_MD.get().digest(bout.toByteArray());
         long hash = 0;
         for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) {
-            hash = (hash << 8) | (hashBytes[i] & 0xFF);
+            hash = hash << 8 | hashBytes[i] & 0xFF;
         }
         return hash;
     }
@@ -208,14 +209,14 @@ public final class BindingGeneratorUtil {
     }
 
     public static Restrictions getRestrictions(final TypeDefinition<?> type) {
-        if ((type == null) || (type.getBaseType() == null)) {
+        if (type == null || type.getBaseType() == null) {
             if (type instanceof DecimalTypeDefinition) {
                 final DecimalTypeDefinition decimal = (DecimalTypeDefinition) type;
                 final DecimalTypeBuilder tmpBuilder = BaseTypes.decimalTypeBuilder(decimal.getPath());
                 tmpBuilder.setFractionDigits(decimal.getFractionDigits());
                 final DecimalTypeDefinition tmp = tmpBuilder.build();
 
-                if (!tmp.getRangeConstraints().equals(decimal.getRangeConstraints())) {
+                if (!tmp.getRangeConstraint().equals(decimal.getRangeConstraint())) {
                     return new Restrictions() {
                         @Override
                         public boolean isEmpty() {
@@ -223,8 +224,8 @@ public final class BindingGeneratorUtil {
                         }
 
                         @Override
-                        public List<RangeConstraint> getRangeConstraints() {
-                            return decimal.getRangeConstraints();
+                        public Optional<RangeConstraint<BigDecimal>> getRangeConstraint() {
+                            return decimal.getRangeConstraint();
                         }
 
                         @Override
@@ -233,8 +234,8 @@ public final class BindingGeneratorUtil {
                         }
 
                         @Override
-                        public List<LengthConstraint> getLengthConstraints() {
-                            return ImmutableList.of();
+                        public Optional<LengthConstraint> getLengthConstraint() {
+                            return Optional.empty();
                         }
                     };
                 }
@@ -243,9 +244,9 @@ public final class BindingGeneratorUtil {
             return EMPTY_RESTRICTIONS;
         }
 
-        final List<LengthConstraint> length;
+        final Optional<LengthConstraint> length;
         final List<PatternConstraint> pattern;
-        final List<RangeConstraint> range;
+        final Optional<? extends RangeConstraint<?>> range;
 
         /*
          * Take care of extended types.
@@ -261,73 +262,56 @@ public final class BindingGeneratorUtil {
         if (type instanceof BinaryTypeDefinition) {
             final BinaryTypeDefinition binary = (BinaryTypeDefinition)type;
             final BinaryTypeDefinition base = binary.getBaseType();
-            if ((base != null) && (base.getBaseType() != null)) {
-                length = currentOrEmpty(binary.getLengthConstraints(), base.getLengthConstraints());
+            if (base != null && base.getBaseType() != null) {
+                length = currentOrEmpty(binary.getLengthConstraint(), base.getLengthConstraint());
             } else {
-                length = binary.getLengthConstraints();
+                length = binary.getLengthConstraint();
             }
 
             pattern = ImmutableList.of();
-            range = ImmutableList.of();
+            range = Optional.empty();
         } else if (type instanceof DecimalTypeDefinition) {
-            length = ImmutableList.of();
+            length = Optional.empty();
             pattern = ImmutableList.of();
 
             final DecimalTypeDefinition decimal = (DecimalTypeDefinition)type;
             final DecimalTypeDefinition base = decimal.getBaseType();
-            if ((base != null) && (base.getBaseType() != null)) {
-                range = currentOrEmpty(decimal.getRangeConstraints(), base.getRangeConstraints());
+            if (base != null && base.getBaseType() != null) {
+                range = currentOrEmpty(decimal.getRangeConstraint(), base.getRangeConstraint());
             } else {
-                range = decimal.getRangeConstraints();
+                range = decimal.getRangeConstraint();
             }
-        } else if (type instanceof IntegerTypeDefinition) {
-            length = ImmutableList.of();
+        } else if (type instanceof RangeRestrictedTypeDefinition) {
+            // Integer-like types
+            length = Optional.empty();
             pattern = ImmutableList.of();
-
-            final IntegerTypeDefinition integer = (IntegerTypeDefinition)type;
-            final IntegerTypeDefinition base = integer.getBaseType();
-            if ((base != null) && (base.getBaseType() != null)) {
-                range = currentOrEmpty(integer.getRangeConstraints(), base.getRangeConstraints());
-            } else {
-                range = integer.getRangeConstraints();
-            }
+            range = extractRangeConstraint((RangeRestrictedTypeDefinition<?, ?>)type);
         } else if (type instanceof StringTypeDefinition) {
             final StringTypeDefinition string = (StringTypeDefinition)type;
             final StringTypeDefinition base = string.getBaseType();
-            if ((base != null) && (base.getBaseType() != null)) {
-                length = currentOrEmpty(string.getLengthConstraints(), base.getLengthConstraints());
+            if (base != null && base.getBaseType() != null) {
+                length = currentOrEmpty(string.getLengthConstraint(), base.getLengthConstraint());
             } else {
-                length = string.getLengthConstraints();
+                length = string.getLengthConstraint();
             }
 
             pattern = uniquePatterns(string);
-            range = ImmutableList.of();
-        } else if (type instanceof UnsignedIntegerTypeDefinition) {
-            length = ImmutableList.of();
-            pattern = ImmutableList.of();
-
-            final UnsignedIntegerTypeDefinition unsigned = (UnsignedIntegerTypeDefinition)type;
-            final UnsignedIntegerTypeDefinition base = unsigned.getBaseType();
-            if ((base != null) && (base.getBaseType() != null)) {
-                range = currentOrEmpty(unsigned.getRangeConstraints(), base.getRangeConstraints());
-            } else {
-                range = unsigned.getRangeConstraints();
-            }
+            range = Optional.empty();
         } else {
-            length = ImmutableList.of();
+            length = Optional.empty();
             pattern = ImmutableList.of();
-            range = ImmutableList.of();
+            range = Optional.empty();
         }
 
         // Now, this may have ended up being empty, too...
-        if (length.isEmpty() && pattern.isEmpty() && range.isEmpty()) {
+        if (!length.isPresent() && pattern.isEmpty() && !range.isPresent()) {
             return EMPTY_RESTRICTIONS;
         }
 
         // Nope, not empty allocate a holder
         return new Restrictions() {
             @Override
-            public List<RangeConstraint> getRangeConstraints() {
+            public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
                 return range;
             }
             @Override
@@ -335,7 +319,7 @@ public final class BindingGeneratorUtil {
                 return pattern;
             }
             @Override
-            public List<LengthConstraint> getLengthConstraints() {
+            public Optional<LengthConstraint> getLengthConstraint() {
                 return length;
             }
             @Override
@@ -395,7 +379,7 @@ public final class BindingGeneratorUtil {
      * @throws NullPointerException if any of the arguments are null
      */
     public static String packageNameForAugmentedGeneratedType(final String parentAugmentPackageName,
-                                                              final AugmentationSchema augmentationSchema) {
+                                                              final AugmentationSchemaNode augmentationSchema) {
         final QName last = augmentationSchema.getTargetPath().getLastComponent();
 
         return generateNormalizedPackageName(parentAugmentPackageName, last);
@@ -485,8 +469,8 @@ public final class BindingGeneratorUtil {
         }
     }
 
-    private static <T> List<T> currentOrEmpty(final List<T> current, final List<T> base) {
-        return current.equals(base) ? ImmutableList.of() : current;
+    private static <T extends Optional<?>> T currentOrEmpty(final T current, final T base) {
+        return current.equals(base) ? (T)Optional.empty() : current;
     }
 
     private static List<PatternConstraint> uniquePatterns(final StringTypeDefinition type) {
@@ -517,4 +501,15 @@ public final class BindingGeneratorUtil {
 
         return false;
     }
+
+    private static <T extends RangeRestrictedTypeDefinition<?, ?>> Optional<? extends RangeConstraint<?>>
+            extractRangeConstraint(final T def) {
+        final T base = (T) def.getBaseType();
+        if (base != null && base.getBaseType() != null) {
+            return currentOrEmpty(def.getRangeConstraint(), base.getRangeConstraint());
+        }
+
+        return def.getRangeConstraint();
+    }
+
 }