--- /dev/null
+/*\r
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+package org.opendaylight.controller.sal.binding.generator.impl;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.opendaylight.controller.sal.binding.model.api.AccessModifier;\r
+import org.opendaylight.controller.sal.binding.model.api.Constant;\r
+import org.opendaylight.controller.sal.binding.model.api.Enumeration;\r
+import org.opendaylight.controller.sal.binding.model.api.GeneratedType;\r
+import org.opendaylight.controller.sal.binding.model.api.MethodSignature;\r
+import org.opendaylight.controller.sal.binding.model.api.Type;\r
+import org.opendaylight.controller.sal.binding.model.api.type.builder.ConstantBuilder;\r
+import org.opendaylight.controller.sal.binding.model.api.type.builder.EnumBuilder;\r
+import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;\r
+import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSignatureBuilder;\r
+\r
+public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {\r
+\r
+ private final String packageName;\r
+ private String comment;\r
+ private final String name;\r
+ private final List<EnumBuilder> enumDefinitions = new ArrayList<EnumBuilder>();\r
+ private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();\r
+ private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();\r
+\r
+ public GeneratedTypeBuilderImpl(final String packageName, final String name) {\r
+ this.packageName = packageName;\r
+ this.name = name;\r
+ }\r
+\r
+ @Override\r
+ public Type getParentType() {\r
+ return this;\r
+ }\r
+\r
+ @Override\r
+ public String getPackageName() {\r
+ return packageName;\r
+ }\r
+\r
+ @Override\r
+ public String getName() {\r
+ return name;\r
+ }\r
+\r
+ @Override\r
+ public void addComment(String comment) {\r
+ this.comment = comment;\r
+ }\r
+\r
+ @Override\r
+ public ConstantBuilder addConstant(Type type, String name, Object value) {\r
+ final ConstantBuilder builder = new ConstantBuilderImpl(type, name,\r
+ value);\r
+ constantDefintions.add(builder);\r
+\r
+ return builder;\r
+ }\r
+\r
+ @Override\r
+ public EnumBuilder addEnumeration(final String name) {\r
+ final EnumBuilder builder = new EnumerationBuilderImpl(packageName,\r
+ name);\r
+ enumDefinitions.add(builder);\r
+ return builder;\r
+ }\r
+\r
+ @Override\r
+ public MethodSignatureBuilder addMethod(final String name) {\r
+ final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(\r
+ this, name);\r
+ methodDefinitions.add(builder);\r
+ return builder;\r
+ }\r
+\r
+ @Override\r
+ public GeneratedType toInstance() {\r
+ return new GeneratedTypeImpl(this, packageName, name, enumDefinitions,\r
+ constantDefintions, methodDefinitions);\r
+ }\r
+\r
+ private static final class MethodSignatureBuilderImpl implements\r
+ MethodSignatureBuilder {\r
+\r
+ private final String name;\r
+ private Type returnType;\r
+ private final List<MethodSignature.Parameter> parameters;\r
+ private String comment = "";\r
+ private final Type parent;\r
+\r
+ public MethodSignatureBuilderImpl(final Type parent, final String name) {\r
+ super();\r
+ this.name = name;\r
+ this.parent = parent;\r
+ parameters = new ArrayList<MethodSignature.Parameter>();\r
+ }\r
+\r
+ @Override\r
+ public void addReturnType(Type returnType) {\r
+ if (returnType != null) {\r
+ this.returnType = returnType;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void addParameter(Type type, String name) {\r
+ parameters.add(new MethodParameterImpl(name, type));\r
+ }\r
+\r
+ @Override\r
+ public void addComment(String comment) {\r
+ this.comment = comment;\r
+ }\r
+\r
+ @Override\r
+ public MethodSignature toInstance(Type definingType) {\r
+ return new MethodSignatureImpl(definingType, name, comment,\r
+ returnType, parameters);\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result + ((name == null) ? 0 : name.hashCode());\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj) {\r
+ return true;\r
+ }\r
+ if (obj == null) {\r
+ return false;\r
+ }\r
+ if (getClass() != obj.getClass()) {\r
+ return false;\r
+ }\r
+ MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;\r
+ if (name == null) {\r
+ if (other.name != null) {\r
+ return false;\r
+ }\r
+ } else if (!name.equals(other.name)) {\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ StringBuilder builder = new StringBuilder();\r
+ builder.append("MethodBuilderImpl [name=");\r
+ builder.append(name);\r
+ builder.append(", returnType=");\r
+ builder.append(returnType);\r
+ builder.append(", parameters=");\r
+ builder.append(parameters);\r
+ builder.append(", comment=");\r
+ builder.append(comment);\r
+ builder.append(", parent=");\r
+ builder.append(parent.getName());\r
+ builder.append("]");\r
+ return builder.toString();\r
+ }\r
+\r
+ }\r
+\r
+ private static final class MethodSignatureImpl implements MethodSignature {\r
+\r
+ private final String name;\r
+ private final String comment;\r
+ private final Type definingType;\r
+ private final Type returnType;\r
+ private final List<Parameter> params;\r
+\r
+ public MethodSignatureImpl(final Type definingType, final String name,\r
+ final String comment, final Type returnType,\r
+ final List<Parameter> params) {\r
+ super();\r
+ this.name = name;\r
+ this.comment = comment;\r
+ this.definingType = definingType;\r
+ this.returnType = returnType;\r
+ this.params = Collections.unmodifiableList(params);\r
+ }\r
+\r
+ @Override\r
+ public String getName() {\r
+ return name;\r
+ }\r
+\r
+ @Override\r
+ public String getComment() {\r
+ return comment;\r
+ }\r
+\r
+ @Override\r
+ public Type getDefiningType() {\r
+ return definingType;\r
+ }\r
+\r
+ @Override\r
+ public Type getReturnType() {\r
+ return returnType;\r
+ }\r
+\r
+ @Override\r
+ public List<Parameter> getParameters() {\r
+ return params;\r
+ }\r
+\r
+ @Override\r
+ public AccessModifier getAccessModifier() {\r
+ return AccessModifier.PUBLIC;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result\r
+ + ((comment == null) ? 0 : comment.hashCode());\r
+ result = prime * result + ((name == null) ? 0 : name.hashCode());\r
+ result = prime * result\r
+ + ((params == null) ? 0 : params.hashCode());\r
+ result = prime * result\r
+ + ((returnType == null) ? 0 : returnType.hashCode());\r
+\r
+ if (definingType != null) {\r
+ result = prime\r
+ * result\r
+ + ((definingType.getPackageName() == null) ? 0\r
+ : definingType.getPackageName().hashCode());\r
+ result = prime\r
+ * result\r
+ + ((definingType.getName() == null) ? 0 : definingType\r
+ .getName().hashCode());\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj) {\r
+ return true;\r
+ }\r
+ if (obj == null) {\r
+ return false;\r
+ }\r
+ if (getClass() != obj.getClass()) {\r
+ return false;\r
+ }\r
+ MethodSignatureImpl other = (MethodSignatureImpl) obj;\r
+ if (comment == null) {\r
+ if (other.comment != null) {\r
+ return false;\r
+ }\r
+ } else if (!comment.equals(other.comment)) {\r
+ return false;\r
+ }\r
+ if (name == null) {\r
+ if (other.name != null) {\r
+ return false;\r
+ }\r
+ } else if (!name.equals(other.name)) {\r
+ return false;\r
+ }\r
+ if (params == null) {\r
+ if (other.params != null) {\r
+ return false;\r
+ }\r
+ } else if (!params.equals(other.params)) {\r
+ return false;\r
+ }\r
+ if (definingType == null) {\r
+ if (other.definingType != null) {\r
+ return false;\r
+ }\r
+ } else if ((definingType != null) && (other.definingType != null)) {\r
+ if (!definingType.getPackageName().equals(\r
+ other.definingType.getPackageName())\r
+ && !definingType.getName().equals(\r
+ other.definingType.getName())) {\r
+ return false;\r
+ }\r
+ }\r
+ if (returnType == null) {\r
+ if (other.returnType != null) {\r
+ return false;\r
+ }\r
+ } else if (!returnType.equals(other.returnType)) {\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ StringBuilder builder = new StringBuilder();\r
+ builder.append("MethodImpl [name=");\r
+ builder.append(name);\r
+ builder.append(", comment=");\r
+ builder.append(comment);\r
+ if (definingType != null) {\r
+ builder.append(", definingType=");\r
+ builder.append(definingType.getPackageName());\r
+ builder.append(".");\r
+ builder.append(definingType.getName());\r
+ } else {\r
+ builder.append(", definingType= null");\r
+ }\r
+ builder.append(", returnType=");\r
+ builder.append(returnType);\r
+ builder.append(", params=");\r
+ builder.append(params);\r
+ builder.append("]");\r
+ return builder.toString();\r
+ }\r
+ }\r
+\r
+ private static final class GeneratedTypeImpl implements GeneratedType {\r
+\r
+ private final Type parent;\r
+ private final String packageName;\r
+ private final String name;\r
+ private final List<Enumeration> enumDefinitions;\r
+ private final List<Constant> constantDefintions;\r
+ private final List<MethodSignature> methodDefinitions;\r
+\r
+ public GeneratedTypeImpl(final Type parent, final String packageName,\r
+ final String name, final List<EnumBuilder> enumBuilders,\r
+ final List<ConstantBuilder> constantBuilders,\r
+ final List<MethodSignatureBuilder> methodBuilders) {\r
+ super();\r
+ this.parent = parent;\r
+ this.packageName = packageName;\r
+ this.name = name;\r
+\r
+ this.constantDefintions = toUnmodifiableConstants(constantBuilders);\r
+ this.enumDefinitions = toUnmodifiableEnums(enumBuilders);\r
+ this.methodDefinitions = toUnmodifiableMethods(methodBuilders);\r
+ }\r
+\r
+ private List<MethodSignature> toUnmodifiableMethods(\r
+ List<MethodSignatureBuilder> methodBuilders) {\r
+ final List<MethodSignature> methods = new ArrayList<MethodSignature>();\r
+ for (final MethodSignatureBuilder methodBuilder : methodBuilders) {\r
+ methods.add(methodBuilder.toInstance(this));\r
+ }\r
+ return Collections.unmodifiableList(methods);\r
+ }\r
+\r
+ private List<Enumeration> toUnmodifiableEnums(\r
+ List<EnumBuilder> enumBuilders) {\r
+ final List<Enumeration> enums = new ArrayList<Enumeration>();\r
+ for (final EnumBuilder enumBuilder : enumBuilders) {\r
+ enums.add(enumBuilder.toInstance(this));\r
+ }\r
+ return Collections.unmodifiableList(enums);\r
+ }\r
+\r
+ private List<Constant> toUnmodifiableConstants(\r
+ List<ConstantBuilder> constantBuilders) {\r
+ final List<Constant> constants = new ArrayList<Constant>();\r
+ for (final ConstantBuilder enumBuilder : constantBuilders) {\r
+ constants.add(enumBuilder.toInstance(this));\r
+ }\r
+ return Collections.unmodifiableList(constants);\r
+ }\r
+\r
+ @Override\r
+ public String getPackageName() {\r
+ return packageName;\r
+ }\r
+\r
+ @Override\r
+ public String getName() {\r
+ return name;\r
+ }\r
+\r
+ @Override\r
+ public Type getParentType() {\r
+ return parent;\r
+ }\r
+\r
+ @Override\r
+ public List<Enumeration> getEnumDefintions() {\r
+ return enumDefinitions;\r
+ }\r
+\r
+ @Override\r
+ public List<Constant> getConstantDefinitions() {\r
+ return constantDefintions;\r
+ }\r
+\r
+ @Override\r
+ public List<MethodSignature> getMethodDefinitions() {\r
+ return methodDefinitions;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime\r
+ * result\r
+ + ((constantDefintions == null) ? 0 : constantDefintions\r
+ .hashCode());\r
+ result = prime\r
+ * result\r
+ + ((enumDefinitions == null) ? 0 : enumDefinitions\r
+ .hashCode());\r
+ result = prime\r
+ * result\r
+ + ((methodDefinitions == null) ? 0 : methodDefinitions\r
+ .hashCode());\r
+ result = prime * result + ((name == null) ? 0 : name.hashCode());\r
+ result = prime * result\r
+ + ((packageName == null) ? 0 : packageName.hashCode());\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj) {\r
+ return true;\r
+ }\r
+ if (obj == null) {\r
+ return false;\r
+ }\r
+ if (getClass() != obj.getClass()) {\r
+ return false;\r
+ }\r
+ GeneratedTypeImpl other = (GeneratedTypeImpl) obj;\r
+ if (constantDefintions == null) {\r
+ if (other.constantDefintions != null) {\r
+ return false;\r
+ }\r
+ } else if (!constantDefintions.equals(other.constantDefintions)) {\r
+ return false;\r
+ }\r
+ if (enumDefinitions == null) {\r
+ if (other.enumDefinitions != null) {\r
+ return false;\r
+ }\r
+ } else if (!enumDefinitions.equals(other.enumDefinitions)) {\r
+ return false;\r
+ }\r
+ if (methodDefinitions == null) {\r
+ if (other.methodDefinitions != null) {\r
+ return false;\r
+ }\r
+ } else if (!methodDefinitions.equals(other.methodDefinitions)) {\r
+ return false;\r
+ }\r
+ if (name == null) {\r
+ if (other.name != null) {\r
+ return false;\r
+ }\r
+ } else if (!name.equals(other.name)) {\r
+ return false;\r
+ }\r
+ if (packageName == null) {\r
+ if (other.packageName != null) {\r
+ return false;\r
+ }\r
+ } else if (!packageName.equals(other.packageName)) {\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ StringBuilder builder = new StringBuilder();\r
+ builder.append("GeneratedTypeImpl [parent=");\r
+ builder.append(parent.getName());\r
+ builder.append(", packageName=");\r
+ builder.append(packageName);\r
+ builder.append(", name=");\r
+ builder.append(name);\r
+ builder.append(", enumDefinitions=");\r
+ builder.append(enumDefinitions);\r
+ builder.append(", constantDefintions=");\r
+ builder.append(constantDefintions);\r
+ builder.append(", methodDefinitions=");\r
+ builder.append(methodDefinitions);\r
+ builder.append("]");\r
+ return builder.toString();\r
+ }\r
+ }\r
+}\r