Merge "BUG-1275: Expose XmlStreamUtils.writeValue()"
[yangtools.git] / code-generator / binding-generator-util / src / main / java / org / opendaylight / yangtools / binding / generator / util / generated / type / builder / AbstractGeneratedTypeBuilder.java
index d47757a14cee6a09d79fde284f624d9abf770ec0..b80ada0f2f5a19c32519148541c6e83dd0e7c38e 100644 (file)
@@ -7,40 +7,39 @@
  */
 package org.opendaylight.yangtools.binding.generator.util.generated.type.builder;
 
+import com.google.common.base.Preconditions;
+
+import java.util.Collections;
+import java.util.List;
+
 import org.opendaylight.yangtools.binding.generator.util.AbstractBaseType;
 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
 import org.opendaylight.yangtools.sal.binding.model.api.Type;
-import org.opendaylight.yangtools.sal.binding.model.api.type.builder.*;
-
-import java.util.ArrayList;
-import java.util.List;
-
-abstract class AbstractGeneratedTypeBuilder extends AbstractBaseType implements GeneratedTypeBuilder {
-
-    private final String packageName;
+import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
+import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
+import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
+import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
+import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
+import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
+import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
+import org.opendaylight.yangtools.util.LazyCollections;
+
+abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType implements GeneratedTypeBuilderBase<T> {
+
+    private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
+    private List<Type> implementsTypes = Collections.emptyList();
+    private List<EnumBuilder> enumDefinitions = Collections.emptyList();
+    private List<Constant> constants = Collections.emptyList();
+    private List<MethodSignatureBuilder> methodDefinitions = Collections.emptyList();
+    private final List<GeneratedTypeBuilder> enclosedTypes = Collections.emptyList();
+    private List<GeneratedTOBuilder> enclosedTransferObjects = Collections.emptyList();
+    private List<GeneratedPropertyBuilder> properties = Collections.emptyList();
     private String comment = "";
-    private final String name;
-
-    private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<>();
-    private final List<Type> implementsTypes = new ArrayList<>();
-    private final List<EnumBuilder> enumDefinitions = new ArrayList<>();
-    private final List<Constant> constants = new ArrayList<>();
-    private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<>();
-    private final List<GeneratedTypeBuilder> enclosedTypes = new ArrayList<>();
-    private final List<GeneratedTOBuilder> enclosingTransferObjects = new ArrayList<>();
     private boolean isAbstract;
 
-    public AbstractGeneratedTypeBuilder(final String packageName, final String name) {
+    protected AbstractGeneratedTypeBuilder(final String packageName, final String name) {
         super(packageName, name);
-        if (packageName == null) {
-            throw new IllegalArgumentException("Package Name for Generated Type cannot be null!");
-        }
-        if (name == null) {
-            throw new IllegalArgumentException("Name of Generated Type cannot be null!");
-        }
-        this.packageName = packageName;
-        this.name = name;
     }
 
     protected String getComment() {
@@ -51,11 +50,13 @@ abstract class AbstractGeneratedTypeBuilder extends AbstractBaseType implements
         return annotationBuilders;
     }
 
-    protected boolean isAbstract() {
+    @Override
+    public boolean isAbstract() {
         return isAbstract;
     }
 
-    protected List<Type> getImplementsTypes() {
+    @Override
+    public List<Type> getImplementsTypes() {
         return implementsTypes;
     }
 
@@ -67,7 +68,8 @@ abstract class AbstractGeneratedTypeBuilder extends AbstractBaseType implements
         return constants;
     }
 
-    protected List<MethodSignatureBuilder> getMethodDefinitions() {
+    @Override
+    public List<MethodSignatureBuilder> getMethodDefinitions() {
         return methodDefinitions;
     }
 
@@ -76,110 +78,87 @@ abstract class AbstractGeneratedTypeBuilder extends AbstractBaseType implements
     }
 
     protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
-        return enclosingTransferObjects;
+        return enclosedTransferObjects;
     }
 
-    @Override
-    public GeneratedTypeBuilder addEnclosingType(String name) {
-        if (name == null) {
-            throw new IllegalArgumentException("Name for Enclosing Generated Type cannot be null!");
-        }
-        GeneratedTypeBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
-        enclosedTypes.add(builder);
-        return builder;
-    }
+    protected abstract T thisInstance();
 
     @Override
-    public GeneratedTOBuilder addEnclosingTransferObject(String name) {
-        if (name == null) {
-            throw new IllegalArgumentException("Name for Enclosing Generated Transfer Object cannot be null!");
-        }
+    public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
+        Preconditions.checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
         GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
-        enclosingTransferObjects.add(builder);
+
+        enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, builder);
         return builder;
     }
 
     @Override
-    public void addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
-        if (genTOBuilder == null) {
-            throw new IllegalArgumentException("Parameter genTOBuilder cannot be null!");
-        }
-        enclosingTransferObjects.add(genTOBuilder);
+    public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
+        Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
+        enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, genTOBuilder);
+        return thisInstance();
     }
 
     @Override
-    public void addComment(String comment) {
+    public T addComment(final String comment) {
         this.comment = comment;
+        return thisInstance();
     }
 
     @Override
     public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
-        if (packageName == null) {
-            throw new IllegalArgumentException("Package Name for Annotation Type cannot be null!");
-        }
-        if (name == null) {
-            throw new IllegalArgumentException("Name of Annotation Type cannot be null!");
-        }
+        Preconditions.checkArgument(packageName != null, "Package Name for Annotation Type cannot be null!");
+        Preconditions.checkArgument(name != null, "Name of Annotation Type cannot be null!");
 
         final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
-        annotationBuilders.add(builder);
+        annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
         return builder;
     }
 
     @Override
-    public void setAbstract(boolean isAbstract) {
+    public T setAbstract(final boolean isAbstract) {
         this.isAbstract = isAbstract;
+        return thisInstance();
     }
 
     @Override
-    public boolean addImplementsType(Type genType) {
-        if (genType == null) {
-            throw new IllegalArgumentException("Type cannot be null");
-        }
-        return implementsTypes.add(genType);
+    public T addImplementsType(final Type genType) {
+        Preconditions.checkArgument(genType != null, "Type cannot be null");
+        implementsTypes = LazyCollections.lazyAdd(implementsTypes, genType);
+        return thisInstance();
     }
 
     @Override
-    public Constant addConstant(Type type, String name, Object value) {
-        if (type == null) {
-            throw new IllegalArgumentException("Returning Type for Constant cannot be null!");
-        }
-        if (name == null) {
-            throw new IllegalArgumentException("Name of constant cannot be null!");
-        }
+    public Constant addConstant(final Type type, final String name, final Object value) {
+        Preconditions.checkArgument(type != null, "Returning Type for Constant cannot be null!");
+        Preconditions.checkArgument(name != null, "Name of constant cannot be null!");
 
         final Constant constant = new ConstantImpl(this, type, name, value);
-        constants.add(constant);
+        constants = LazyCollections.lazyAdd(constants, constant);
         return constant;
     }
 
     @Override
-    public EnumBuilder addEnumeration(String name) {
-        if (name == null) {
-            throw new IllegalArgumentException("Name of enumeration cannot be null!");
-        }
+    public EnumBuilder addEnumeration(final String name) {
+        Preconditions.checkArgument(name != null, "Name of enumeration cannot be null!");
         final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name);
-        enumDefinitions.add(builder);
+        enumDefinitions = LazyCollections.lazyAdd(enumDefinitions, builder);
         return builder;
     }
 
     @Override
-    public MethodSignatureBuilder addMethod(String name) {
-        if (name == null) {
-            throw new IllegalArgumentException("Name of method cannot be null!");
-        }
+    public MethodSignatureBuilder addMethod(final String name) {
+        Preconditions.checkArgument(name != null, "Name of method cannot be null!");
         final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
         builder.setAccessModifier(AccessModifier.PUBLIC);
         builder.setAbstract(true);
-        methodDefinitions.add(builder);
+        methodDefinitions = LazyCollections.lazyAdd(methodDefinitions, builder);
         return builder;
     }
 
     @Override
-    public boolean containsMethod(String name) {
-        if (name == null) {
-            throw new IllegalArgumentException("Parameter name can't be null");
-        }
+    public boolean containsMethod(final String name) {
+        Preconditions.checkArgument(name != null, "Parameter name can't be null");
         for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
             if (name.equals(methodDefinition.getName())) {
                 return true;
@@ -188,6 +167,25 @@ abstract class AbstractGeneratedTypeBuilder extends AbstractBaseType implements
         return false;
     }
 
+    @Override
+    public GeneratedPropertyBuilder addProperty(final String name) {
+        final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
+        builder.setAccessModifier(AccessModifier.PUBLIC);
+        properties = LazyCollections.lazyAdd(properties, builder);
+        return builder;
+    }
+
+    @Override
+    public boolean containsProperty(final String name) {
+        Preconditions.checkArgument(name != null, "Parameter name can't be null");
+        for (GeneratedPropertyBuilder property : properties) {
+            if (name.equals(property.getName())) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -198,7 +196,7 @@ abstract class AbstractGeneratedTypeBuilder extends AbstractBaseType implements
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
@@ -208,7 +206,7 @@ abstract class AbstractGeneratedTypeBuilder extends AbstractBaseType implements
         if (getClass() != obj.getClass()) {
             return false;
         }
-        AbstractGeneratedTypeBuilder other = (AbstractGeneratedTypeBuilder) obj;
+        AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
         if (getName() == null) {
             if (other.getName() != null) {
                 return false;
@@ -225,4 +223,13 @@ abstract class AbstractGeneratedTypeBuilder extends AbstractBaseType implements
         }
         return true;
     }
+
+    public Type getParent() {
+        return null;
+    }
+
+    @Override
+    public List<GeneratedPropertyBuilder> getProperties() {
+        return properties;
+    }
 }