Deprecate ClassToInstance-taking methods
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / GeneratorUtil.java
index 8c8e3075c305806fc2135143db92046b101026b8..2c6c5a487d0d8e00711d02e533d0ba560af70988 100644 (file)
@@ -13,7 +13,9 @@ import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
+import org.opendaylight.mdsal.binding.model.api.ConcreteType;
 import org.opendaylight.mdsal.binding.model.api.Constant;
 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
@@ -23,10 +25,14 @@ import org.opendaylight.mdsal.binding.model.api.MethodSignature;
 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
 import org.opendaylight.mdsal.binding.model.api.Type;
 import org.opendaylight.mdsal.binding.model.api.WildcardType;
+import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Single;
 import org.opendaylight.mdsal.binding.model.ri.TypeConstants;
 import org.opendaylight.mdsal.binding.model.ri.Types;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 
 public final class GeneratorUtil {
+    private static final ConcreteType PATTERN = Types.typeForClass(Pattern.class);
+
     private GeneratorUtil() {
         // Hidden on purpose
     }
@@ -54,9 +60,9 @@ public final class GeneratorUtil {
         }
 
         // REGULAR EXPRESSION
-        if (genType instanceof GeneratedTransferObject
-                && isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {
-            putTypeIntoImports(genType, Types.typeForClass(java.util.regex.Pattern.class), imports);
+        if (genType instanceof GeneratedTransferObject gto
+                && isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, gto)) {
+            putTypeIntoImports(genType, PATTERN, imports);
         }
 
         final List<MethodSignature> methods = genType.getMethodDefinitions();
@@ -75,9 +81,8 @@ public final class GeneratorUtil {
         }
 
         // PROPERTIES
-        if (genType instanceof GeneratedTransferObject) {
-            final GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
-            final List<GeneratedProperty> properties = genTO.getProperties();
+        if (genType instanceof GeneratedTransferObject gto) {
+            final List<GeneratedProperty> properties = gto.getProperties();
             if (properties != null) {
                 for (GeneratedProperty property : properties) {
                     final Type propertyType = property.getReturnType();
@@ -131,8 +136,7 @@ public final class GeneratorUtil {
         if (!imports.containsKey(typeName)) {
             imports.put(typeName, type.getIdentifier());
         }
-        if (type instanceof ParameterizedType) {
-            final ParameterizedType paramType = (ParameterizedType) type;
+        if (type instanceof ParameterizedType paramType) {
             final Type[] params = paramType.getActualTypeArguments();
             if (params != null) {
                 for (Type param : params) {
@@ -244,8 +248,7 @@ public final class GeneratorUtil {
      */
     private static StringBuilder addActualTypeParameters(final StringBuilder builder, final Type type,
             final GeneratedType parentGenType, final Map<String, JavaTypeName> imports) {
-        if (type instanceof ParameterizedType) {
-            final ParameterizedType pType = (ParameterizedType) type;
+        if (type instanceof ParameterizedType pType) {
             final Type[] pTypes = pType.getActualTypeArguments();
             builder.append('<').append(getParameters(parentGenType, pTypes, imports)).append('>');
         }
@@ -321,4 +324,19 @@ public final class GeneratorUtil {
         }
         return propertiesOfAllParents;
     }
+
+    /**
+     * Check if the {@code type} represents non-presence container.
+     *
+     * @param type {@link GeneratedType} to be checked if represents container without presence statement.
+     * @return {@code true} if specified {@code type} is a container without presence statement,
+     *     {@code false} otherwise.
+     */
+    static boolean isNonPresenceContainer(final GeneratedType type) {
+        final var definition = type.getYangSourceDefinition();
+        return definition.isPresent()
+            && definition.orElseThrow() instanceof Single single
+            && single.getNode() instanceof ContainerSchemaNode container
+            && !container.isPresenceContainer();
+    }
 }