From: Martin Ciglan Date: Fri, 10 Jun 2016 07:39:15 +0000 (+0200) Subject: Bug 1411-3: MDSAL Binding2 Generator Util X-Git-Tag: release/boron~3 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=mdsal.git;a=commitdiff_plain;h=648acd938a955f8b1d4c00c59d1654dbe63c791d Bug 1411-3: MDSAL Binding2 Generator Util - general util classes & methods - generated type & builder classes - Types & BindingTypes - Annotations, Enumeration - Method signature support - Deviation/AnnotationType issue fixed - Objects.hash() used in hashCode() methods - review comments fixed FIXME/TODO: Binding2GeneratorUtil - implement rest of static methods, colon-dash issue Binding2Mapping - implement rest of static methods, underscore issue Change-Id: I9f602a893f6cbd2e9e30c6ca6dc3810e0c678d89 Signed-off-by: Martin Ciglan (cherry picked from commit b798df97a01fdd0ac78d1d57a24ebadc7b38bed3) --- diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTemplate.xtend b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTemplate.xtend index 0814a898ab..322d335f65 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTemplate.xtend +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/YangTemplate.xtend @@ -82,11 +82,11 @@ class YangTemplate { «ENDIF» ''' } - + def static String generateYangSnipet(Set nodes) { if (nodes.nullOrEmpty) return '' - + ''' «FOR node : nodes» «IF node instanceof NotificationDefinition» diff --git a/binding2/mdsal-binding2-generator-api/src/main/java/org/opendaylight/mdsal/binding2/model/api/ParametrizedType.java b/binding2/mdsal-binding2-generator-api/src/main/java/org/opendaylight/mdsal/binding2/model/api/ParametrizedType.java deleted file mode 100644 index 58c08b0ba5..0000000000 --- a/binding2/mdsal-binding2-generator-api/src/main/java/org/opendaylight/mdsal/binding2/model/api/ParametrizedType.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2016 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.binding2.model.api; - -import com.google.common.annotations.Beta; - -/** - * Represents an instance of simple parametrized type such as List<String>. - * - * The parametrized Type is designed to be used to store information of Java - * Generic Type. The array of {@link #getActualTypeArguments()} holds - * information of all generic parameters defined for Parameterized Type. - */ -@Beta -public interface ParametrizedType extends Type { - - /** - * Returns array of Types that are defined for Parameterized Type. - *
- * (for example if ParameterizedType encapsulates java generic Map that - * specifies two parameters Map<K,V> and the K is java.lang.Integer and V - * is defined as GeneratedType the array will contain two Types to store - * the information of generic parameters.) - * - * @return array of Types that are defined for Parameterized Type. - */ - Type[] getActualTypeArguments(); - - /** - * Returns the Raw Type definition of Parameterized Type. - * - * @return the Raw Type definition of Parameterized Type. - */ - Type getRawType(); -} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/AbstractBaseType.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/AbstractBaseType.java new file mode 100644 index 0000000000..3e0c205fba --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/AbstractBaseType.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import java.util.Objects; +import org.opendaylight.mdsal.binding2.model.api.Type; + +/** + * This class represents ancestor for other Types + */ +@Beta +abstract public class AbstractBaseType implements Type { + + /** + * Name of the package to which this Type belongs. + */ + private final String packageName; + + /** + * Name of this Type. + */ + private final String name; + + /** + * Constructs the instance of this class with the concrete package name type + * name. + * + * @param pkName + * string with the package name to which this Type + * belongs + * @param name + * string with the name for this Type + */ + protected AbstractBaseType(final String pkName, final String name) { + this.packageName = Preconditions.checkNotNull(pkName, "Package Name for Generated Type cannot be null!"); + this.name = Preconditions.checkNotNull(name, "Name of Generated Type cannot be null!"); + } + + @Override + public int hashCode() { + return Objects.hash(name, packageName); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Type other = (Type) obj; + return Objects.equals(this, other); + } + + + @Override + public String toString() { + if (packageName.isEmpty()) { + return "Type (" + name + ")"; + } + return "Type (" + packageName + "." + name + ")"; + } + + @Override + public String getPackageName() { + return packageName; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getFullyQualifiedName() { + if (packageName.isEmpty()) { + return name; + } else { + return packageName + "." + name; + } + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/Binding2GeneratorUtil.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/Binding2GeneratorUtil.java new file mode 100644 index 0000000000..e3a70aeb09 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/Binding2GeneratorUtil.java @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util; + +import com.google.common.annotations.Beta; +import com.google.common.base.CharMatcher; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import org.opendaylight.mdsal.binding2.model.api.Restrictions; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.model.api.type.builder.TypeMemberBuilder; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; +import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; +import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; + +/** + * Standard Util class that contains various method for converting + * input strings to valid JAVA language strings e.g. package names, + * class names, attribute names and/or valid JavaDoc comments. + */ +@Beta +public final class Binding2GeneratorUtil { + + private Binding2GeneratorUtil() { + throw new UnsupportedOperationException("Utility class"); + } + + private static final CharMatcher GT_MATCHER = CharMatcher.is('>'); + private static final CharMatcher LT_MATCHER = CharMatcher.is('<'); + + private static final Restrictions EMPTY_RESTRICTIONS = new Restrictions() { + @Override + public List getLengthConstraints() { + return ImmutableList.of(); + } + + @Override + public List getPatternConstraints() { + return ImmutableList.of(); + } + + @Override + public List getRangeConstraints() { + return ImmutableList.of(); + } + + @Override + public boolean isEmpty() { + return true; + } + }; + + private static final Comparator> SUID_MEMBER_COMPARATOR = + (o1, o2) -> o1.getName().compareTo(o2.getName()); + + private static final Comparator SUID_NAME_COMPARATOR = + (o1, o2) -> o1.getFullyQualifiedName().compareTo(o2.getFullyQualifiedName()); + + /** + * Creates package name from specified basePackageName (package + * name for module) and schemaPath. + * + * Resulting package name is concatenation of basePackageName + * and all local names of YANG nodes which are parents of some node for + * which schemaPath is specified. + * + * Based on type of node, there is also possible suffix added in order + * to prevent package name conflicts. + * + * @param basePackageName + * string with package name of the module, MUST be normalized, + * otherwise this method may return an invalid string. + * @param schemaPath + * list of names of YANG nodes which are parents of some node + + * name of this node + * @return string with valid JAVA package name + * @throws NullPointerException if any of the arguments are null + */ + public static String packageNameForGeneratedType(final String basePackageName, final SchemaPath schemaPath) { + final Iterable pathTowardsRoot = schemaPath.getPathTowardsRoot(); + final Iterable pathFromRoot = schemaPath.getPathFromRoot(); + final int size = Iterables.size(pathTowardsRoot) - 1; + if (size <= 0) { + return basePackageName; + } + + return generateNormalizedPackageName(basePackageName, pathFromRoot, size); + } + + /** + * Creates package name from specified basePackageName (package + * name for module) and schemaPath which crosses an augmentation. + * + * Resulting package name is concatenation of basePackageName + * and all local names of YANG nodes which are parents of some node for + * which schemaPath is specified. + * + * Based on type of node, there is also possible suffix added in order + * to prevent package name conflicts. + * + * @param basePackageName + * string with package name of the module, MUST be normalized, + * otherwise this method may return an invalid string. + * @param schemaPath + * list of names of YANG nodes which are parents of some node + + * name of this node + * @return string with valid JAVA package name + * @throws NullPointerException if any of the arguments are null + */ + public static String packageNameForAugmentedGeneratedType(final String basePackageName, final SchemaPath schemaPath) { + final Iterable pathTowardsRoot = schemaPath.getPathTowardsRoot(); + final Iterable pathFromRoot = schemaPath.getPathFromRoot(); + final int size = Iterables.size(pathTowardsRoot); + if (size == 0) { + return basePackageName; + } + + return generateNormalizedPackageName(basePackageName, pathFromRoot, size); + } + + + private static String generateNormalizedPackageName(final String base, final Iterable path, final int size) { + final StringBuilder builder = new StringBuilder(base); + final Iterator iterator = path.iterator(); + for (int i = 0; i < size; ++i) { + builder.append('.'); + String nodeLocalName = iterator.next().getLocalName(); + //FIXME: colon or dash in identifier? + builder.append(nodeLocalName); + } + return Binding2Mapping.normalizePackageName(builder.toString()); + } + + /** + * Encodes angle brackets in yang statement description + * @param description description of a yang statement which is used to generate javadoc comments + * @return string with encoded angle brackets + */ + public static String encodeAngleBrackets(String description) { + if (description != null) { + description = LT_MATCHER.replaceFrom(description, "<"); + description = GT_MATCHER.replaceFrom(description, ">"); + } + return description; + } + + //TODO: further implementation of static util methods... +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/Binding2Mapping.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/Binding2Mapping.java new file mode 100644 index 0000000000..1013421bb2 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/Binding2Mapping.java @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.annotations.Beta; +import com.google.common.base.CharMatcher; +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; +import java.text.SimpleDateFormat; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.opendaylight.yangtools.concepts.SemVer; +import org.opendaylight.yangtools.yang.model.api.Module; + +/** + * Standard Util class that provides generated Java related functionality + */ +@Beta +public final class Binding2Mapping { + + private Binding2Mapping() { + throw new UnsupportedOperationException("Utility class"); + } + + public static final Set JAVA_RESERVED_WORDS = ImmutableSet.of("abstract", "assert", "boolean", "break", + "byte", "case", "catch", "char", "class", "const", "continue", "default", "double", "do", "else", "enum", + "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", + "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", + "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", + "true", "try", "void", "volatile", "while"); + + public static final String QNAME_STATIC_FIELD_NAME = "QNAME"; + public static final String PACKAGE_PREFIX = "org.opendaylight.yang.gen.v2"; + + private static final Splitter DOT_SPLITTER = Splitter.on('.'); + private static final Interner PACKAGE_INTERNER = Interners.newWeakInterner(); + private static final Splitter CAMEL_SPLITTER = Splitter.on(CharMatcher.anyOf(" _.-/").precomputed()) + .omitEmptyStrings().trimResults(); + private static final Pattern COLON_SLASH_SLASH = Pattern.compile("://", Pattern.LITERAL); + private static final String QUOTED_DOT = Matcher.quoteReplacement("."); + + private static final ThreadLocal PACKAGE_DATE_FORMAT = new ThreadLocal() { + + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyMMdd"); + } + + @Override + public void set(final SimpleDateFormat value) { + throw new UnsupportedOperationException(); + } + }; + + public static String getRootPackageName(final Module module) { + checkArgument(module != null, "Module must not be null"); + checkArgument(module.getRevision() != null, "Revision must not be null"); + checkArgument(module.getNamespace() != null, "Namespace must not be null"); + + final StringBuilder packageNameBuilder = new StringBuilder(); + packageNameBuilder.append(PACKAGE_PREFIX); + packageNameBuilder.append('.'); + + String namespace = module.getNamespace().toString(); + namespace = COLON_SLASH_SLASH.matcher(namespace).replaceAll(QUOTED_DOT); + + final char[] chars = namespace.toCharArray(); + for (int i = 0; i < chars.length; ++i) { + switch (chars[i]) { + case '/': + case ':': + case '-': + case '@': + case '$': + case '#': + case '\'': + case '*': + case '+': + case ',': + case ';': + case '=': + chars[i] = '.'; + } + } + + packageNameBuilder.append(chars); + if (chars[chars.length - 1] != '.') { + packageNameBuilder.append('.'); + } + + final SemVer semVer = module.getSemanticVersion(); + if (semVer != null) { + packageNameBuilder.append(semVer.toString()); + } else { + packageNameBuilder.append("rev"); + packageNameBuilder.append(PACKAGE_DATE_FORMAT.get().format(module.getRevision())); + } + return normalizePackageName(packageNameBuilder.toString()); + } + + public static String normalizePackageName(final String packageName) { + if (packageName == null) { + return null; + } + + final StringBuilder builder = new StringBuilder(); + boolean first = true; + + for (String p : DOT_SPLITTER.split(packageName.toLowerCase())) { + if (first) { + first = false; + } else { + builder.append('.'); + } + + //FIXME: don't use underscore in v2 + if (Character.isDigit(p.charAt(0)) || Binding2Mapping.JAVA_RESERVED_WORDS.contains(p)) { + builder.append('_'); + } + builder.append(p); + } + + // Prevent duplication of input string + return PACKAGE_INTERNER.intern(builder.toString()); + } + + public static String getClassName(final String localName) { + checkArgument(localName != null, "Name should not be null."); + return toFirstUpper(toCamelCase(localName)); + } + + private static String toCamelCase(final String rawString) { + checkArgument(rawString != null, "String should not be null"); + Iterable components = CAMEL_SPLITTER.split(rawString); + StringBuilder builder = new StringBuilder(); + for (String comp : components) { + builder.append(toFirstUpper(comp)); + } + return checkNumericPrefix(builder.toString()); + } + + private static String checkNumericPrefix(final String rawString) { + if (rawString == null || rawString.isEmpty()) { + return rawString; + } + char firstChar = rawString.charAt(0); + if (firstChar >= '0' && firstChar <= '9') { + return "_" + rawString; + } else { + return rawString; + } + } + + /** + * Returns the {@link String} {@code s} with an + * {@link Character#isUpperCase(char) upper case} first character. This + * function is null-safe. + * + * @param s + * the string that should get an upper case first character. May + * be null. + * @return the {@link String} {@code s} with an upper case first character + * or null if the input {@link String} {@code s} was + * null. + */ + public static String toFirstUpper(final String s) { + if (s == null || s.length() == 0) { + return s; + } + if (Character.isUpperCase(s.charAt(0))) { + return s; + } + if (s.length() == 1) { + return s.toUpperCase(); + } + return s.substring(0, 1).toUpperCase() + s.substring(1); + } + + //TODO: further implementation of static util methods... + +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/BindingTypes.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/BindingTypes.java new file mode 100644 index 0000000000..d8eceeb082 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/BindingTypes.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util; + +import static org.opendaylight.mdsal.binding2.generator.util.Types.parameterizedTypeFor; +import static org.opendaylight.mdsal.binding2.generator.util.Types.typeForClass; + +import javax.management.NotificationListener; +import org.opendaylight.mdsal.binding2.model.api.ConcreteType; +import org.opendaylight.mdsal.binding2.model.api.ParameterizedType; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.spec.Augmentable; +import org.opendaylight.mdsal.binding2.spec.Augmentation; +import org.opendaylight.mdsal.binding2.spec.IdentifiableItem; +import org.opendaylight.mdsal.binding2.spec.InstanceIdentifier; +import org.opendaylight.mdsal.binding2.spec.Notification; +import org.opendaylight.mdsal.binding2.spec.TreeChildNode; +import org.opendaylight.mdsal.binding2.spec.TreeNode; +import org.opendaylight.mdsal.binding2.spec.TreeRoot; +import org.opendaylight.yangtools.concepts.Identifier; + +public final class BindingTypes { + + public static final ConcreteType AUGMENTABLE = typeForClass(Augmentable.class); + public static final ConcreteType AUGMENTATION = typeForClass(Augmentation.class); + public static final ConcreteType TREE_NODE = typeForClass(TreeNode.class); + public static final ConcreteType TREE_ROOT = typeForClass(TreeRoot.class); + public static final ConcreteType IDENTIFIABLE_ITEM = typeForClass(IdentifiableItem.class); + public static final ConcreteType IDENTIFIER = typeForClass(Identifier.class); + public static final ConcreteType INSTANCE_IDENTIFIER = typeForClass(InstanceIdentifier.class); + public static final ConcreteType NOTIFICATION = typeForClass(Notification.class); + public static final ConcreteType NOTIFICATION_LISTENER = typeForClass(NotificationListener.class); + + private static final ConcreteType TREE_CHILD_NODE = typeForClass(TreeChildNode.class); + + private BindingTypes() { + throw new UnsupportedOperationException("Utility class"); + } + + public static ParameterizedType augmentable(Type t) { + return parameterizedTypeFor(AUGMENTABLE, t); + } + + public static ParameterizedType treeChildNode(Type t) { + return parameterizedTypeFor(TREE_CHILD_NODE, t); + } + +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/ReferencedTypeImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/ReferencedTypeImpl.java new file mode 100644 index 0000000000..d09b108afa --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/ReferencedTypeImpl.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util; + +/** + * + * Wraps combination of packageName and name to the + * object representation + * + */ +public final class ReferencedTypeImpl extends AbstractBaseType { + + /** + * Creates instance of this class with concrete package name and type name + * + * @param packageName + * string with the package name + * @param name + * string with the name for referenced type + */ + public ReferencedTypeImpl(String packageName, String name) { + super(packageName, name); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("ReferencedTypeImpl [packageName="); + builder.append(getPackageName()); + builder.append(", name="); + builder.append(getName()); + builder.append("]"); + return builder.toString(); + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/TypeConstants.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/TypeConstants.java new file mode 100644 index 0000000000..cfff9140af --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/TypeConstants.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util; + +import com.google.common.annotations.Beta; + +/** + * + * Contains constants used in relations with Type. + */ +@Beta +public final class TypeConstants { + + /** + * Name of the class constant which hold list of the regular expression + * strings. + */ + public static final String PATTERN_CONSTANT_NAME = "PATTERN_CONSTANTS"; + + /** + * Creation of new instance is prohibited. + */ + private TypeConstants() { + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/Types.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/Types.java new file mode 100644 index 0000000000..014992661a --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/Types.java @@ -0,0 +1,398 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util; + +import com.google.common.annotations.Beta; +import com.google.common.base.CharMatcher; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import javax.annotation.Nullable; +import org.opendaylight.mdsal.binding2.model.api.BaseTypeWithRestrictions; +import org.opendaylight.mdsal.binding2.model.api.ConcreteType; +import org.opendaylight.mdsal.binding2.model.api.ParameterizedType; +import org.opendaylight.mdsal.binding2.model.api.Restrictions; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.model.api.WildcardType; +import org.opendaylight.mdsal.binding2.spec.Augmentable; +import org.opendaylight.mdsal.binding2.spec.Augmentation; +import org.opendaylight.mdsal.binding2.spec.RpcCallback; +import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; +import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; +import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; +import org.opendaylight.yangtools.yang.model.util.BaseConstraints; + +@Beta +public final class Types { + + public static final ConcreteType BOOLEAN = typeForClass(Boolean.class); + public static final ConcreteType RPC_CALLBACK = typeForClass(RpcCallback.class); + public static final ConcreteType STRING = typeForClass(String.class); + public static final ConcreteType VOID = typeForClass(Void.class); + public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null); + public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null); + + private static final Splitter DOT_SPLITTER = Splitter.on('.'); + private static final Type SET_TYPE = typeForClass(Set.class); + private static final Type LIST_TYPE = typeForClass(List.class); + private static final Type MAP_TYPE = typeForClass(Map.class); + + private static final CacheLoader, ConcreteType> TYPE_LOADER = + new CacheLoader, ConcreteType>() { + + @Override + public ConcreteType load(Class key) throws Exception { + return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null); + } + }; + + private static final LoadingCache, ConcreteType> TYPE_CACHE = + CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER); + + + private Types() { + throw new UnsupportedOperationException("Utility class"); + } + + /** + * Creates the instance of type + * {@link org.opendaylight.mdsal.binding2.model.api.ConcreteType + * ConcreteType} which represents JAVA void type. + * + * @return ConcreteType instance which represents JAVA + * void + */ + public static ConcreteType voidType() { + return VOID; + } + + /** + * Creates the instance of type + * {@link org.opendaylight.mdsal.binding2.model.api.ConcreteType + * ConcreteType} which represents primitive JAVA type for which package + * doesn't exist. + * + * @param primitiveType + * string containing programmatic construction based on + * primitive type (e.g byte[]) + * @return ConcreteType instance which represents programmatic + * construction with primitive JAVA type + */ + public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) { + return new ConcreteTypeImpl("", primitiveType, restrictions); + } + + /** + * Returns an instance of {@link ConcreteType} describing the class + * + * @param cls + * Class to describe + * @return Description of class + */ + public static ConcreteType typeForClass(final Class cls) { + return TYPE_CACHE.getUnchecked(cls); + } + + + public static ConcreteType typeForClass(final Class cls, final Restrictions restrictions) { + if (restrictions != null) { + if (restrictions instanceof DefaultRestrictions) { + return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions); + } else { + return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions); + } + } else { + return typeForClass(cls); + } + } + + /** + * Returns an instance of {@link ParameterizedType} describing the typed + * {@link Map}<K,V> + * + * @param keyType + * Key Type + * @param valueType + * Value Type + * @return Description of generic type instance + */ + public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) { + return parameterizedTypeFor(MAP_TYPE, keyType, valueType); + } + + /** + * Returns an instance of {@link ParameterizedType} describing the typed + * {@link Set}<V> with concrete type of value. + * + * @param valueType + * Value Type + * @return Description of generic type instance of Set + */ + public static ParameterizedType setTypeFor(final Type valueType) { + return parameterizedTypeFor(SET_TYPE, valueType); + } + + /** + * Returns an instance of {@link ParameterizedType} describing the typed + * {@link List}<V> with concrete type of value. + * + * @param valueType + * Value Type + * @return Description of type instance of List + */ + public static ParameterizedType listTypeFor(final Type valueType) { + return parameterizedTypeFor(LIST_TYPE, valueType); + } + + /** + * Creates instance of type + * {@link org.opendaylight.mdsal.binding2.model.api.ParameterizedType + * ParameterizedType} + * + * @param type + * JAVA Type for raw type + * @param parameters + * JAVA Types for actual parameter types + * @return ParametrizedType reprezentation of type + * and its parameters parameters + */ + public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) { + return new ParameterizedTypeImpl(type, parameters); + } + + /** + * Creates instance of type + * {@link org.opendaylight.mdsal.binding2.model.api.WildcardType + * WildcardType} + * + * @param packageName + * string with the package name + * @param typeName + * string with the type name + * @return WildcardType representation of + * packageName and typeName + */ + public static WildcardType wildcardTypeFor(final String packageName, final String typeName) { + return new WildcardTypeImpl(packageName, typeName); + } + + /** + * Creates instance of + * {@link org.opendaylight.mdsal.binding2.model.api.ParameterizedType + * ParameterizedType} where raw type is + * {@link org.opendaylight.mdsal.binding2.spec.Augmentable} and actual + * parameter is valueType. + * + * @param valueType + * JAVA Type with actual parameter + * @return ParametrizedType representation of raw type + * Augmentable with actual parameter + * valueType + */ + public static ParameterizedType augmentableTypeFor(final Type valueType) { + final Type augmentable = typeForClass(Augmentable.class); + return parameterizedTypeFor(augmentable, valueType); + } + + /** + * Creates instance of + * {@link org.opendaylight.mdsal.binding2.model.api.ParameterizedType + * ParameterizedType} where raw type is + * {@link org.opendaylight.mdsal.binding2.spec.Augmentation} and actual + * parameter is valueType. + * + * @param valueType + * JAVA Type with actual parameter + * @return ParametrizedType reprezentation of raw type + * Augmentation with actual parameter + * valueType + */ + public static ParameterizedType augmentationTypeFor(final Type valueType) { + final Type augmentation = typeForClass(Augmentation.class); + return parameterizedTypeFor(augmentation, valueType); + } + + + public static @Nullable + String getOuterClassName(final Type valueType) { + final String pkgName = valueType.getPackageName(); + if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) { + // It is inner class. + return Iterables.getLast(DOT_SPLITTER.split(pkgName)); + } + return null; + } + + /** + * + * Represents concrete JAVA type. + * + */ + private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType { + + private final Restrictions restrictions; + + /** + * Creates instance of this class with package pkName and + * with the type name name. + * + * @param pkName + * string with package name + * @param name + * string with the name of the type + */ + private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) { + super(pkName, name); + this.restrictions = restrictions; + } + + @Override + public Restrictions getRestrictions() { + return restrictions; + } + } + + /** + * + * Represents concrete JAVA type with changed restriction values. + * + */ + private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions { + private final Restrictions restrictions; + + /** + * Creates instance of this class with package pkName and + * with the type name name. + * + * @param pkName + * string with package name + * @param name + * string with the name of the type + */ + private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) { + super(pkName, name); + this.restrictions = Preconditions.checkNotNull(restrictions); + } + + @Override + public Restrictions getRestrictions() { + return restrictions; + } + } + + /** + * + * Represents parametrized JAVA type. + * + */ + private static class ParameterizedTypeImpl extends AbstractBaseType implements ParameterizedType { + /** + * Array of JAVA actual type parameters. + */ + private final Type[] actualTypes; + + /** + * JAVA raw type (like List, Set, Map...) + */ + private final Type rawType; + + @Override + public Type[] getActualTypeArguments() { + + return actualTypes; + } + + @Override + public Type getRawType() { + return rawType; + } + + /** + * Creates instance of this class with concrete rawType and array of + * actual parameters. + * + * @param rawType + * JAVA Type for raw type + * @param actTypes + * array of actual parameters + */ + public ParameterizedTypeImpl(final Type rawType, final Type[] actTypes) { + super(rawType.getPackageName(), rawType.getName()); + this.rawType = rawType; + this.actualTypes = actTypes.clone(); + } + + } + + /** + * + * Represents JAVA bounded wildcard type. + * + */ + private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType { + /** + * Creates instance of this class with concrete package and type name. + * + * @param packageName + * string with the package name + * @param typeName + * string with the name of type + */ + public WildcardTypeImpl(final String packageName, final String typeName) { + super(packageName, typeName); + } + } + + public static DefaultRestrictions getDefaultRestrictions(final T min, final T max) { + return new DefaultRestrictions<>(min, max); + } + + private static final class DefaultRestrictions implements Restrictions { + private final T min; + private final T max; + private final List rangeConstraints; + + private DefaultRestrictions(final T min, final T max) { + this.min = Preconditions.checkNotNull(min); + this.max = Preconditions.checkNotNull(max); + this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional + .absent(), Optional.absent())); + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public List getRangeConstraints() { + return rangeConstraints; + } + + @Override + public List getPatternConstraints() { + return ImmutableList.of(); + } + + @Override + public List getLengthConstraints() { + return ImmutableList.of(); + } + } +} + diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractGeneratedType.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractGeneratedType.java new file mode 100644 index 0000000000..08b786a1e2 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractGeneratedType.java @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import org.opendaylight.mdsal.binding2.generator.util.AbstractBaseType; +import org.opendaylight.mdsal.binding2.model.api.AnnotationType; +import org.opendaylight.mdsal.binding2.model.api.Constant; +import org.opendaylight.mdsal.binding2.model.api.Enumeration; +import org.opendaylight.mdsal.binding2.model.api.GeneratedProperty; +import org.opendaylight.mdsal.binding2.model.api.GeneratedType; +import org.opendaylight.mdsal.binding2.model.api.MethodSignature; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.model.api.type.builder.AnnotationTypeBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.EnumBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedPropertyBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTOBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTypeBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.MethodSignatureBuilder; + +@Beta +abstract class AbstractGeneratedType extends AbstractBaseType implements GeneratedType { + + private final Type parent; + private final String 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; + + 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(); + } + + public AbstractGeneratedType(final Type parent, final String packageName, final String name, final String 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; + } + + protected static final List makeUnmodifiable(final List list) { + switch (list.size()) { + case 0: + return ImmutableList.of(); + 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()); + enclosedTypesList.addAll(enclosedGenTypeBuilders.stream().filter(builder -> builder != null).map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList())); + + enclosedTypesList.addAll(enclosedGenTOBuilders.stream().filter(builder -> builder != null).map(GeneratedTOBuilder::toInstance).collect(Collectors.toList())); + + return makeUnmodifiable(enclosedTypesList); + } + + protected static final List toUnmodifiableAnnotations(final List annotationBuilders) { + final List annotationList = new ArrayList<>(annotationBuilders.size()); + annotationList.addAll(annotationBuilders.stream().map(AnnotationTypeBuilder::toInstance).collect(Collectors.toList())); + return makeUnmodifiable(annotationList); + } + + protected final List toUnmodifiableMethods(final List methodBuilders) { + final List methods = new ArrayList<>(methodBuilders.size()); + methods.addAll(methodBuilders.stream().map(methodBuilder -> methodBuilder.toInstance(this)).collect(Collectors.toList())); + return makeUnmodifiable(methods); + } + + protected final List toUnmodifiableEnumerations(final List enumBuilders) { + final List enums = new ArrayList<>(enumBuilders.size()); + enums.addAll(enumBuilders.stream().map(enumBuilder -> enumBuilder.toInstance(this)).collect(Collectors.toList())); + return makeUnmodifiable(enums); + } + + protected final List toUnmodifiableProperties(final List methodBuilders) { + final List methods = new ArrayList<>(methodBuilders.size()); + methods.addAll(methodBuilders.stream().map(methodBuilder -> methodBuilder.toInstance(this)).collect(Collectors.toList())); + return makeUnmodifiable(methods); + } + + @Override + public Type getParentType() { + return parent; + } + + @Override + public String getComment() { + return comment; + } + + @Override + public List getAnnotations() { + return annotations; + } + + @Override + public boolean isAbstract() { + return isAbstract; + } + + @Override + public List getImplements() { + return implementsTypes; + } + + @Override + public List getEnclosedTypes() { + return enclosedTypes; + } + + @Override + public List getEnumerations() { + return enumerations; + } + + @Override + public List getConstantDefinitions() { + return constants; + } + + @Override + public List getMethodDefinitions() { + return methodSignatures; + } + + @Override + public List getProperties() { + return properties; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("GeneratedType [packageName="); + builder.append(getPackageName()); + builder.append(", name="); + builder.append(getName()); + if (parent != null) { + builder.append(", parent="); + builder.append(parent.getFullyQualifiedName()); + } else { + builder.append(", parent=null"); + } + builder.append(", comment="); + builder.append(comment); + builder.append(", annotations="); + builder.append(annotations); + builder.append(", enclosedTypes="); + builder.append(enclosedTypes); + builder.append(", enumerations="); + builder.append(enumerations); + builder.append(", constants="); + builder.append(constants); + builder.append(", methodSignatures="); + builder.append(methodSignatures); + builder.append("]"); + return builder.toString(); + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractGeneratedTypeBuilder.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractGeneratedTypeBuilder.java new file mode 100644 index 0000000000..e7e08b3ecd --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractGeneratedTypeBuilder.java @@ -0,0 +1,247 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import java.util.List; +import java.util.Objects; +import org.opendaylight.mdsal.binding2.generator.util.AbstractBaseType; +import org.opendaylight.mdsal.binding2.model.api.AccessModifier; +import org.opendaylight.mdsal.binding2.model.api.Constant; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.model.api.type.builder.AnnotationTypeBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.EnumBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedPropertyBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTOBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTypeBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTypeBuilderBase; +import org.opendaylight.mdsal.binding2.model.api.type.builder.MethodSignatureBuilder; +import org.opendaylight.yangtools.util.LazyCollections; + +@Beta +abstract class AbstractGeneratedTypeBuilder> extends AbstractBaseType + implements GeneratedTypeBuilderBase { + + private List annotationBuilders = ImmutableList.of(); + private List implementsTypes = ImmutableList.of(); + private List enumDefinitions = ImmutableList.of(); + private List constants = ImmutableList.of(); + private List methodDefinitions = ImmutableList.of(); + private final List enclosedTypes = ImmutableList.of(); + private List enclosedTransferObjects = ImmutableList.of(); + private List properties = ImmutableList.of(); + private String comment = ""; + private boolean isAbstract; + + protected AbstractGeneratedTypeBuilder(final String packageName, final String name) { + super(packageName, name); + } + + protected String getComment() { + return comment; + } + + protected List getAnnotations() { + return annotationBuilders; + } + + @Override + public boolean isAbstract() { + return isAbstract; + } + + @Override + public List getImplementsTypes() { + return implementsTypes; + } + + protected List getEnumerations() { + return enumDefinitions; + } + + protected List getConstants() { + return constants; + } + + @Override + public List getMethodDefinitions() { + return methodDefinitions; + } + + protected List getEnclosedTypes() { + return enclosedTypes; + } + + protected List getEnclosedTransferObjects() { + return enclosedTransferObjects; + } + + protected abstract T thisInstance(); + + @Override + 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); + + Preconditions.checkArgument(!enclosedTransferObjects.contains(builder), "This generated type already contains equal enclosing transfer object."); + enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, builder); + return builder; + } + + @Override + public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) { + Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!"); + Preconditions.checkArgument(!enclosedTransferObjects.contains(genTOBuilder), "This generated type already contains equal enclosing transfer object."); + enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, genTOBuilder); + return thisInstance(); + } + + @Override + public T addComment(final String comment) { + this.comment = comment; + return thisInstance(); + } + + @Override + public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) { + 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); + + Preconditions.checkArgument(!annotationBuilders.contains(builder), "This generated type already contains equal annotation."); + annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder); + return builder; + } + + @Override + public T setAbstract(final boolean isAbstract) { + this.isAbstract = isAbstract; + return thisInstance(); + } + + @Override + public T addImplementsType(final Type genType) { + Preconditions.checkArgument(genType != null, "Type cannot be null"); + Preconditions.checkArgument(!implementsTypes.contains(genType), "This generated type already contains equal implements type."); + implementsTypes = LazyCollections.lazyAdd(implementsTypes, genType); + return thisInstance(); + } + + @Override + 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!"); + Preconditions.checkArgument(!containsConstant(name), "This generated type already contains constant with the same name."); + + final Constant constant = new ConstantImpl(this, type, name, value); + constants = LazyCollections.lazyAdd(constants, constant); + return constant; + } + + public boolean containsConstant(final String name) { + Preconditions.checkArgument(name != null, "Parameter name can't be null"); + for (Constant constant : constants) { + if (name.equals(constant.getName())) { + return true; + } + } + return false; + } + + @Override + public EnumBuilder addEnumeration(final String name) { + Preconditions.checkArgument(name != null, "Name of enumeration cannot be null!"); + final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name); + + Preconditions.checkArgument(!enumDefinitions.contains(builder), "This generated type already contains equal enumeration."); + enumDefinitions = LazyCollections.lazyAdd(enumDefinitions, builder); + return builder; + } + + @Override + 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 = LazyCollections.lazyAdd(methodDefinitions, builder); + return builder; + } + + @Override + 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; + } + } + return false; + } + + @Override + public GeneratedPropertyBuilder addProperty(final String name) { + Preconditions.checkArgument(name != null, "Parameter name can't be null"); + Preconditions.checkArgument(!containsProperty(name), "This generated type already contains property with the same 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() { + return Objects.hash(getName(), getPackageName()); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + + if (!(obj instanceof AbstractGeneratedTypeBuilder)) { + return false; + } + + AbstractGeneratedTypeBuilder other = (AbstractGeneratedTypeBuilder) obj; + return Objects.equals(this, other); + } + + public Type getParent() { + return null; + } + + @Override + public List getProperties() { + return properties; + } + +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractTypeMember.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractTypeMember.java new file mode 100644 index 0000000000..c5ba844c8f --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractTypeMember.java @@ -0,0 +1,131 @@ +/* + * 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import java.util.List; +import java.util.Objects; +import org.opendaylight.mdsal.binding2.model.api.AccessModifier; +import org.opendaylight.mdsal.binding2.model.api.AnnotationType; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.model.api.TypeMember; + +@Beta +abstract class AbstractTypeMember implements TypeMember { + + private final String name; + private final String comment; + private final Type definingType; + private final Type returnType; + private final List annotations; + private final boolean isFinal; + private final boolean isStatic; + private final AccessModifier accessModifier; + + protected AbstractTypeMember(final Type definingType, final String name, final List annotations, + final String comment, final AccessModifier accessModifier, final Type returnType, + final boolean isFinal, final boolean isStatic) { + + this.definingType = definingType; + this.name = name; + this.annotations = annotations; + this.comment = comment; + this.accessModifier = accessModifier; + this.returnType = returnType; + this.isFinal = isFinal; + this.isStatic = isStatic; + } + + @Override + public List getAnnotations() { + return annotations; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getComment() { + return comment; + } + + @Override + public Type getDefiningType() { + return definingType; + } + + @Override + public AccessModifier getAccessModifier() { + return accessModifier; + } + + @Override + public Type getReturnType() { + return returnType; + } + + @Override + public boolean isFinal() { + return isFinal; + } + + @Override + public boolean isStatic() { + return isStatic; + } + + @Override + public int hashCode() { + return Objects.hash(name, returnType); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + + if (!(obj instanceof AbstractTypeMember)) { + return false; + } + + AbstractTypeMember other = (AbstractTypeMember) obj; + return Objects.equals(getName(), other.getName()) && Objects.equals(getReturnType(), other.getReturnType()); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("MethodSignatureImpl [name="); + builder.append(getName()); + builder.append(", comment="); + builder.append(getComment()); + if (getDefiningType() != null) { + builder.append(", definingType="); + builder.append(getDefiningType().getPackageName()); + builder.append("."); + builder.append(getDefiningType().getName()); + } else { + builder.append(", definingType= null"); + } + builder.append(", returnType="); + builder.append(getReturnType()); + builder.append(", annotations="); + builder.append(getAnnotations()); + builder.append("]"); + return builder.toString(); + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractTypeMemberBuilder.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractTypeMemberBuilder.java new file mode 100644 index 0000000000..4491fe641a --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AbstractTypeMemberBuilder.java @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import org.opendaylight.mdsal.binding2.model.api.AccessModifier; +import org.opendaylight.mdsal.binding2.model.api.AnnotationType; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.model.api.type.builder.AnnotationTypeBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.TypeMemberBuilder; +import org.opendaylight.yangtools.util.LazyCollections; + +@Beta +abstract class AbstractTypeMemberBuilder> implements TypeMemberBuilder { + + private final String name; + private String comment = ""; + private boolean isFinal; + private boolean isStatic; + private Type returnType; + private AccessModifier accessModifier; + private List annotationBuilders = ImmutableList.of(); + + public AbstractTypeMemberBuilder(final String name) { + this.name = name; + } + + @Override + public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) { + Preconditions.checkArgument(packageName != null, "Annotation Type cannot have package name null!"); + Preconditions.checkArgument(name != null, "Annotation Type cannot have name as null!"); + + final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name); + annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder); + return builder; + } + + protected abstract T thisInstance(); + + protected Iterable getAnnotationBuilders() { + return annotationBuilders; + } + + @Override + public AccessModifier getAccessModifier() { + return accessModifier; + } + + @Override + public String getName() { + return name; + } + + protected String getComment() { + return comment; + } + + protected boolean isFinal() { + return isFinal; + } + + protected boolean isStatic() { + return isStatic; + } + + protected Type getReturnType() { + return returnType; + } + + @Override + public T setReturnType(final Type returnType) { + Preconditions.checkArgument(returnType != null, "Return Type of member cannot be null!"); + this.returnType = returnType; + return thisInstance(); + } + + @Override + public T setAccessModifier(final AccessModifier modifier) { + Preconditions.checkArgument(modifier != null, "Access Modifier for member type cannot be null!"); + this.accessModifier = modifier; + return thisInstance(); + } + + @Override + public T setComment(final String comment) { + if (comment == null) { + this.comment = ""; + } + this.comment = comment; + return thisInstance(); + } + + @Override + public T setFinal(final boolean isFinal) { + this.isFinal = isFinal; + return thisInstance(); + } + + @Override + public T setStatic(final boolean isStatic) { + this.isStatic = isStatic; + return thisInstance(); + } + + protected List toAnnotationTypes() { + final List annotations = new ArrayList<>(); + for (final AnnotationTypeBuilder annotBuilder : getAnnotationBuilders()) { + if (annotBuilder != null) { + annotations.add(annotBuilder.toInstance()); + } + } + + return ImmutableList.copyOf(annotations); + } + + @Override + public int hashCode() { + return Objects.hash(getName(), getReturnType()); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + + if (!(obj instanceof AbstractTypeMemberBuilder)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + + AbstractTypeMemberBuilder other = (AbstractTypeMemberBuilder) obj; + return Objects.equals(getName(), other.getName()) && Objects.equals(getReturnType(), other.getReturnType()); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("GeneratedPropertyImpl [name="); + builder.append(getName()); + builder.append(", annotations="); + builder.append(getAnnotationBuilders()); + builder.append(", comment="); + builder.append(getComment()); + builder.append(", returnType="); + builder.append(getReturnType()); + builder.append(", isFinal="); + builder.append(isFinal()); + builder.append(", modifier="); + builder.append(getAccessModifier()); + builder.append("]"); + return builder.toString(); + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AnnotationTypeBuilderImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AnnotationTypeBuilderImpl.java new file mode 100644 index 0000000000..6962a5e8d4 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/AnnotationTypeBuilderImpl.java @@ -0,0 +1,320 @@ +/* + * 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import com.google.common.collect.ComparisonChain; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Ordering; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import org.opendaylight.mdsal.binding2.generator.util.AbstractBaseType; +import org.opendaylight.mdsal.binding2.model.api.AnnotationType; +import org.opendaylight.mdsal.binding2.model.api.type.builder.AnnotationTypeBuilder; +import org.opendaylight.yangtools.util.LazyCollections; + +@Beta +final class AnnotationTypeBuilderImpl extends AbstractBaseType implements AnnotationTypeBuilder { + + private final String packageName; + private final String name; + private List annotationBuilders = ImmutableList.of(); + private List parameters = ImmutableList.of(); + + public AnnotationTypeBuilderImpl(final String packageName, final String name) { + super(packageName, name); + this.packageName = packageName; + this.name = name; + } + + @Override + public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) { + if (packageName != null && name != null) { + final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name); + if (!annotationBuilders.contains(builder)) { + annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder); + return builder; + } + } + return null; + } + + private boolean addParameter(final ParameterImpl param) { + if (!parameters.contains(param)) { + parameters = LazyCollections.lazyAdd(parameters, param); + return true; + } else { + return false; + } + } + + @Override + public boolean addParameter(final String paramName, final String value) { + if (paramName != null && value != null) { + final ParameterImpl param = new ParameterImpl(paramName, value); + return addParameter(param); + } + return false; + } + + @Override + public boolean addParameters(final String paramName, final List values) { + if (paramName != null && values != null) { + final ParameterImpl param = new ParameterImpl(paramName, values); + return addParameter(param); + } + return false; + } + + @Override + public AnnotationType toInstance() { + return new AnnotationTypeImpl(packageName, name, annotationBuilders, parameters); + } + + @Override + public int hashCode() { + return Objects.hash(name, packageName); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + if (!(obj instanceof AnnotationTypeBuilderImpl)) { + return false; + } + + AnnotationTypeBuilderImpl other = (AnnotationTypeBuilderImpl) obj; + return Objects.equals(name, other.name) && Objects.equals(packageName, other.packageName); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("AnnotationTypeBuilder [packageName="); + builder.append(packageName); + builder.append(", name="); + builder.append(name); + builder.append(", annotationBuilders="); + builder.append(annotationBuilders); + builder.append(", parameters="); + builder.append(parameters); + builder.append("]"); + return builder.toString(); + } + + private static final class AnnotationTypeImpl implements AnnotationType { + + private final String packageName; + private final String name; + private final List annotations; + private final List parameters; + private final List paramNames; + + public AnnotationTypeImpl(final String packageName, final String name, + final List annotationBuilders, + final List parameters) { + super(); + this.packageName = packageName; + this.name = name; + + final List a = new ArrayList<>(); + for (final AnnotationTypeBuilder builder : annotationBuilders) { + a.add(builder.toInstance()); + } + this.annotations = ImmutableList.copyOf(a); + + final List p = new ArrayList<>(); + for (final AnnotationType.Parameter parameter : parameters) { + p.add(parameter.getName()); + } + this.paramNames = ImmutableList.copyOf(p); + + this.parameters = parameters.isEmpty() ? ImmutableList.of() + : Collections.unmodifiableList(parameters); + } + + @Override + public String getPackageName() { + return packageName; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getFullyQualifiedName() { + return packageName + "." + name; + } + + @Override + public List getAnnotations() { + return annotations; + } + + @Override + public Parameter getParameter(final String paramName) { + if (paramName != null) { + for (final AnnotationType.Parameter parameter : parameters) { + if (parameter.getName().equals(paramName)) { + return parameter; + } + } + } + return null; + } + + @Override + public List getParameters() { + return parameters; + } + + @Override + public List getParameterNames() { + return paramNames; + } + + @Override + public boolean containsParameters() { + return !parameters.isEmpty(); + } + + @Override + public int hashCode() { + return Objects.hash(name, packageName); + } + + @Override + public int compareTo(AnnotationType other) { + return ComparisonChain.start() + .compare(this.name, other.getName()) + .compare(this.packageName, other.getPackageName()) + //FIXME: what is natural ordering for AnnotationType? + .compare(this.annotations, other.getAnnotations(), Ordering.natural().lexicographical()) + .compare(this.paramNames, other.getParameterNames(), Ordering.natural().lexicographical()) + .result(); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + if (!(obj instanceof AnnotationTypeImpl)) { + return false; + } + + AnnotationTypeImpl other = (AnnotationTypeImpl) obj; + return Objects.equals(name, other.name) && Objects.equals(packageName, other.packageName); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("AnnotationType [packageName="); + builder.append(packageName); + builder.append(", name="); + builder.append(name); + builder.append(", annotations="); + builder.append(annotations); + builder.append(", parameters="); + builder.append(parameters); + builder.append("]"); + return builder.toString(); + } + } + + private static final class ParameterImpl implements AnnotationType.Parameter { + + private final String name; + private final String value; + private final List values; + + public ParameterImpl(final String name, final String value) { + super(); + this.name = name; + this.value = value; + this.values = ImmutableList.of(); + } + + public ParameterImpl(final String name, final List values) { + super(); + this.name = name; + this.values = values; + this.value = null; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getSingleValue() { + return value; + } + + @Override + public List getValues() { + return values; + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + if (!(obj instanceof ParameterImpl)) { + return false; + } + ParameterImpl other = (ParameterImpl) obj; + return Objects.equals(name, other.name); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("ParameterImpl [name="); + builder.append(name); + builder.append(", value="); + builder.append(value); + builder.append(", values="); + builder.append(values); + builder.append("]"); + return builder.toString(); + } + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/ConstantImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/ConstantImpl.java new file mode 100644 index 0000000000..f331db8778 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/ConstantImpl.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import java.util.Objects; +import org.opendaylight.mdsal.binding2.model.api.Constant; +import org.opendaylight.mdsal.binding2.model.api.Type; + +@Beta +final class ConstantImpl implements Constant { + + private final Type definingType; + private final Type type; + private final String name; + private final Object value; + + public ConstantImpl(final Type definingType, final Type type, final String name, final Object value) { + super(); + this.definingType = definingType; + this.type = type; + this.name = name; + this.value = value; + } + + + @Override + public Type getDefiningType() { + return definingType; + } + + @Override + public Type getType() { + return type; + } + + @Override + public String getName() { + return name; + } + + @Override + public Object getValue() { + return value; + } + + @Override + public String toFormattedString() { + StringBuilder builder = new StringBuilder(); + builder.append(type); + builder.append(" "); + builder.append(name); + builder.append(" "); + builder.append(value); + return builder.toString(); + } + + @Override + public int hashCode() { + return Objects.hash(name, type); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + + if (!(obj instanceof ConstantImpl)) { + return false; + } + + ConstantImpl other = (ConstantImpl) obj; + return Objects.equals(name, other.name) && Objects.equals(type, other.type) && Objects.equals(value, other.value); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Constant [type="); + builder.append(type); + builder.append(", name="); + builder.append(name); + builder.append(", value="); + builder.append(value); + if (definingType != null) { + builder.append(", definingType="); + builder.append(definingType.getPackageName()); + builder.append("."); + builder.append(definingType.getName()); + } else { + builder.append(", definingType= null"); + } + builder.append("]"); + return builder.toString(); + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/EnumerationBuilderImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/EnumerationBuilderImpl.java new file mode 100644 index 0000000000..b1b6bf8f5c --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/EnumerationBuilderImpl.java @@ -0,0 +1,355 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.mdsal.binding2.generator.util.AbstractBaseType; +import org.opendaylight.mdsal.binding2.generator.util.Binding2Mapping; +import org.opendaylight.mdsal.binding2.model.api.AnnotationType; +import org.opendaylight.mdsal.binding2.model.api.Constant; +import org.opendaylight.mdsal.binding2.model.api.Enumeration; +import org.opendaylight.mdsal.binding2.model.api.Enumeration.Pair; +import org.opendaylight.mdsal.binding2.model.api.GeneratedProperty; +import org.opendaylight.mdsal.binding2.model.api.GeneratedType; +import org.opendaylight.mdsal.binding2.model.api.MethodSignature; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.model.api.type.builder.AnnotationTypeBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.EnumBuilder; +import org.opendaylight.yangtools.util.LazyCollections; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.Status; +import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair; + +@Beta +public class EnumerationBuilderImpl extends AbstractBaseType implements EnumBuilder { + + private final String packageName; + private final String name; + private List values = ImmutableList.of(); + private List annotationBuilders = ImmutableList.of(); + private List unmodifiableValues = ImmutableList.of(); + private String description; + private String reference; + private Status status; + private String moduleName; + private List schemaPath; + + public EnumerationBuilderImpl(String packageName, String name) { + super(packageName, name); + this.packageName = packageName; + this.name = name; + } + + public void setReference(final String reference) { + this.reference = reference; + } + + public void setModuleName(final String moduleName) { + this.moduleName = moduleName; + } + + public void setSchemaPath(final List schemaPath) { + this.schemaPath = schemaPath; + } + + public void setStatus(final Status status) { + this.status = status; + } + + @Override + public void setDescription(String description) { + this.description = description; + } + + @Override + public AnnotationTypeBuilder addAnnotation(String packageName, String name) { + final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name); + if (!annotationBuilders.contains(builder)) { + annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder); + } + return builder; + } + + @Override + public void addValue(String name, int value, String description, String reference, Status status) { + final EnumPairImpl p = new EnumPairImpl(name, value, description, reference, status); + values = LazyCollections.lazyAdd(values, p); + unmodifiableValues = Collections.unmodifiableList(values); + } + + @Override + public Enumeration toInstance(Type definingType) { + return new EnumerationImpl(definingType, annotationBuilders, packageName, name, values, description, + reference, status, moduleName, schemaPath); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("EnumerationBuilderImpl [packageName="); + builder.append(packageName); + builder.append(", name="); + builder.append(name); + builder.append(", values="); + builder.append(values); + builder.append("]"); + return builder.toString(); + } + + @Override + public void updateEnumPairsFromEnumTypeDef(EnumTypeDefinition enumTypeDef) { + final List enums = enumTypeDef.getValues(); + if (enums != null) { + enums.stream().filter(enumPair -> enumPair != null).forEach(enumPair -> this.addValue(enumPair.getName(), + enumPair.getValue(), enumPair.getDescription(), enumPair.getReference(), enumPair.getStatus())); + } + } + + private static final class EnumPairImpl implements Enumeration.Pair { + + private final String name; + private final String mappedName; + private final int value; + private final String description; + private final String reference; + private final Status status; + + public EnumPairImpl(final String name, final int value, final String description, + final String reference, final Status status) { + + this.name = name; + this.mappedName = Binding2Mapping.getClassName(name); + this.value = value; + this.description = description; + this.reference = reference; + this.status = status; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getMappedName() { + return mappedName; + } + + @Override + public int getValue() { + return value; + } + + @Nullable + @Override + public String getDescription() { + return description; + } + + @Nullable + @Override + public String getReference() { + return reference; + } + + @Nonnull + @Override + public Status getStatus() { + return status; + } + + @Override + public int hashCode() { + return Objects.hash(name, value); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj == null) { + return false; + } + + if (getClass() != obj.getClass()) { + return false; + } + + if (!(obj instanceof EnumPairImpl)) { + return false; + } + + EnumPairImpl other = (EnumPairImpl) obj; + + return Objects.equals(name, other.name) && Objects.equals(value, other.value); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("EnumPair [name="); + builder.append(name); + builder.append(", mappedName="); + builder.append(getMappedName()); + builder.append(", value="); + builder.append(value); + builder.append("]"); + return builder.toString(); + } + } + + private static final class EnumerationImpl extends AbstractBaseType implements Enumeration { + + private final Type definingType; + private final String description; + private final String reference; + private final Status status; + private final String moduleName; + private final List schemaPath; + private final List values; + private final List annotations; + + public EnumerationImpl(final Type definingType, final List annotationBuilders, + final String packageName, final String name, final List values, final String description, + final String reference, final Status status, final String moduleName, final + List schemaPath) { + super(packageName, name); + this.definingType = definingType; + this.values = values; + this.description = description; + this.reference = reference; + this.status = status; + this.moduleName = moduleName; + this.schemaPath = schemaPath; + + final List a = annotationBuilders.stream().map(AnnotationTypeBuilder::toInstance) + .collect(Collectors.toList()); + this.annotations = ImmutableList.copyOf(a); + } + + @Override + public List getAnnotations() { + return annotations; + } + + @Override + public Type getParentType() { + return definingType; + } + + @Override + public Optional getDescription() { + return Optional.of(description); + } + + @Override + public List getValues() { + return values; + } + + @Override + public String toFormattedString() { + StringBuilder builder = new StringBuilder(); + builder.append("public enum"); + builder.append(" "); + builder.append(getName()); + builder.append(" {"); + builder.append("\n"); + + int i = 0; + for (final Enumeration.Pair valPair : values) { + builder.append("\t"); + builder.append(" "); + builder.append(valPair.getMappedName()); + builder.append(" ("); + builder.append(valPair.getValue()); + + if (i == (values.size() - 1)) { + builder.append(" );"); + } else { + builder.append(" ),"); + } + ++i; + } + builder.append("\n}"); + return builder.toString(); + } + + @Override + public Optional getReference() { + return Optional.of(reference); + } + + @Override + public List getSchemaPath() { + return schemaPath; + } + + @Override + public String getModuleName() { + return moduleName; + } + + @Override + public String getComment() { + //noop + return null; + } + + @Override + public boolean isAbstract() { + return false; + } + + @Override + public List getImplements() { + return ImmutableList.of(); + } + + @Override + public List getEnclosedTypes() { + return ImmutableList.of(); + } + + @Override + public List getEnumerations() { + return ImmutableList.of(); + } + + @Override + public List getConstantDefinitions() { + return ImmutableList.of(); + } + + @Override + public List getMethodDefinitions() { + return ImmutableList.of(); + } + + @Override + public List getProperties() { + return ImmutableList.of(); + } + + public Status getStatus() { + return status; + } + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedPropertyBuilderImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedPropertyBuilderImpl.java new file mode 100644 index 0000000000..eb298a7617 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedPropertyBuilderImpl.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import java.util.List; +import org.opendaylight.mdsal.binding2.model.api.AnnotationType; +import org.opendaylight.mdsal.binding2.model.api.GeneratedProperty; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedPropertyBuilder; + +@Beta +public final class GeneratedPropertyBuilderImpl extends AbstractTypeMemberBuilder implements + GeneratedPropertyBuilder { + + private String value; + private boolean isReadOnly; + + public GeneratedPropertyBuilderImpl(String name) { + super(name); + this.isReadOnly = true; + } + + @Override + public GeneratedPropertyBuilder setValue(String value) { + this.value = value; + return this; + } + + @Override + public GeneratedPropertyBuilder setReadOnly(boolean isReadOnly) { + this.isReadOnly = isReadOnly; + return this; + } + + @Override + protected GeneratedPropertyBuilderImpl thisInstance() { + return this; + } + + @Override + public GeneratedProperty toInstance(Type definingType) { + final List annotations = toAnnotationTypes(); + return new GeneratedPropertyImpl(definingType, getName(), annotations, getComment(), getAccessModifier(), + getReturnType(), isFinal(), isStatic(), isReadOnly, value); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("GeneratedPropertyImpl [name="); + builder.append(getName()); + builder.append(", annotations="); + builder.append(getAnnotationBuilders()); + builder.append(", comment="); + builder.append(getComment()); + builder.append(", returnType="); + builder.append(getReturnType()); + builder.append(", isFinal="); + builder.append(isFinal()); + builder.append(", isReadOnly="); + builder.append(isReadOnly); + builder.append(", modifier="); + builder.append(getAccessModifier()); + builder.append("]"); + return builder.toString(); + } +} \ No newline at end of file diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedPropertyImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedPropertyImpl.java new file mode 100644 index 0000000000..9a760df2b2 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedPropertyImpl.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import java.util.List; +import org.opendaylight.mdsal.binding2.model.api.AccessModifier; +import org.opendaylight.mdsal.binding2.model.api.AnnotationType; +import org.opendaylight.mdsal.binding2.model.api.GeneratedProperty; +import org.opendaylight.mdsal.binding2.model.api.Type; + +@Beta +final class GeneratedPropertyImpl extends AbstractTypeMember implements GeneratedProperty { + + private String value; + private boolean isReadOnly; + + public GeneratedPropertyImpl(Type definingType, String name, List annotations, String comment, + AccessModifier accessModifier, Type returnType, boolean isFinal, boolean isStatic, + boolean isReadOnly, String value) { + super(definingType, name, annotations, comment, accessModifier, returnType, isFinal, isStatic); + this.value = value; + this.isReadOnly = isReadOnly; + } + + @Override + public String getValue() { + return value; + } + + @Override + public boolean isReadOnly() { + return isReadOnly; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("GeneratedPropertyImpl [name="); + builder.append(getName()); + builder.append(", annotations="); + builder.append(getAnnotations()); + builder.append(", comment="); + builder.append(getComment()); + if (getDefiningType() != null) { + builder.append(", parent="); + builder.append(getDefiningType().getPackageName()); + builder.append("."); + builder.append(getDefiningType().getName()); + } else { + builder.append(", parent=null"); + } + builder.append(", returnType="); + builder.append(getReturnType()); + builder.append(", isFinal="); + builder.append(isFinal()); + builder.append(", isReadOnly="); + builder.append(isReadOnly); + builder.append(", modifier="); + builder.append(getAccessModifier()); + builder.append("]"); + return builder.toString(); + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedTOBuilderImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedTOBuilderImpl.java new file mode 100644 index 0000000000..4126201df6 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedTOBuilderImpl.java @@ -0,0 +1,358 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import java.util.Collections; +import java.util.List; +import org.opendaylight.mdsal.binding2.model.api.GeneratedProperty; +import org.opendaylight.mdsal.binding2.model.api.GeneratedTransferObject; +import org.opendaylight.mdsal.binding2.model.api.ParameterizedType; +import org.opendaylight.mdsal.binding2.model.api.Restrictions; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedPropertyBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTOBuilder; +import org.opendaylight.mdsal.binding2.model.api.type.builder.MethodSignatureBuilder; +import org.opendaylight.yangtools.util.LazyCollections; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; + +@Beta +public final class GeneratedTOBuilderImpl extends AbstractGeneratedTypeBuilder implements + GeneratedTOBuilder { + + private GeneratedTransferObject extendsType; + private List equalsProperties = ImmutableList.of(); + private List hashProperties = ImmutableList.of(); + private List toStringProperties = ImmutableList.of(); + private boolean isTypedef = false; + private boolean isUnionType = false; + private boolean isUnionTypeBuilder = false; + private TypeDefinition baseType = null; + private Restrictions restrictions; + private GeneratedPropertyBuilder SUID; + private String reference; + private String description; + private String moduleName; + private List schemaPath; + + public GeneratedTOBuilderImpl(final String packageName, final String name) { + super(packageName, name); + setAbstract(false); + } + + @Override + public GeneratedTOBuilder setExtendsType(GeneratedTransferObject genTransObj) { + Preconditions.checkArgument(genTransObj != null, "Generated Transfer Object cannot be null!"); + extendsType = genTransObj; + return this; + } + + /** + * Add new MethodSignature definition for GeneratedTypeBuilder and + * returns MethodSignatureBuilder for specifying all Method parameters.
+ * Name of Method cannot be null, if it is null + * the method SHOULD throw {@link IllegalArgumentException}
+ * By Default the MethodSignatureBuilder SHOULD be pre-set as + * {@link MethodSignatureBuilder#setAbstract(boolean)}, + * {TypeMemberBuilder#setFinal(boolean)} and + * {TypeMemberBuilder#setAccessModifier(boolean)} + * + * @param name + * Name of Method + * @return new instance of Method Signature Builder. + */ + @Override + public MethodSignatureBuilder addMethod(final String name) { + final MethodSignatureBuilder builder = super.addMethod(name); + builder.setAbstract(false); + return builder; + } + + @Override + public GeneratedTOBuilder addEqualsIdentity(GeneratedPropertyBuilder property) { + equalsProperties = LazyCollections.lazyAdd(equalsProperties, property); + return this; + } + + @Override + public GeneratedTOBuilder addHashIdentity(GeneratedPropertyBuilder property) { + hashProperties = LazyCollections.lazyAdd(hashProperties, property); + return this; + } + + @Override + public GeneratedTOBuilder addToStringProperty(GeneratedPropertyBuilder property) { + toStringProperties = LazyCollections.lazyAdd(toStringProperties, property); + return this; + } + + @Override + public void setRestrictions(Restrictions restrictions) { + this.restrictions = restrictions; + } + + @Override + public GeneratedTransferObject toInstance() { + return new GeneratedTransferObjectImpl(this); + } + + @Override + public void setTypedef(boolean isTypedef) { + this.isTypedef = isTypedef; + } + + @Override + public void setBaseType(TypeDefinition typeDef) { + this.baseType = typeDef; + } + + @Override + public void setIsUnion(boolean isUnion) { + this.isUnionType = isUnion; + } + + @Override + public void setIsUnionBuilder(boolean isUnionTypeBuilder) { + this.isUnionTypeBuilder = isUnionTypeBuilder; + } + + @Override + public void setSUID(GeneratedPropertyBuilder suid) { + this.SUID = suid; + } + + @Override + public void setDescription(String description) { + this.description = description; + } + + @Override + public void setModuleName(String moduleName) { + this.moduleName = moduleName; + } + + @Override + public void setSchemaPath(List schemaPath) { + this.schemaPath = schemaPath; + } + + @Override + public void setReference(String reference) { + this.reference = reference; + } + + @Override + protected GeneratedTOBuilderImpl thisInstance() { + return this; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("GeneratedTransferObject [packageName="); + builder.append(getPackageName()); + builder.append(", name="); + builder.append(getName()); + builder.append(", comment="); + builder.append(getComment()); + builder.append(", constants="); + builder.append(getConstants()); + builder.append(", enumerations="); + builder.append(getEnumerations()); + builder.append(", equalsProperties="); + builder.append(equalsProperties); + builder.append(", hashCodeProperties="); + builder.append(hashProperties); + builder.append(", stringProperties="); + builder.append(toStringProperties); + builder.append(", annotations="); + builder.append(getAnnotations()); + builder.append(", methods="); + builder.append(getMethodDefinitions()); + builder.append("]"); + return builder.toString(); + } + + private static final class GeneratedTransferObjectImpl extends AbstractGeneratedType implements GeneratedTransferObject { + + private final List equalsProperties; + private final List hashCodeProperties; + private final List stringProperties; + private final GeneratedTransferObject extendsType; + private final boolean isTypedef; + private final TypeDefinition baseType; + private final boolean isUnionType; + private final boolean isUnionTypeBuilder; + private final Restrictions restrictions; + private final GeneratedProperty SUID; + private final String reference; + private final String description; + private final String moduleName; + private final List schemaPath; + + public GeneratedTransferObjectImpl(final GeneratedTOBuilderImpl builder) { + super(builder); + + this.extendsType = builder.extendsType; + this.equalsProperties = toUnmodifiableProperties(builder.equalsProperties); + this.hashCodeProperties = toUnmodifiableProperties(builder.hashProperties); + this.stringProperties = toUnmodifiableProperties(builder.toStringProperties); + + this.isTypedef = builder.isTypedef; + this.baseType = builder.baseType; + this.isUnionType = builder.isUnionType; + this.isUnionTypeBuilder = builder.isUnionTypeBuilder; + this.restrictions = builder.restrictions; + this.reference = builder.reference; + this.description = builder.description; + this.moduleName = builder.moduleName; + this.schemaPath = builder.schemaPath; + + if (builder.SUID == null) { + this.SUID = null; + } else { + this.SUID = builder.SUID.toInstance(GeneratedTransferObjectImpl.this); + } + } + + @Override + public GeneratedProperty getSUID() { + return SUID; + } + + @Override + public GeneratedTransferObject getSuperType() { + return extendsType; + } + + @Override + public List getEqualsIdentifiers() { + return equalsProperties; + } + + @Override + public List getHashCodeIdentifiers() { + return hashCodeProperties; + } + + @Override + public List getToStringIdentifiers() { + return stringProperties; + } + + @Override + public boolean isTypedef() { + return isTypedef; + } + + @Override + public TypeDefinition getBaseType() { + return baseType; + } + + @Override + public boolean isUnionType() { + return isUnionType; + } + + @Override + public boolean isUnionTypeBuilder() { + return isUnionTypeBuilder; + } + + @Override + public Restrictions getRestrictions() { + return restrictions; + } + + @Override + public Optional getDescription() { + return Optional.of(description); + } + + @Override + public Optional getReference() { + return Optional.of(reference); + } + + @Override + public List getSchemaPath() { + return schemaPath; + } + + @Override + public String getModuleName() { + return moduleName; + } + + @Override + public String toString() { + if (isTypedef) { + return serializeTypedef(this); + } + StringBuilder builder = new StringBuilder(); + builder.append("GeneratedTransferObject [packageName="); + builder.append(getPackageName()); + builder.append(", name="); + builder.append(getName()); + builder.append(", comment="); + builder.append(", annotations="); + builder.append(getAnnotations()); + builder.append(getComment()); + builder.append(", extends="); + builder.append(getSuperType()); + builder.append(", implements="); + builder.append(getImplements()); + builder.append(", enclosedTypes="); + builder.append(getEnclosedTypes()); + builder.append(", constants="); + builder.append(getConstantDefinitions()); + builder.append(", enumerations="); + builder.append(getEnumerations()); + builder.append(", properties="); + builder.append(getProperties()); + builder.append(", equalsProperties="); + builder.append(equalsProperties); + builder.append(", hashCodeProperties="); + builder.append(hashCodeProperties); + builder.append(", stringProperties="); + builder.append(stringProperties); + builder.append(", methods="); + builder.append(getMethodDefinitions()); + builder.append("]"); + return builder.toString(); + } + + private String serializeTypedef(final Type type) { + if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; + StringBuilder sb = new StringBuilder(); + sb.append(parameterizedType.getRawType().getFullyQualifiedName()); + sb.append('<'); + boolean first = true; + for (Type parameter : parameterizedType.getActualTypeArguments()) { + if (first) { + first = false; + } else { + sb.append(','); + } + sb.append(serializeTypedef(parameter)); + } + sb.append('>'); + return sb.toString(); + } else { + return type.getFullyQualifiedName(); + } + } + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedTypeBuilderImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedTypeBuilderImpl.java new file mode 100644 index 0000000000..2d09fe18cc --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/GeneratedTypeBuilderImpl.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import com.google.common.base.Optional; +import java.util.List; +import org.opendaylight.mdsal.binding2.model.api.GeneratedType; +import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTypeBuilder; +import org.opendaylight.yangtools.yang.common.QName; + +@Beta +public final class GeneratedTypeBuilderImpl extends AbstractGeneratedTypeBuilder implements + GeneratedTypeBuilder { + + private String description; + private String reference; + private String moduleName; + private List schemaPath; + + public GeneratedTypeBuilderImpl(final String packageName, final String name) { + super(packageName, name); + setAbstract(true); + } + + @Override + public GeneratedType toInstance() { + return new GeneratedTypeImpl(this); + } + + @Override + public void setDescription(String description) { + this.description = description; + } + + @Override + public void setModuleName(String moduleName) { + this.moduleName = moduleName; + } + + @Override + public void setSchemaPath(List schemaPath) { + this.schemaPath = schemaPath; + } + + @Override + public void setReference(String reference) { + this.reference = reference; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("GeneratedTransferObject [packageName="); + builder.append(getPackageName()); + builder.append(", name="); + builder.append(getName()); + builder.append(", comment="); + builder.append(getComment()); + builder.append(", annotations="); + builder.append(getAnnotations()); + builder.append(", implements="); + builder.append(getImplementsTypes()); + builder.append(", enclosedTypes="); + builder.append(getEnclosedTypes()); + builder.append(", constants="); + builder.append(getConstants()); + builder.append(", enumerations="); + builder.append(getEnumerations()); + builder.append(", properties="); + builder.append(", methods="); + builder.append(getMethodDefinitions()); + builder.append("]"); + return builder.toString(); + } + + @Override + protected GeneratedTypeBuilderImpl thisInstance() { + return this; + } + + private static final class GeneratedTypeImpl extends AbstractGeneratedType { + + private final String description; + private final String reference; + private final String moduleName; + private final List schemaPath; + + public GeneratedTypeImpl(final GeneratedTypeBuilderImpl builder) { + super(builder); + + this.description = builder.description; + this.reference = builder.reference; + this.moduleName = builder.moduleName; + this.schemaPath = builder.schemaPath; + } + + @Override + public Optional getDescription() { + return Optional.of(description); + } + + @Override + public Optional getReference() { + return Optional.of(reference); + } + + @Override + public List getSchemaPath() { + return schemaPath; + } + + @Override + public String getModuleName() { + return moduleName; + } + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/MethodParameterImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/MethodParameterImpl.java new file mode 100644 index 0000000000..7e52a57519 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/MethodParameterImpl.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2016 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import java.util.Objects; +import org.opendaylight.mdsal.binding2.model.api.MethodSignature.Parameter; +import org.opendaylight.mdsal.binding2.model.api.Type; + +@Beta +final class MethodParameterImpl implements Parameter { + + private final String name; + private final Type type; + + public MethodParameterImpl(final String name, final Type type) { + super(); + this.name = name; + this.type = type; + } + + + @Override + public String getName() { + return name; + } + + @Override + public Type getType() { + return type; + } + + @Override + public int hashCode() { + return Objects.hash(name, type); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + if (!(obj instanceof MethodParameterImpl)) { + return false; + } + MethodParameterImpl other = (MethodParameterImpl) obj; + return Objects.equals(name, other.name) && Objects.equals(type, other.type); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("MethodParameter [name="); + builder.append(name); + builder.append(", type="); + builder.append(type.getPackageName()); + builder.append("."); + builder.append(type.getName()); + builder.append("]"); + return builder.toString(); + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/MethodSignatureBuilderImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/MethodSignatureBuilderImpl.java new file mode 100644 index 0000000000..ffc4d64060 --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/MethodSignatureBuilderImpl.java @@ -0,0 +1,102 @@ +/* + * 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import com.google.common.collect.ImmutableList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import org.opendaylight.mdsal.binding2.model.api.AnnotationType; +import org.opendaylight.mdsal.binding2.model.api.MethodSignature; +import org.opendaylight.mdsal.binding2.model.api.Type; +import org.opendaylight.mdsal.binding2.model.api.type.builder.MethodSignatureBuilder; +import org.opendaylight.yangtools.util.LazyCollections; + +@Beta +final class MethodSignatureBuilderImpl extends AbstractTypeMemberBuilder implements MethodSignatureBuilder { + + private List parameters = ImmutableList.of(); + private List unmodifiableParams = ImmutableList.of(); + private boolean isAbstract; + + public MethodSignatureBuilderImpl(final String name) { + super(name); + } + + @Override + public MethodSignatureBuilder setAbstract(final boolean isAbstract) { + this.isAbstract = isAbstract; + return this; + } + + @Override + public MethodSignatureBuilder addParameter(final Type type, final String name) { + parameters = LazyCollections.lazyAdd(parameters, new MethodParameterImpl(name, type)); + unmodifiableParams = Collections.unmodifiableList(parameters); + return this; + } + + @Override + protected MethodSignatureBuilder thisInstance() { + return this; + } + + @Override + public MethodSignature toInstance(final Type definingType) { + final List annotations = toAnnotationTypes(); + return new MethodSignatureImpl(definingType, getName(), annotations, getComment(), getAccessModifier(), + getReturnType(), unmodifiableParams, isFinal(), isAbstract, isStatic()); + } + + @Override + public int hashCode() { + return Objects.hash(getName(), parameters, getReturnType()); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj; + if (!Objects.equals(getName(), other.getName())) { + return false; + } + if (!Objects.equals(parameters, other.parameters)) { + return false; + } + if (!Objects.equals(getReturnType(), other.getReturnType())) { + return false; + } + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("MethodSignatureBuilderImpl [name="); + builder.append(getName()); + builder.append(", returnType="); + builder.append(getReturnType()); + builder.append(", parameters="); + builder.append(parameters); + builder.append(", annotationBuilders="); + builder.append(getAnnotationBuilders()); + builder.append(", comment="); + builder.append(getComment()); + builder.append("]"); + return builder.toString(); + } +} diff --git a/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/MethodSignatureImpl.java b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/MethodSignatureImpl.java new file mode 100644 index 0000000000..13afe6a84f --- /dev/null +++ b/binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding2/generator/util/generated/type/builder/MethodSignatureImpl.java @@ -0,0 +1,97 @@ +/* + * 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.binding2.generator.util.generated.type.builder; + +import com.google.common.annotations.Beta; +import java.util.List; +import java.util.Objects; +import org.opendaylight.mdsal.binding2.model.api.AccessModifier; +import org.opendaylight.mdsal.binding2.model.api.AnnotationType; +import org.opendaylight.mdsal.binding2.model.api.MethodSignature; +import org.opendaylight.mdsal.binding2.model.api.Type; + +@Beta +class MethodSignatureImpl extends AbstractTypeMember implements MethodSignature { + + private final List params; + private final boolean isAbstract; + + public MethodSignatureImpl(final Type definingType, final String name, + final List annotations, + final String comment, final AccessModifier accessModifier, + final Type returnType, final List params, final boolean isFinal, + final boolean isAbstract, final boolean isStatic) { + super(definingType, name, annotations, comment, accessModifier, returnType, isFinal, isStatic); + this.params = params; + this.isAbstract = isAbstract; + } + + @Override + public boolean isAbstract() { + return isAbstract; + } + + @Override + public List getParameters() { + return params; + } + + @Override + public int hashCode() { + return Objects.hash(getName(), params, getReturnType()); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + MethodSignatureImpl other = (MethodSignatureImpl) obj; + if (!Objects.equals(getName(), other.getName())) { + return false; + } + if (!Objects.equals(params, other.params)) { + return false; + } + if (!Objects.equals(getReturnType(), other.getReturnType())) { + return false; + } + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("MethodSignatureImpl [name="); + builder.append(getName()); + builder.append(", comment="); + builder.append(getComment()); + if (getDefiningType() != null) { + builder.append(", definingType="); + builder.append(getDefiningType().getPackageName()); + builder.append("."); + builder.append(getDefiningType().getName()); + } else { + builder.append(", definingType= null"); + } + builder.append(", returnType="); + builder.append(getReturnType()); + builder.append(", params="); + builder.append(params); + builder.append(", annotations="); + builder.append(getAnnotations()); + builder.append("]"); + return builder.toString(); + } +}