Use instanceof pattern match 02/101802/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Jul 2022 20:19:50 +0000 (22:19 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Jul 2022 20:19:50 +0000 (22:19 +0200)
We can be more expressive now that we have Java 17, let's do that.

Change-Id: I669fe4bd871c9cc269ead6b3b376af21295efc4e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/AbstractJavaGeneratedType.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGeneratedProperty.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGenerator.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ByTypeMemberComparator.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/EnumGenerator.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/GeneratorUtil.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/InterfaceGenerator.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/JavaFileTemplate.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/TOGenerator.java
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/TypeUtils.java

index 63ecbd0dd1a809f2e870bdb9a30586fed5e14906..7d0e3170ca3ed22b01c275b3af03e879ebdfefc4 100644 (file)
@@ -51,8 +51,8 @@ abstract class AbstractJavaGeneratedType {
         enclosedTypes = b.build();
 
         final Set<String> cb = new HashSet<>();
-        if (genType instanceof Enumeration) {
-            ((Enumeration) genType).getValues().stream().map(Pair::getMappedName).forEach(cb::add);
+        if (genType instanceof Enumeration enumeration) {
+            enumeration.getValues().stream().map(Pair::getMappedName).forEach(cb::add);
         }
         // TODO: perhaps we can do something smarter to actually access the types
         collectAccessibleTypes(cb, genType);
@@ -62,8 +62,7 @@ abstract class AbstractJavaGeneratedType {
 
     private void collectAccessibleTypes(final Set<String> set, final GeneratedType type) {
         for (Type impl : type.getImplements()) {
-            if (impl instanceof GeneratedType) {
-                final GeneratedType genType = (GeneratedType) impl;
+            if (impl instanceof GeneratedType genType) {
                 for (GeneratedType inner : Iterables.concat(genType.getEnclosedTypes(), genType.getEnumerations())) {
                     set.add(inner.getIdentifier().simpleName());
                 }
@@ -81,9 +80,8 @@ abstract class AbstractJavaGeneratedType {
     }
 
     private String annotateReference(final String ref, final Type type, final String annotation) {
-        if (type instanceof ParameterizedType) {
-            return getReferenceString(annotate(ref, annotation), type,
-                ((ParameterizedType) type).getActualTypeArguments());
+        if (type instanceof ParameterizedType parameterized) {
+            return getReferenceString(annotate(ref, annotation), type, parameterized.getActualTypeArguments());
         }
         return "byte[]".equals(ref) ? "byte @" + annotation + "[]" : annotate(ref, annotation).toString();
     }
@@ -94,8 +92,8 @@ abstract class AbstractJavaGeneratedType {
 
     final String getReferenceString(final Type type) {
         final String ref = getReferenceString(type.getIdentifier());
-        return type instanceof ParameterizedType ? getReferenceString(new StringBuilder(ref), type,
-            ((ParameterizedType) type).getActualTypeArguments())
+        return type instanceof ParameterizedType parameterized
+            ? getReferenceString(new StringBuilder(ref), type, parameterized.getActualTypeArguments())
                 : ref;
     }
 
index a9abd4b05f145ad1d28eea68408d31b2efd0c1ff..4c145796d5fbe9d935ab5a4764d49672e3aca105 100644 (file)
@@ -52,14 +52,8 @@ final class BuilderGeneratedProperty implements GeneratedProperty {
 
     @Override
     public boolean equals(final Object obj) {
-        if (obj == this) {
-            return true;
-        }
-        if (!(obj instanceof BuilderGeneratedProperty)) {
-            return false;
-        }
-        final BuilderGeneratedProperty other = (BuilderGeneratedProperty) obj;
-        return name.equals(other.name) && getter.equals(other.getter);
+        return obj == this || obj instanceof BuilderGeneratedProperty other
+            && name.equals(other.name) && getter.equals(other.getter);
     }
 
     @Override
index d92004820c436996f9c7ba423fe41d9b634749a4..b031a957617a917a4fe0c555637dd13fab63c889 100644 (file)
@@ -36,8 +36,8 @@ public final class BuilderGenerator implements CodeGenerator {
      */
     @Override
     public boolean isAcceptable(final Type type) {
-        if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
-            for (Type t : ((GeneratedType) type).getImplements()) {
+        if (type instanceof GeneratedType generated && !(type instanceof GeneratedTransferObject)) {
+            for (Type t : generated.getImplements()) {
                 // "rpc" and "grouping" elements do not implement Augmentable
                 final JavaTypeName name = t.getIdentifier();
                 if (name.equals(AUGMENTABLE) || name.equals(AUGMENTATION)) {
@@ -54,8 +54,8 @@ public final class BuilderGenerator implements CodeGenerator {
      */
     @Override
     public String generate(final Type type) {
-        if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
-            return templateForType((GeneratedType) type).generate();
+        if (type instanceof GeneratedType generated && !(type instanceof GeneratedTransferObject)) {
+            return templateForType(generated).generate();
         }
         return "";
     }
index 2e11f12bfdb35d62f1ad745d918906f129c529eb..c28741b8600be7a9c95f7297f9a06e6185852081 100644 (file)
@@ -118,10 +118,10 @@ final class ByTypeMemberComparator<T extends TypeMember> implements Comparator<T
     private static Type getConcreteType(final Type type) {
         if (type instanceof ConcreteType) {
             return type;
-        } else if (type instanceof ParameterizedType) {
-            return ((ParameterizedType) type).getRawType();
-        } else if (type instanceof GeneratedTransferObject) {
-            GeneratedTransferObject rootGto = (GeneratedTransferObject) type;
+        } else if (type instanceof ParameterizedType generated) {
+            return generated.getRawType();
+        } else if (type instanceof GeneratedTransferObject gto) {
+            GeneratedTransferObject rootGto = gto;
             while (rootGto.getSuperType() != null) {
                 rootGto = rootGto.getSuperType();
             }
@@ -144,8 +144,8 @@ final class ByTypeMemberComparator<T extends TypeMember> implements Comparator<T
         if (type.equals(BindingTypes.INSTANCE_IDENTIFIER) || type.equals(BindingTypes.KEYED_INSTANCE_IDENTIFIER)) {
             return RANK_INSTANCE_IDENTIFIER;
         }
-        if (type instanceof GeneratedTransferObject) {
-            final TypeDefinition<?> typedef = topParentTransportObject((GeneratedTransferObject) type).getBaseType();
+        if (type instanceof GeneratedTransferObject gto) {
+            final TypeDefinition<?> typedef = topParentTransportObject(gto).getBaseType();
             if (typedef instanceof BitsTypeDefinition) {
                 return RANK_VARIABLE_ARRAY;
             }
index 3f9efcb8f55546e42d2808dafda2e03b4a240d26..a1615ad78abc27a5f94baf4eb2c59dc680b144ca 100644 (file)
@@ -27,8 +27,7 @@ public class EnumGenerator implements CodeGenerator {
      */
     @Override
     public String generate(final Type type) {
-        if (type instanceof Enumeration) {
-            final Enumeration enums = (Enumeration) type;
+        if (type instanceof Enumeration enums) {
             final EnumTemplate enumTemplate = new EnumTemplate(enums);
             return enumTemplate.generate();
         }
index 48ff6c3957ab40a3f97b87c29c75e6034904fe66..88c5ab194dd761584e1157f1e5c1c522bea34851 100644 (file)
@@ -58,8 +58,8 @@ public final class GeneratorUtil {
         }
 
         // REGULAR EXPRESSION
-        if (genType instanceof GeneratedTransferObject
-                && isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {
+        if (genType instanceof GeneratedTransferObject gto
+                && isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, gto)) {
             putTypeIntoImports(genType, PATTERN, imports);
         }
 
@@ -79,9 +79,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();
@@ -135,8 +134,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) {
@@ -248,8 +246,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('>');
         }
index 6b8a0761203116e327789b9db6c2d335b0060662..f6eff74e54a0789991dd8ca78c5ff81b37efffe4 100644 (file)
@@ -16,7 +16,7 @@ import org.opendaylight.mdsal.binding.model.api.Type;
 public final class InterfaceGenerator implements CodeGenerator {
 
     @Override
-    public boolean isAcceptable(Type type) {
+    public boolean isAcceptable(final Type type) {
         return type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)
                 && !(type instanceof Enumeration);
     }
@@ -27,9 +27,8 @@ public final class InterfaceGenerator implements CodeGenerator {
      * written in XTEND language.
      */
     @Override
-    public String generate(Type type) {
-        if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
-            final GeneratedType genType = (GeneratedType) type;
+    public String generate(final Type type) {
+        if (type instanceof GeneratedType genType && !(type instanceof GeneratedTransferObject)) {
             final InterfaceTemplate interfaceTemplate = new InterfaceTemplate(genType);
             return interfaceTemplate.generate();
         }
@@ -37,8 +36,7 @@ public final class InterfaceGenerator implements CodeGenerator {
     }
 
     @Override
-    public String getUnitName(Type type) {
+    public String getUnitName(final Type type) {
         return type.getName();
     }
-
 }
index 845545b50629402dd9a9750abfafda1911061e09..60ffe7f2ec08f4093478ca5cf83ed41aece282be 100644 (file)
@@ -199,7 +199,7 @@ class JavaFileTemplate {
     }
 
     final @NonNull String importedJavadocName(final @NonNull Type intype) {
-        return importedName(intype instanceof ParameterizedType ? ((ParameterizedType) intype).getRawType() : intype);
+        return importedName(intype instanceof ParameterizedType parameterized ? parameterized.getRawType() : intype);
     }
 
     final @NonNull String importedName(final @NonNull Type intype) {
@@ -241,11 +241,10 @@ class JavaFileTemplate {
     }
 
     final CharSequence generateInnerClass(final GeneratedType innerClass) {
-        if (!(innerClass instanceof GeneratedTransferObject)) {
+        if (!(innerClass instanceof GeneratedTransferObject gto)) {
             return "";
         }
 
-        final GeneratedTransferObject gto = (GeneratedTransferObject) innerClass;
         final NestedJavaGeneratedType innerJavaType = javaType.getEnclosedType(innerClass.getIdentifier());
         return gto.isUnionType() ? new UnionTemplate(innerJavaType, gto).generateAsInnerClass()
                 : new ClassTemplate(innerJavaType, gto).generateAsInnerClass();
@@ -329,8 +328,7 @@ class JavaFileTemplate {
 
         ParameterizedType augmentType = null;
         for (Type implementedIfc : implementedIfcs) {
-            if (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject)) {
-                final GeneratedType ifc = (GeneratedType) implementedIfc;
+            if (implementedIfc instanceof GeneratedType ifc && !(implementedIfc instanceof GeneratedTransferObject)) {
                 addImplMethods(methods, ifc);
 
                 final ParameterizedType t = collectImplementedMethods(type, methods, ifc.getImplements());
@@ -408,8 +406,8 @@ class JavaFileTemplate {
         type.getYangSourceDefinition().ifPresent(def -> {
             sb.append('\n');
 
-            if (def instanceof Single) {
-                final DocumentedNode node = ((Single) def).getNode();
+            if (def instanceof Single single) {
+                final DocumentedNode node = single.getNode();
 
                 sb.append("<p>\n")
                     .append("This class represents the following YANG schema fragment defined in module <b>")
@@ -418,8 +416,7 @@ class JavaFileTemplate {
                 appendYangSnippet(sb, def.getModule(), ((EffectiveStatement<?, ?>) node).getDeclared());
                 sb.append("</pre>");
 
-                if (node instanceof SchemaNode) {
-                    final SchemaNode schema = (SchemaNode) node;
+                if (node instanceof SchemaNode schema) {
 //                    sb.append("The schema path to identify an instance is\n");
 //                    appendPath(sb.append("<i>"), def.getModule(), schema.getPath().getPathFromRoot());
 //                    sb.append("</i>\n");
@@ -439,9 +436,9 @@ class JavaFileTemplate {
                         }
                     }
                 }
-            } else if (def instanceof Multiple) {
+            } else if (def instanceof Multiple multiple) {
                 sb.append("<pre>\n");
-                for (SchemaNode node : ((Multiple) def).getNodes()) {
+                for (SchemaNode node : multiple.getNodes()) {
                     appendYangSnippet(sb, def.getModule(), ((EffectiveStatement<?, ?>) node).getDeclared());
                 }
                 sb.append("</pre>\n");
index 7b30a6aea8bfd66546ccb6966e404cfb89c0981e..8fd35f86f166fd521a92922e45fd692c460776f7 100644 (file)
@@ -22,8 +22,7 @@ public final class TOGenerator implements CodeGenerator {
      */
     @Override
     public String generate(final Type type) {
-        if (type instanceof GeneratedTransferObject) {
-            final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
+        if (type instanceof GeneratedTransferObject genTO) {
             if (genTO.isUnionType()) {
                 final UnionTemplate template = new UnionTemplate(genTO);
                 return template.generate();
index 888fdc6ebf4271c8e69d64a59a6af313a703dbea..e2e4da7438377495f5f19da48deb881954b6df12 100644 (file)
@@ -33,8 +33,8 @@ final class TypeUtils {
      */
     static ConcreteType getBaseYangType(final @NonNull Type type) {
         // Already the correct type
-        if (type instanceof ConcreteType) {
-            return (ConcreteType) type;
+        if (type instanceof ConcreteType concrete) {
+            return concrete;
         }
 
         checkArgument(type instanceof GeneratedTransferObject, "Unsupported type %s", type);