/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.mdsal.binding.model.util.generated.type.builder; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; import org.opendaylight.mdsal.binding.model.api.AnnotationType; import org.opendaylight.mdsal.binding.model.api.Constant; import org.opendaylight.mdsal.binding.model.api.Enumeration; import org.opendaylight.mdsal.binding.model.api.GeneratedProperty; import org.opendaylight.mdsal.binding.model.api.GeneratedType; import org.opendaylight.mdsal.binding.model.api.MethodSignature; import org.opendaylight.mdsal.binding.model.api.Type; import org.opendaylight.mdsal.binding.model.api.TypeComment; import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition; import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder; import org.opendaylight.mdsal.binding.model.api.type.builder.EnumBuilder; import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder; import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder; import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder; import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder; import org.opendaylight.mdsal.binding.model.util.AbstractBaseType; abstract class AbstractGeneratedType extends AbstractBaseType implements GeneratedType { private final Type parent; private final TypeComment comment; private final List annotations; private final List implementsTypes; private final List enumerations; private final List constants; private final List methodSignatures; private final List enclosedTypes; private final List properties; private final boolean isAbstract; private final YangSourceDefinition definition; public AbstractGeneratedType(final AbstractGeneratedTypeBuilder builder) { super(builder.getPackageName(), builder.getName()); this.parent = builder.getParent(); this.comment = builder.getComment(); this.annotations = toUnmodifiableAnnotations(builder.getAnnotations()); this.implementsTypes = makeUnmodifiable(builder.getImplementsTypes()); this.constants = makeUnmodifiable(builder.getConstants()); this.enumerations = toUnmodifiableEnumerations(builder.getEnumerations()); this.methodSignatures = toUnmodifiableMethods(builder.getMethodDefinitions()); this.enclosedTypes = toUnmodifiableEnclosedTypes(builder.getEnclosedTypes(), builder.getEnclosedTransferObjects()); this.properties = toUnmodifiableProperties(builder.getProperties()); this.isAbstract = builder.isAbstract(); this.definition = builder.getYangSourceDefinition().orElse(null); } public AbstractGeneratedType(final Type parent, final String packageName, final String name, final TypeComment comment, final List annotationBuilders, final boolean isAbstract, final List implementsTypes, final List enclosedGenTypeBuilders, final List enclosedGenTOBuilders, final List enumBuilders, final List constants, final List methodBuilders, final List propertyBuilders) { super(packageName, name); this.parent = parent; this.comment = comment; this.annotations = toUnmodifiableAnnotations(annotationBuilders); this.implementsTypes = makeUnmodifiable(implementsTypes); this.constants = makeUnmodifiable(constants); this.enumerations = toUnmodifiableEnumerations(enumBuilders); this.methodSignatures = toUnmodifiableMethods(methodBuilders); this.enclosedTypes = toUnmodifiableEnclosedTypes(enclosedGenTypeBuilders, enclosedGenTOBuilders); this.properties = toUnmodifiableProperties(propertyBuilders); this.isAbstract = isAbstract; this.definition = null; } protected static final List makeUnmodifiable(final List list) { switch (list.size()) { case 0: return Collections.emptyList(); case 1: return Collections.singletonList(list.get(0)); default: return Collections.unmodifiableList(list); } } private static List toUnmodifiableEnclosedTypes(final List enclosedGenTypeBuilders, final List enclosedGenTOBuilders) { final ArrayList enclosedTypesList = new ArrayList<>(enclosedGenTypeBuilders.size() + enclosedGenTOBuilders.size()); for (final GeneratedTypeBuilder builder : enclosedGenTypeBuilders) { if (builder != null) { enclosedTypesList.add(builder.build()); } } for (final GeneratedTOBuilder builder : enclosedGenTOBuilders) { if (builder != null) { enclosedTypesList.add(builder.build()); } } return makeUnmodifiable(enclosedTypesList); } protected static final List toUnmodifiableAnnotations( final List annotationBuilders) { final List annotationList = new ArrayList<>(annotationBuilders.size()); for (final AnnotationTypeBuilder builder : annotationBuilders) { annotationList.add(builder.build()); } return makeUnmodifiable(annotationList); } protected final List toUnmodifiableMethods(final List methodBuilders) { final List methods = new ArrayList<>(methodBuilders.size()); for (final MethodSignatureBuilder methodBuilder : methodBuilders) { methods.add(methodBuilder.toInstance(this)); } return makeUnmodifiable(methods); } protected final List toUnmodifiableEnumerations(final List enumBuilders) { final List enums = new ArrayList<>(enumBuilders.size()); for (final EnumBuilder enumBuilder : enumBuilders) { enums.add(enumBuilder.toInstance(this)); } return makeUnmodifiable(enums); } protected final List toUnmodifiableProperties(final List methodBuilders) { final List methods = new ArrayList<>(methodBuilders.size()); for (final GeneratedPropertyBuilder methodBuilder : methodBuilders) { methods.add(methodBuilder.toInstance(this)); } return makeUnmodifiable(methods); } @Override public final Type getParentType() { return this.parent; } @Override public final TypeComment getComment() { return this.comment; } @Override public final List getAnnotations() { return this.annotations; } @Override public final boolean isAbstract() { return this.isAbstract; } @Override public final List getImplements() { return this.implementsTypes; } @Override public final List getEnclosedTypes() { return this.enclosedTypes; } @Override public final List getEnumerations() { return this.enumerations; } @Override public final List getConstantDefinitions() { return this.constants; } @Override public final List getMethodDefinitions() { return this.methodSignatures; } @Override public final List getProperties() { return this.properties; } @Override public final Optional getYangSourceDefinition() { return Optional.ofNullable(definition); } @Override public String toString() { final StringBuilder builder = new StringBuilder(); builder.append("GeneratedType [packageName="); builder.append(getPackageName()); builder.append(", name="); builder.append(getName()); if (this.parent != null) { builder.append(", parent="); builder.append(this.parent.getFullyQualifiedName()); } else { builder.append(", parent=null"); } builder.append(", comment="); builder.append(this.comment); builder.append(", annotations="); builder.append(this.annotations); builder.append(", enclosedTypes="); builder.append(this.enclosedTypes); builder.append(", enumerations="); builder.append(this.enumerations); builder.append(", constants="); builder.append(this.constants); builder.append(", methodSignatures="); builder.append(this.methodSignatures); builder.append("]"); return builder.toString(); } }