2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.binding.generator.util;
10 import java.util.List;
14 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
15 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
16 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
17 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
18 import org.opendaylight.yangtools.sal.binding.model.api.Type;
19 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
20 import org.opendaylight.yangtools.yang.binding.Augmentable;
21 import org.opendaylight.yangtools.yang.binding.Augmentation;
22 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
25 public final class Types {
26 private static final Type SET_TYPE = typeForClass(Set.class);
27 private static final Type LIST_TYPE = typeForClass(List.class);
28 private static final Type MAP_TYPE = typeForClass(Map.class);
29 public static final Type DATA_OBJECT = typeForClass(DataObject.class);
32 * Creates the instance of type
33 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
34 * ConcreteType} which represents JAVA <code>void</code> type.
36 * @return <code>ConcreteType</code> instance which represents JAVA
39 public static ConcreteType voidType() {
40 return new ConcreteTypeImpl(Void.class.getPackage().getName(), Void.class.getSimpleName());
44 * Creates the instance of type
45 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
46 * ConcreteType} which represents primitive JAVA type for which package
49 * @param primitiveType
50 * string containing programaticall construction based on
51 * primitive type (e.g byte[])
52 * @return <code>ConcreteType</code> instance which represents programatic
53 * construction with primitive JAVA type
55 public static final Type primitiveType(final String primitiveType) {
56 return new ConcreteTypeImpl("", primitiveType);
60 * Returns an instance of {@link ConcreteType} describing the class
64 * @return Description of class
66 public static ConcreteType typeForClass(Class<?> cls) {
67 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName());
71 * Returns an instance of {@link ParameterizedType} describing the typed
78 * @return Description of generic type instance
80 public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {
81 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
85 * Returns an instance of {@link ParameterizedType} describing the typed
86 * {@link Set}<V> with concrete type of value.
90 * @return Description of generic type instance of Set
92 public static ParameterizedType setTypeFor(Type valueType) {
93 return parameterizedTypeFor(SET_TYPE, valueType);
97 * Returns an instance of {@link ParameterizedType} describing the typed
98 * {@link List}<V> with concrete type of value.
102 * @return Description of type instance of List
104 public static ParameterizedType listTypeFor(Type valueType) {
105 return parameterizedTypeFor(LIST_TYPE, valueType);
109 * Creates generated transfer object for
110 * {@link org.opendaylight.yangtools.yang.binding.BaseIdentity BaseIdentity}
112 * @return generated transfer object which is used as extension when YANG
113 * <code>identity</code> is mapped to generated TO
115 public static GeneratedTransferObject getBaseIdentityTO() {
116 Class<BaseIdentity> cls = BaseIdentity.class;
117 GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(cls.getPackage().getName(), cls.getSimpleName());
118 return gto.toInstance();
122 * Creates instance of type
123 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
127 * JAVA <code>Type</code> for raw type
129 * JAVA <code>Type</code>s for actual parameter types
130 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
131 * and its parameters <code>parameters</code>
133 public static ParameterizedType parameterizedTypeFor(Type type, Type... parameters) {
134 return new ParametrizedTypeImpl(type, parameters);
138 * Creates instance of type
139 * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
143 * string with the package name
145 * string with the type name
146 * @return <code>WildcardType</code> reprezentation of
147 * <code>packageName</code> and <code>typeName</code>
149 public static WildcardType wildcardTypeFor(String packageName, String typeName) {
150 return new WildcardTypeImpl(packageName, typeName);
154 * Creates instance of
155 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
156 * ParameterizedType} where raw type is
157 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
158 * parameter is <code>valueType</code>.
161 * JAVA <code>Type</code> with actual parameter
162 * @return <code>ParametrizedType</code> reprezentation of raw type
163 * <code>Augmentable</code> with actual parameter
164 * <code>valueType</code>
166 public static ParameterizedType augmentableTypeFor(Type valueType) {
167 final Type augmentable = typeForClass(Augmentable.class);
168 return parameterizedTypeFor(augmentable, valueType);
172 * Creates instance of
173 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
174 * ParameterizedType} where raw type is
175 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
176 * parameter is <code>valueType</code>.
179 * JAVA <code>Type</code> with actual parameter
180 * @return <code>ParametrizedType</code> reprezentation of raw type
181 * <code>Augmentation</code> with actual parameter
182 * <code>valueType</code>
184 public static ParameterizedType augmentationTypeFor(Type valueType) {
185 final Type augmentation = typeForClass(Augmentation.class);
186 return parameterizedTypeFor(augmentation, valueType);
191 * Represents concrete JAVA type.
194 private static class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
196 * Creates instance of this class with package <code>pkName</code> and
197 * with the type name <code>name</code>.
200 * string with package name
202 * string with the name of the type
204 private ConcreteTypeImpl(String pkName, String name) {
211 * Represents parametrized JAVA type.
214 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
216 * Array of JAVA actual type parameters.
218 private Type[] actualTypes;
221 * JAVA raw type (like List, Set, Map...)
223 private Type rawType;
226 public Type[] getActualTypeArguments() {
232 public Type getRawType() {
237 * Creates instance of this class with concrete rawType and array of
241 * JAVA <code>Type</code> for raw type
243 * array of actual parameters
245 public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {
246 super(rawType.getPackageName(), rawType.getName());
247 this.rawType = rawType;
248 this.actualTypes = actTypes;
255 * Represents JAVA bounded wildcard type.
258 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
260 * Creates instance of this class with concrete package and type name.
263 * string with the package name
265 * string with the name of type
267 public WildcardTypeImpl(String packageName, String typeName) {
268 super(packageName, typeName);