/* * 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.yangtools.binding.generator.util; import java.util.List; import java.util.Map; import java.util.Set; import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl; import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType; import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject; import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType; import org.opendaylight.yangtools.sal.binding.model.api.Type; import org.opendaylight.yangtools.sal.binding.model.api.WildcardType; import org.opendaylight.yangtools.yang.binding.Augmentable; import org.opendaylight.yangtools.yang.binding.Augmentation; import org.opendaylight.yangtools.yang.binding.BaseIdentity; import org.opendaylight.yangtools.yang.binding.DataObject; public final class Types { 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); public static final Type DATA_OBJECT = typeForClass(DataObject.class); /** * Creates the instance of type * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType * ConcreteType} which represents JAVA void type. * * @return ConcreteType instance which represents JAVA * void */ public static ConcreteType voidType() { return new ConcreteTypeImpl(Void.class.getPackage().getName(), Void.class.getSimpleName()); } /** * Creates the instance of type * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType * ConcreteType} which represents primitive JAVA type for which package * doesn't exist. * * @param primitiveType * string containing programaticall construction based on * primitive type (e.g byte[]) * @return ConcreteType instance which represents programatic * construction with primitive JAVA type */ public static final Type primitiveType(final String primitiveType) { return new ConcreteTypeImpl("", primitiveType); } /** * Returns an instance of {@link ConcreteType} describing the class * * @param cls * Class to describe * @return Description of class */ public static ConcreteType typeForClass(Class cls) { return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName()); } /** * Returns an instance of {@link ParameterizedType} describing the typed * {@link Map} * * @param keyType * Key Type * @param valueType * Value Type * @return Description of generic type instance */ public static ParameterizedType mapTypeFor(Type keyType, Type valueType) { return parameterizedTypeFor(MAP_TYPE, keyType, valueType); } /** * Returns an instance of {@link ParameterizedType} describing the typed * {@link Set} with concrete type of value. * * @param valueType * Value Type * @return Description of generic type instance of Set */ public static ParameterizedType setTypeFor(Type valueType) { return parameterizedTypeFor(SET_TYPE, valueType); } /** * Returns an instance of {@link ParameterizedType} describing the typed * {@link List} with concrete type of value. * * @param valueType * Value Type * @return Description of type instance of List */ public static ParameterizedType listTypeFor(Type valueType) { return parameterizedTypeFor(LIST_TYPE, valueType); } /** * Creates generated transfer object for * {@link org.opendaylight.yangtools.yang.binding.BaseIdentity BaseIdentity} * * @return generated transfer object which is used as extension when YANG * identity is mapped to generated TO */ public static GeneratedTransferObject getBaseIdentityTO() { Class cls = BaseIdentity.class; GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(cls.getPackage().getName(), cls.getSimpleName()); return gto.toInstance(); } /** * Creates instance of type * {@link org.opendaylight.yangtools.sal.binding.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(Type type, Type... parameters) { return new ParametrizedTypeImpl(type, parameters); } /** * Creates instance of type * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType * WildcardType} * * @param packageName * string with the package name * @param typeName * string with the type name * @return WildcardType reprezentation of * packageName and typeName */ public static WildcardType wildcardTypeFor(String packageName, String typeName) { return new WildcardTypeImpl(packageName, typeName); } /** * Creates instance of * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType * ParameterizedType} where raw type is * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual * parameter is valueType. * * @param valueType * JAVA Type with actual parameter * @return ParametrizedType reprezentation of raw type * Augmentable with actual parameter * valueType */ public static ParameterizedType augmentableTypeFor(Type valueType) { final Type augmentable = typeForClass(Augmentable.class); return parameterizedTypeFor(augmentable, valueType); } /** * Creates instance of * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType * ParameterizedType} where raw type is * {@link org.opendaylight.yangtools.yang.binding.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(Type valueType) { final Type augmentation = typeForClass(Augmentation.class); return parameterizedTypeFor(augmentation, valueType); } /** * * Represents concrete JAVA type. * */ private static class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType { /** * 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(String pkName, String name) { super(pkName, name); } } /** * * Represents parametrized JAVA type. * */ private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType { /** * Array of JAVA actual type parameters. */ private Type[] actualTypes; /** * JAVA raw type (like List, Set, Map...) */ private 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 ParametrizedTypeImpl(Type rawType, Type[] actTypes) { super(rawType.getPackageName(), rawType.getName()); this.rawType = rawType; this.actualTypes = actTypes; } } /** * * 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(String packageName, String typeName) { super(packageName, typeName); } } }