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;
13 import java.util.concurrent.Future;
15 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
16 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
17 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
18 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
19 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
20 import org.opendaylight.yangtools.sal.binding.model.api.Type;
21 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
22 import org.opendaylight.yangtools.yang.binding.Augmentable;
23 import org.opendaylight.yangtools.yang.binding.Augmentation;
24 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
26 public final class Types {
27 private static final Type SET_TYPE = typeForClass(Set.class);
28 private static final Type LIST_TYPE = typeForClass(List.class);
29 private static final Type MAP_TYPE = typeForClass(Map.class);
31 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
32 public static final ConcreteType FUTURE = typeForClass(Future.class);
33 public static final ConcreteType VOID = typeForClass(Void.class);
36 * It is not desirable to create instance of this class
42 * Creates the instance of type
43 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
44 * ConcreteType} which represents JAVA <code>void</code> type.
46 * @return <code>ConcreteType</code> instance which represents JAVA
49 public static ConcreteType voidType() {
54 * Creates the instance of type
55 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
56 * ConcreteType} which represents primitive JAVA type for which package
59 * @param primitiveType
60 * string containing programaticall construction based on
61 * primitive type (e.g byte[])
62 * @return <code>ConcreteType</code> instance which represents programatic
63 * construction with primitive JAVA type
65 public static Type primitiveType(final String primitiveType, final Restrictions restrictions) {
66 return new ConcreteTypeImpl("", primitiveType, restrictions);
70 * Returns an instance of {@link ConcreteType} describing the class
74 * @return Description of class
76 public static ConcreteType typeForClass(Class<?> cls) {
77 return typeForClass(cls, null);
80 public static ConcreteType typeForClass(Class<?> cls, Restrictions restrictions) {
81 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
85 * Returns an instance of {@link ParameterizedType} describing the typed
92 * @return Description of generic type instance
94 public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {
95 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
99 * Returns an instance of {@link ParameterizedType} describing the typed
100 * {@link Set}<V> with concrete type of value.
104 * @return Description of generic type instance of Set
106 public static ParameterizedType setTypeFor(Type valueType) {
107 return parameterizedTypeFor(SET_TYPE, valueType);
111 * Returns an instance of {@link ParameterizedType} describing the typed
112 * {@link List}<V> with concrete type of value.
116 * @return Description of type instance of List
118 public static ParameterizedType listTypeFor(Type valueType) {
119 return parameterizedTypeFor(LIST_TYPE, valueType);
123 * Creates generated transfer object for
124 * {@link org.opendaylight.yangtools.yang.binding.BaseIdentity BaseIdentity}
126 * @return generated transfer object which is used as extension when YANG
127 * <code>identity</code> is mapped to generated TO
129 public static GeneratedTransferObject getBaseIdentityTO() {
130 Class<BaseIdentity> cls = BaseIdentity.class;
131 GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(cls.getPackage().getName(), cls.getSimpleName());
132 return gto.toInstance();
136 * Creates instance of type
137 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
141 * JAVA <code>Type</code> for raw type
143 * JAVA <code>Type</code>s for actual parameter types
144 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
145 * and its parameters <code>parameters</code>
147 public static ParameterizedType parameterizedTypeFor(Type type, Type... parameters) {
148 return new ParametrizedTypeImpl(type, parameters);
152 * Creates instance of type
153 * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
157 * string with the package name
159 * string with the type name
160 * @return <code>WildcardType</code> reprezentation of
161 * <code>packageName</code> and <code>typeName</code>
163 public static WildcardType wildcardTypeFor(String packageName, String typeName) {
164 return new WildcardTypeImpl(packageName, typeName);
168 * Creates instance of
169 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
170 * ParameterizedType} where raw type is
171 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
172 * parameter is <code>valueType</code>.
175 * JAVA <code>Type</code> with actual parameter
176 * @return <code>ParametrizedType</code> reprezentation of raw type
177 * <code>Augmentable</code> with actual parameter
178 * <code>valueType</code>
180 public static ParameterizedType augmentableTypeFor(Type valueType) {
181 final Type augmentable = typeForClass(Augmentable.class);
182 return parameterizedTypeFor(augmentable, valueType);
186 * Creates instance of
187 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
188 * ParameterizedType} where raw type is
189 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
190 * parameter is <code>valueType</code>.
193 * JAVA <code>Type</code> with actual parameter
194 * @return <code>ParametrizedType</code> reprezentation of raw type
195 * <code>Augmentation</code> with actual parameter
196 * <code>valueType</code>
198 public static ParameterizedType augmentationTypeFor(Type valueType) {
199 final Type augmentation = typeForClass(Augmentation.class);
200 return parameterizedTypeFor(augmentation, valueType);
205 * Represents concrete JAVA type.
208 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
209 private final Restrictions restrictions;
212 * Creates instance of this class with package <code>pkName</code> and
213 * with the type name <code>name</code>.
216 * string with package name
218 * string with the name of the type
220 private ConcreteTypeImpl(String pkName, String name, Restrictions restrictions) {
222 this.restrictions = restrictions;
226 public Restrictions getRestrictions() {
233 * Represents parametrized JAVA type.
236 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
238 * Array of JAVA actual type parameters.
240 private Type[] actualTypes;
243 * JAVA raw type (like List, Set, Map...)
245 private Type rawType;
248 public Type[] getActualTypeArguments() {
254 public Type getRawType() {
259 * Creates instance of this class with concrete rawType and array of
263 * JAVA <code>Type</code> for raw type
265 * array of actual parameters
267 public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {
268 super(rawType.getPackageName(), rawType.getName());
269 this.rawType = rawType;
270 this.actualTypes = actTypes;
277 * Represents JAVA bounded wildcard type.
280 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
282 * Creates instance of this class with concrete package and type name.
285 * string with the package name
287 * string with the name of type
289 public WildcardTypeImpl(String packageName, String typeName) {
290 super(packageName, typeName);