Use switch expressions in mdsal-binding-model-ri 54/101354/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 30 May 2022 08:46:42 +0000 (10:46 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 30 May 2022 08:46:42 +0000 (10:46 +0200)
We can improve code layout a bit by using a switch expression.

Change-Id: I0784fa87890d81849e08f531b84b081033166c32
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-model-ri/src/main/java/org/opendaylight/mdsal/binding/model/ri/BaseYangTypes.java
binding/mdsal-binding-model-ri/src/main/java/org/opendaylight/mdsal/binding/model/ri/generated/type/builder/AbstractGeneratedType.java

index 917429337972e27f61b542c88917a3086744f3a8..d2941bc14059f0511bd8aaffc4b87afa75687268 100644 (file)
@@ -98,39 +98,23 @@ public final class BaseYangTypes {
      * @throws NullPointerException if type is null
      */
     public static Type javaTypeForYangType(final String type) {
-        switch (type) {
-            case "binary":
-                return BINARY_TYPE;
-            case "boolean":
-                return BOOLEAN_TYPE;
-            case "decimal64":
-                return DECIMAL64_TYPE;
-            case "empty":
-                return EMPTY_TYPE;
-            case "enumeration":
-                return ENUM_TYPE;
-            case "instance-identifier":
-                return INSTANCE_IDENTIFIER;
-            case "int8":
-                return INT8_TYPE;
-            case "int16":
-                return INT16_TYPE;
-            case "int32":
-                return INT32_TYPE;
-            case "int64":
-                return INT64_TYPE;
-            case "string":
-                return STRING_TYPE;
-            case "uint8":
-                return UINT8_TYPE;
-            case "uint16":
-                return UINT16_TYPE;
-            case "uint32":
-                return UINT32_TYPE;
-            case "uint64":
-                return UINT64_TYPE;
-            default:
-                return null;
-        }
+        return switch (type) {
+            case "binary" -> BINARY_TYPE;
+            case "boolean" -> BOOLEAN_TYPE;
+            case "decimal64" -> DECIMAL64_TYPE;
+            case "empty" -> EMPTY_TYPE;
+            case "enumeration" -> ENUM_TYPE;
+            case "instance-identifier" -> INSTANCE_IDENTIFIER;
+            case "int8" -> INT8_TYPE;
+            case "int16" -> INT16_TYPE;
+            case "int32" -> INT32_TYPE;
+            case "int64" -> INT64_TYPE;
+            case "string" -> STRING_TYPE;
+            case "uint8" -> UINT8_TYPE;
+            case "uint16" -> UINT16_TYPE;
+            case "uint32" -> UINT32_TYPE;
+            case "uint64" -> UINT64_TYPE;
+            default -> null;
+        };
     }
 }
index d653093647423af33d9ce824da738286a3d21fb6..d46799d6c19f890d6e6cb51635a2f5ef71277033 100644 (file)
@@ -46,16 +46,16 @@ abstract class AbstractGeneratedType extends AbstractType implements GeneratedTy
 
     AbstractGeneratedType(final AbstractGeneratedTypeBuilder<?> builder) {
         super(builder.getIdentifier());
-        this.comment = builder.getComment();
-        this.annotations = toUnmodifiableAnnotations(builder.getAnnotations());
-        this.implementsTypes = makeUnmodifiable(builder.getImplementsTypes());
-        this.constants = makeUnmodifiable(builder.getConstants());
-        this.enumerations = List.copyOf(builder.getEnumerations());
-        this.methodSignatures = toUnmodifiableMethods(builder.getMethodDefinitions());
-        this.enclosedTypes = List.copyOf(builder.getEnclosedTransferObjects());
-        this.properties = toUnmodifiableProperties(builder.getProperties());
-        this.isAbstract = builder.isAbstract();
-        this.definition = builder.getYangSourceDefinition().orElse(null);
+        comment = builder.getComment();
+        annotations = toUnmodifiableAnnotations(builder.getAnnotations());
+        implementsTypes = makeUnmodifiable(builder.getImplementsTypes());
+        constants = makeUnmodifiable(builder.getConstants());
+        enumerations = List.copyOf(builder.getEnumerations());
+        methodSignatures = toUnmodifiableMethods(builder.getMethodDefinitions());
+        enclosedTypes = List.copyOf(builder.getEnclosedTransferObjects());
+        properties = toUnmodifiableProperties(builder.getProperties());
+        isAbstract = builder.isAbstract();
+        definition = builder.getYangSourceDefinition().orElse(null);
     }
 
     AbstractGeneratedType(final JavaTypeName identifier, final TypeComment comment,
@@ -66,37 +66,31 @@ abstract class AbstractGeneratedType extends AbstractType implements GeneratedTy
             final List<GeneratedPropertyBuilder> propertyBuilders) {
         super(identifier);
         this.comment = comment;
-        this.annotations = toUnmodifiableAnnotations(annotationBuilders);
+        annotations = toUnmodifiableAnnotations(annotationBuilders);
         this.implementsTypes = makeUnmodifiable(implementsTypes);
         this.constants = makeUnmodifiable(constants);
-        this.enumerations = toUnmodifiableEnumerations(enumBuilders);
-        this.methodSignatures = toUnmodifiableMethods(methodBuilders);
-        this.enclosedTypes = toUnmodifiableEnclosedTypes(enclosedGenTypeBuilders, enclosedGenTOBuilders);
-        this.properties = toUnmodifiableProperties(propertyBuilders);
+        enumerations = toUnmodifiableEnumerations(enumBuilders);
+        methodSignatures = toUnmodifiableMethods(methodBuilders);
+        enclosedTypes = toUnmodifiableEnclosedTypes(enclosedGenTypeBuilders, enclosedGenTOBuilders);
+        properties = toUnmodifiableProperties(propertyBuilders);
         this.isAbstract = isAbstract;
-        this.definition = null;
+        definition = null;
     }
 
     protected static final <T> List<T> makeUnmodifiable(final List<T> list) {
-        switch (list.size()) {
-            case 0:
-                return Collections.emptyList();
-            case 1:
-                return Collections.singletonList(list.get(0));
-            default:
-                return Collections.unmodifiableList(list);
-        }
+        return switch (list.size()) {
+            case 0 -> Collections.emptyList();
+            case 1 -> Collections.singletonList(list.get(0));
+            default -> Collections.unmodifiableList(list);
+        };
     }
 
     protected static <T> Set<T> makeUnmodifiable(final Set<T> set) {
-        switch (set.size()) {
-            case 0:
-                return Collections.emptySet();
-            case 1:
-                return Collections.singleton(set.iterator().next());
-            default:
-                return Collections.unmodifiableSet(set);
-        }
+        return switch (set.size()) {
+            case 0 -> Collections.emptySet();
+            case 1 -> Collections.singleton(set.iterator().next());
+            default -> Collections.unmodifiableSet(set);
+        };
     }
 
     private static List<GeneratedType> toUnmodifiableEnclosedTypes(
@@ -163,47 +157,47 @@ abstract class AbstractGeneratedType extends AbstractType implements GeneratedTy
 
     @Override
     public final TypeComment getComment() {
-        return this.comment;
+        return comment;
     }
 
     @Override
     public final List<AnnotationType> getAnnotations() {
-        return this.annotations;
+        return annotations;
     }
 
     @Override
     public final boolean isAbstract() {
-        return this.isAbstract;
+        return isAbstract;
     }
 
     @Override
     public final List<Type> getImplements() {
-        return this.implementsTypes;
+        return implementsTypes;
     }
 
     @Override
     public final List<GeneratedType> getEnclosedTypes() {
-        return this.enclosedTypes;
+        return enclosedTypes;
     }
 
     @Override
     public final List<Enumeration> getEnumerations() {
-        return this.enumerations;
+        return enumerations;
     }
 
     @Override
     public final List<Constant> getConstantDefinitions() {
-        return this.constants;
+        return constants;
     }
 
     @Override
     public final List<MethodSignature> getMethodDefinitions() {
-        return this.methodSignatures;
+        return methodSignatures;
     }
 
     @Override
     public final List<GeneratedProperty> getProperties() {
-        return this.properties;
+        return properties;
     }
 
     @Override