Added support for annotations in generated APIs.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / main / java / org / opendaylight / controller / sal / binding / generator / impl / GeneratedTypeBuilderImpl.java
index 2e4ed89c7b5003ab0a0e4ec5b8192037bd7f6288..1b8246ab2b072ef85e505eb5e9276491293f233e 100644 (file)
@@ -11,12 +11,13 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
-import org.opendaylight.controller.sal.binding.model.api.AccessModifier;
+import org.opendaylight.controller.sal.binding.model.api.AnnotationType;
 import org.opendaylight.controller.sal.binding.model.api.Constant;
 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
 import org.opendaylight.controller.sal.binding.model.api.Type;
+import org.opendaylight.controller.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
 import org.opendaylight.controller.sal.binding.model.api.type.builder.ConstantBuilder;
 import org.opendaylight.controller.sal.binding.model.api.type.builder.EnumBuilder;
 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
@@ -25,8 +26,9 @@ import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSign
 public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {
 
     private final String packageName;
-    private String comment;
+    private String comment = "";
     private final String name;
+    private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<AnnotationTypeBuilder>();
     private final List<EnumBuilder> enumDefinitions = new ArrayList<EnumBuilder>();
     private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();
     private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();
@@ -56,20 +58,31 @@ public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {
         this.comment = comment;
     }
 
+    @Override
+    public AnnotationTypeBuilder addAnnotation(String packageName, String name) {
+        if (packageName != null && name != null) {
+            final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
+                    packageName, name);
+            if (annotationBuilders.add(builder)) {
+                return builder;
+            }
+        }
+        return null;
+    }
+
     @Override
     public ConstantBuilder addConstant(Type type, String name, Object value) {
         final ConstantBuilder builder = new ConstantBuilderImpl(type, name,
                 value);
         constantDefintions.add(builder);
-
         return builder;
     }
 
     @Override
     public EnumBuilder addEnumeration(final String name) {
         final String innerPackageName = packageName + "." + this.name;
-        final EnumBuilder builder = new EnumerationBuilderImpl(innerPackageName,
-                name);
+        final EnumBuilder builder = new EnumerationBuilderImpl(
+                innerPackageName, name);
         enumDefinitions.add(builder);
         return builder;
     }
@@ -84,251 +97,9 @@ public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {
 
     @Override
     public GeneratedType toInstance() {
-        return new GeneratedTypeImpl(this, packageName, name, enumDefinitions,
-                constantDefintions, methodDefinitions);
-    }
-
-    private static final class MethodSignatureBuilderImpl implements
-            MethodSignatureBuilder {
-        private final String name;
-        private Type returnType;
-        private final List<MethodSignature.Parameter> parameters;
-        private String comment = "";
-        private final Type parent;
-
-        public MethodSignatureBuilderImpl(final Type parent, final String name) {
-            super();
-            this.name = name;
-            this.parent = parent;
-            parameters = new ArrayList<MethodSignature.Parameter>();
-            // TODO: move implementation elsewhere!
-
-        }
-
-        @Override
-        public void addReturnType(Type returnType) {
-            if (returnType != null) {
-                this.returnType = returnType;
-            }
-        }
-
-        @Override
-        public void addParameter(Type type, String name) {
-            parameters.add(new MethodParameterImpl(name, type));
-        }
-
-        @Override
-        public void addComment(String comment) {
-            this.comment = comment;
-        }
-
-        @Override
-        public MethodSignature toInstance(Type definingType) {
-            return new MethodSignatureImpl(definingType, name, comment,
-                    returnType, parameters);
-        }
-
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + ((name == null) ? 0 : name.hashCode());
-            return result;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (obj == null) {
-                return false;
-            }
-            if (getClass() != obj.getClass()) {
-                return false;
-            }
-            MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;
-            if (name == null) {
-                if (other.name != null) {
-                    return false;
-                }
-            } else if (!name.equals(other.name)) {
-                return false;
-            }
-            return true;
-        }
-
-        @Override
-        public String toString() {
-            StringBuilder builder = new StringBuilder();
-            builder.append("MethodBuilderImpl [name=");
-            builder.append(name);
-            builder.append(", returnType=");
-            builder.append(returnType);
-            builder.append(", parameters=");
-            builder.append(parameters);
-            builder.append(", comment=");
-            builder.append(comment);
-            builder.append(", parent=");
-            builder.append(parent.getName());
-            builder.append("]");
-            return builder.toString();
-        }
-
-    }
-
-    private static final class MethodSignatureImpl implements MethodSignature {
-
-        private final String name;
-        private final String comment;
-        private final Type definingType;
-        private final Type returnType;
-        private final List<Parameter> params;
-
-        public MethodSignatureImpl(final Type definingType, final String name,
-                final String comment, final Type returnType,
-                final List<Parameter> params) {
-            super();
-            this.name = name;
-            this.comment = comment;
-            this.definingType = definingType;
-            this.returnType = returnType;
-            this.params = Collections.unmodifiableList(params);
-        }
-
-        @Override
-        public String getName() {
-            return name;
-        }
-
-        @Override
-        public String getComment() {
-            return comment;
-        }
-
-        @Override
-        public Type getDefiningType() {
-            return definingType;
-        }
-
-        @Override
-        public Type getReturnType() {
-            return returnType;
-        }
-
-        @Override
-        public List<Parameter> getParameters() {
-            return params;
-        }
-
-        @Override
-        public AccessModifier getAccessModifier() {
-            return AccessModifier.PUBLIC;
-        }
-
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result
-                    + ((comment == null) ? 0 : comment.hashCode());
-            result = prime * result + ((name == null) ? 0 : name.hashCode());
-            result = prime * result
-                    + ((params == null) ? 0 : params.hashCode());
-            result = prime * result
-                    + ((returnType == null) ? 0 : returnType.hashCode());
-
-            if (definingType != null) {
-                result = prime
-                        * result
-                        + ((definingType.getPackageName() == null) ? 0
-                                : definingType.getPackageName().hashCode());
-                result = prime
-                        * result
-                        + ((definingType.getName() == null) ? 0 : definingType
-                                .getName().hashCode());
-            }
-
-            return result;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (obj == null) {
-                return false;
-            }
-            if (getClass() != obj.getClass()) {
-                return false;
-            }
-            MethodSignatureImpl other = (MethodSignatureImpl) obj;
-            if (comment == null) {
-                if (other.comment != null) {
-                    return false;
-                }
-            } else if (!comment.equals(other.comment)) {
-                return false;
-            }
-            if (name == null) {
-                if (other.name != null) {
-                    return false;
-                }
-            } else if (!name.equals(other.name)) {
-                return false;
-            }
-            if (params == null) {
-                if (other.params != null) {
-                    return false;
-                }
-            } else if (!params.equals(other.params)) {
-                return false;
-            }
-            if (definingType == null) {
-                if (other.definingType != null) {
-                    return false;
-                }
-            } else if ((definingType != null) && (other.definingType != null)) {
-                if (!definingType.getPackageName().equals(
-                        other.definingType.getPackageName())
-                        && !definingType.getName().equals(
-                                other.definingType.getName())) {
-                    return false;
-                }
-            }
-            if (returnType == null) {
-                if (other.returnType != null) {
-                    return false;
-                }
-            } else if (!returnType.equals(other.returnType)) {
-                return false;
-            }
-            return true;
-        }
-
-        @Override
-        public String toString() {
-            StringBuilder builder = new StringBuilder();
-            builder.append("MethodImpl [name=");
-            builder.append(name);
-            builder.append(", comment=");
-            builder.append(comment);
-            if (definingType != null) {
-                builder.append(", definingType=");
-                builder.append(definingType.getPackageName());
-                builder.append(".");
-                builder.append(definingType.getName());
-            } else {
-                builder.append(", definingType= null");
-            }
-            builder.append(", returnType=");
-            builder.append(returnType);
-            builder.append(", params=");
-            builder.append(params);
-            builder.append("]");
-            return builder.toString();
-        }
+        return new GeneratedTypeImpl(this, packageName, name, comment,
+                annotationBuilders, enumDefinitions, constantDefintions,
+                methodDefinitions);
     }
 
     private static final class GeneratedTypeImpl implements GeneratedType {
@@ -336,24 +107,38 @@ public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {
         private final Type parent;
         private final String packageName;
         private final String name;
+        private final String comment;
+        private final List<AnnotationType> annotations;
         private final List<Enumeration> enumDefinitions;
         private final List<Constant> constantDefintions;
         private final List<MethodSignature> methodDefinitions;
 
         public GeneratedTypeImpl(final Type parent, final String packageName,
-                final String name, final List<EnumBuilder> enumBuilders,
+                final String name, final String comment,
+                final List<AnnotationTypeBuilder> annotationBuilders,
+                final List<EnumBuilder> enumBuilders,
                 final List<ConstantBuilder> constantBuilders,
                 final List<MethodSignatureBuilder> methodBuilders) {
             super();
             this.parent = parent;
             this.packageName = packageName;
             this.name = name;
-
+            this.comment = comment;
+            this.annotations = toUnmodifiableAnnotations(annotationBuilders);
             this.constantDefintions = toUnmodifiableConstants(constantBuilders);
             this.enumDefinitions = toUnmodifiableEnums(enumBuilders);
             this.methodDefinitions = toUnmodifiableMethods(methodBuilders);
         }
 
+        private List<AnnotationType> toUnmodifiableAnnotations(
+                final List<AnnotationTypeBuilder> annotationBuilders) {
+            final List<AnnotationType> annotations = new ArrayList<AnnotationType>();
+            for (final AnnotationTypeBuilder builder : annotationBuilders) {
+                annotations.add(builder.toInstance());
+            }
+            return Collections.unmodifiableList(annotations);
+        }
+
         private List<MethodSignature> toUnmodifiableMethods(
                 List<MethodSignatureBuilder> methodBuilders) {
             final List<MethodSignature> methods = new ArrayList<MethodSignature>();
@@ -396,6 +181,16 @@ public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {
             return parent;
         }
 
+        @Override
+        public String getComment() {
+            return comment;
+        }
+
+        @Override
+        public List<AnnotationType> getAnnotations() {
+            return annotations;
+        }
+
         @Override
         public List<Enumeration> getEnumDefintions() {
             return enumDefinitions;
@@ -486,12 +281,22 @@ public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {
         @Override
         public String toString() {
             StringBuilder builder = new StringBuilder();
-            builder.append("GeneratedTypeImpl [parent=");
-            builder.append(parent.getName());
-            builder.append(", packageName=");
+            builder.append("GeneratedType [packageName=");
             builder.append(packageName);
             builder.append(", name=");
             builder.append(name);
+            if (parent != null) {
+                builder.append(", parent=");
+                builder.append(parent.getPackageName());
+                builder.append(".");
+                builder.append(parent.getName());
+            } else {
+                builder.append(", parent=null");
+            }
+            builder.append(", comment=");
+            builder.append(comment);
+            builder.append(", annotations=");
+            builder.append(annotations);
             builder.append(", enumDefinitions=");
             builder.append(enumDefinitions);
             builder.append(", constantDefintions=");