2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.binding.generator.util.generated.type.builder;
10 import com.google.common.base.Preconditions;
12 import java.util.Collections;
13 import java.util.List;
15 import org.opendaylight.yangtools.binding.generator.util.AbstractBaseType;
16 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
17 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
18 import org.opendaylight.yangtools.sal.binding.model.api.Type;
19 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
20 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
21 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
22 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
23 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
24 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
25 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
26 import org.opendaylight.yangtools.util.LazyCollections;
28 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType implements GeneratedTypeBuilderBase<T> {
30 private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
31 private List<Type> implementsTypes = Collections.emptyList();
32 private List<EnumBuilder> enumDefinitions = Collections.emptyList();
33 private List<Constant> constants = Collections.emptyList();
34 private List<MethodSignatureBuilder> methodDefinitions = Collections.emptyList();
35 private final List<GeneratedTypeBuilder> enclosedTypes = Collections.emptyList();
36 private List<GeneratedTOBuilder> enclosedTransferObjects = Collections.emptyList();
37 private List<GeneratedPropertyBuilder> properties = Collections.emptyList();
38 private String comment = "";
39 private boolean isAbstract;
41 protected AbstractGeneratedTypeBuilder(final String packageName, final String name) {
42 super(packageName, name);
45 protected String getComment() {
49 protected List<AnnotationTypeBuilder> getAnnotations() {
50 return annotationBuilders;
54 public boolean isAbstract() {
59 public List<Type> getImplementsTypes() {
60 return implementsTypes;
63 protected List<EnumBuilder> getEnumerations() {
64 return enumDefinitions;
67 protected List<Constant> getConstants() {
72 public List<MethodSignatureBuilder> getMethodDefinitions() {
73 return methodDefinitions;
76 protected List<GeneratedTypeBuilder> getEnclosedTypes() {
80 protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
81 return enclosedTransferObjects;
84 protected abstract T thisInstance();
87 public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
88 Preconditions.checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
89 GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
91 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, builder);
96 public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
97 Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
98 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, genTOBuilder);
99 return thisInstance();
103 public T addComment(final String comment) {
104 this.comment = comment;
105 return thisInstance();
109 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
110 Preconditions.checkArgument(packageName != null, "Package Name for Annotation Type cannot be null!");
111 Preconditions.checkArgument(name != null, "Name of Annotation Type cannot be null!");
113 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
114 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
119 public T setAbstract(final boolean isAbstract) {
120 this.isAbstract = isAbstract;
121 return thisInstance();
125 public T addImplementsType(final Type genType) {
126 Preconditions.checkArgument(genType != null, "Type cannot be null");
127 implementsTypes = LazyCollections.lazyAdd(implementsTypes, genType);
128 return thisInstance();
132 public Constant addConstant(final Type type, final String name, final Object value) {
133 Preconditions.checkArgument(type != null, "Returning Type for Constant cannot be null!");
134 Preconditions.checkArgument(name != null, "Name of constant cannot be null!");
136 final Constant constant = new ConstantImpl(this, type, name, value);
137 constants = LazyCollections.lazyAdd(constants, constant);
142 public EnumBuilder addEnumeration(final String name) {
143 Preconditions.checkArgument(name != null, "Name of enumeration cannot be null!");
144 final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name);
145 enumDefinitions = LazyCollections.lazyAdd(enumDefinitions, builder);
150 public MethodSignatureBuilder addMethod(final String name) {
151 Preconditions.checkArgument(name != null, "Name of method cannot be null!");
152 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
153 builder.setAccessModifier(AccessModifier.PUBLIC);
154 builder.setAbstract(true);
155 methodDefinitions = LazyCollections.lazyAdd(methodDefinitions, builder);
160 public boolean containsMethod(final String name) {
161 Preconditions.checkArgument(name != null, "Parameter name can't be null");
162 for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
163 if (name.equals(methodDefinition.getName())) {
171 public GeneratedPropertyBuilder addProperty(final String name) {
172 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
173 builder.setAccessModifier(AccessModifier.PUBLIC);
174 properties = LazyCollections.lazyAdd(properties, builder);
179 public boolean containsProperty(final String name) {
180 Preconditions.checkArgument(name != null, "Parameter name can't be null");
181 for (GeneratedPropertyBuilder property : properties) {
182 if (name.equals(property.getName())) {
190 public int hashCode() {
191 final int prime = 31;
193 result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
194 result = prime * result + ((getPackageName() == null) ? 0 : getPackageName().hashCode());
199 public boolean equals(final Object obj) {
206 if (getClass() != obj.getClass()) {
209 AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
210 if (getName() == null) {
211 if (other.getName() != null) {
214 } else if (!getName().equals(other.getName())) {
217 if (getPackageName() == null) {
218 if (other.getPackageName() != null) {
221 } else if (!getPackageName().equals(other.getPackageName())) {
227 public Type getParent() {
232 public List<GeneratedPropertyBuilder> getProperties() {