Add alternative enum assigned name mapping
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / mdsal / binding / model / util / generated / type / builder / EnumerationBuilderImpl.java
index 72ebc0487f6aa296e4372eae2c5dbd368788ce42..07a704047b5fbaf5b2b6c4adbeb5ddbb02d345b5 100644 (file)
@@ -7,11 +7,16 @@
  */
 package org.opendaylight.mdsal.binding.model.util.generated.type.builder;
 
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
+import java.util.stream.Collectors;
 import org.opendaylight.mdsal.binding.model.api.AnnotationType;
 import org.opendaylight.mdsal.binding.model.api.Constant;
 import org.opendaylight.mdsal.binding.model.api.Enumeration;
@@ -67,7 +72,7 @@ public final class EnumerationBuilderImpl extends AbstractBaseType implements En
 
     @Override
     public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
-        if ((packageName != null) && (name != null)) {
+        if (packageName != null && name != null) {
             final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
             if (!this.annotationBuilders.contains(builder)) {
                 this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
@@ -77,17 +82,17 @@ public final class EnumerationBuilderImpl extends AbstractBaseType implements En
         return null;
     }
 
-    @Override
-    public void addValue(final String name, final int value, final String description) {
-        final EnumPairImpl p = new EnumPairImpl(name, value, description);
+    @VisibleForTesting
+    void addValue(final String name, final String mappedName, final int value, final String description) {
+        final EnumPairImpl p = new EnumPairImpl(name, mappedName, value, description);
         this.values = LazyCollections.lazyAdd(this.values, p);
         this.unmodifiableValues = Collections.unmodifiableList(this.values);
     }
 
     @Override
     public Enumeration toInstance(final Type definingType) {
-        return new EnumerationImpl(definingType, this.annotationBuilders, this.packageName, this.name, this.unmodifiableValues,
-                this.description, this.reference, this.moduleName, this.schemaPath);
+        return new EnumerationImpl(definingType, this.annotationBuilders, this.packageName, this.name,
+            this.unmodifiableValues, this.description, this.reference, this.moduleName, this.schemaPath);
     }
 
     /*
@@ -111,14 +116,13 @@ public final class EnumerationBuilderImpl extends AbstractBaseType implements En
     @Override
     public void updateEnumPairsFromEnumTypeDef(final EnumTypeDefinition enumTypeDef) {
         final List<EnumPair> enums = enumTypeDef.getValues();
-        if (enums != null) {
-            for (final EnumPair enumPair : enums) {
-                if (enumPair != null) {
-                    this.addValue(enumPair.getName(), enumPair.getValue(), enumPair.getDescription());
-                }
-            }
-        }
+        final Map<String, String> valueIds = BindingMapping.mapEnumAssignedNames(enums.stream().map(EnumPair::getName)
+            .collect(Collectors.toList()));
 
+        for (EnumPair enumPair : enums) {
+            addValue(enumPair.getName(), valueIds.get(enumPair.getName()), enumPair.getValue(),
+                enumPair.getDescription());
+        }
     }
 
     private static final class EnumPairImpl implements Enumeration.Pair {
@@ -128,10 +132,9 @@ public final class EnumerationBuilderImpl extends AbstractBaseType implements En
         private final int value;
         private final String description;
 
-        public EnumPairImpl(final String name, final int value, final String description) {
-            super();
-            this.name = name;
-            this.mappedName = BindingMapping.getClassName(name);
+        EnumPairImpl(final String name, final String mappedName, final int value, final String description) {
+            this.name = requireNonNull(name);
+            this.mappedName = requireNonNull(mappedName);
             this.value = value;
             this.description = description;
         }
@@ -160,8 +163,8 @@ public final class EnumerationBuilderImpl extends AbstractBaseType implements En
         public int hashCode() {
             final int prime = 31;
             int result = 1;
-            result = (prime * result) + Objects.hashCode(this.name);
-            result = (prime * result) + Objects.hashCode(this.value);
+            result = prime * result + Objects.hashCode(this.name);
+            result = prime * result + Objects.hashCode(this.value);
             return result;
         }
 
@@ -281,7 +284,7 @@ public final class EnumerationBuilderImpl extends AbstractBaseType implements En
                 builder.append(" (");
                 builder.append(valPair.getValue());
 
-                if (i == (this.values.size() - 1)) {
+                if (i == this.values.size() - 1) {
                     builder.append(" );");
                 } else {
                     builder.append(" ),");