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 STRING = typeForClass(String.class);
34 public static final ConcreteType VOID = typeForClass(Void.class);
37 * It is not desirable to create instance of this class
43 * Creates the instance of type
44 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
45 * ConcreteType} which represents JAVA <code>void</code> type.
47 * @return <code>ConcreteType</code> instance which represents JAVA
50 public static ConcreteType voidType() {
55 * Creates the instance of type
56 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
57 * ConcreteType} which represents primitive JAVA type for which package
60 * @param primitiveType
61 * string containing programaticall construction based on
62 * primitive type (e.g byte[])
63 * @return <code>ConcreteType</code> instance which represents programatic
64 * construction with primitive JAVA type
66 public static Type primitiveType(final String primitiveType, final Restrictions restrictions) {
67 return new ConcreteTypeImpl("", primitiveType, restrictions);
71 * Returns an instance of {@link ConcreteType} describing the class
75 * @return Description of class
77 public static ConcreteType typeForClass(Class<?> cls) {
78 return typeForClass(cls, null);
81 public static ConcreteType typeForClass(Class<?> cls, Restrictions restrictions) {
82 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
86 * Returns an instance of {@link ParameterizedType} describing the typed
93 * @return Description of generic type instance
95 public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {
96 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
100 * Returns an instance of {@link ParameterizedType} describing the typed
101 * {@link Set}<V> with concrete type of value.
105 * @return Description of generic type instance of Set
107 public static ParameterizedType setTypeFor(Type valueType) {
108 return parameterizedTypeFor(SET_TYPE, valueType);
112 * Returns an instance of {@link ParameterizedType} describing the typed
113 * {@link List}<V> with concrete type of value.
117 * @return Description of type instance of List
119 public static ParameterizedType listTypeFor(Type valueType) {
120 return parameterizedTypeFor(LIST_TYPE, valueType);
124 * Creates generated transfer object for
125 * {@link org.opendaylight.yangtools.yang.binding.BaseIdentity BaseIdentity}
127 * @return generated transfer object which is used as extension when YANG
128 * <code>identity</code> is mapped to generated TO
130 public static GeneratedTransferObject getBaseIdentityTO() {
131 Class<BaseIdentity> cls = BaseIdentity.class;
132 GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(cls.getPackage().getName(), cls.getSimpleName());
133 return gto.toInstance();
137 * Creates instance of type
138 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
142 * JAVA <code>Type</code> for raw type
144 * JAVA <code>Type</code>s for actual parameter types
145 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
146 * and its parameters <code>parameters</code>
148 public static ParameterizedType parameterizedTypeFor(Type type, Type... parameters) {
149 return new ParametrizedTypeImpl(type, parameters);
153 * Creates instance of type
154 * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
158 * string with the package name
160 * string with the type name
161 * @return <code>WildcardType</code> reprezentation of
162 * <code>packageName</code> and <code>typeName</code>
164 public static WildcardType wildcardTypeFor(String packageName, String typeName) {
165 return new WildcardTypeImpl(packageName, typeName);
169 * Creates instance of
170 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
171 * ParameterizedType} where raw type is
172 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
173 * parameter is <code>valueType</code>.
176 * JAVA <code>Type</code> with actual parameter
177 * @return <code>ParametrizedType</code> reprezentation of raw type
178 * <code>Augmentable</code> with actual parameter
179 * <code>valueType</code>
181 public static ParameterizedType augmentableTypeFor(Type valueType) {
182 final Type augmentable = typeForClass(Augmentable.class);
183 return parameterizedTypeFor(augmentable, valueType);
187 * Creates instance of
188 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
189 * ParameterizedType} where raw type is
190 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
191 * parameter is <code>valueType</code>.
194 * JAVA <code>Type</code> with actual parameter
195 * @return <code>ParametrizedType</code> reprezentation of raw type
196 * <code>Augmentation</code> with actual parameter
197 * <code>valueType</code>
199 public static ParameterizedType augmentationTypeFor(Type valueType) {
200 final Type augmentation = typeForClass(Augmentation.class);
201 return parameterizedTypeFor(augmentation, valueType);
206 * Represents concrete JAVA type.
209 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
210 private final Restrictions restrictions;
213 * Creates instance of this class with package <code>pkName</code> and
214 * with the type name <code>name</code>.
217 * string with package name
219 * string with the name of the type
221 private ConcreteTypeImpl(String pkName, String name, Restrictions restrictions) {
223 this.restrictions = restrictions;
227 public Restrictions getRestrictions() {
234 * Represents parametrized JAVA type.
237 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
239 * Array of JAVA actual type parameters.
241 private Type[] actualTypes;
244 * JAVA raw type (like List, Set, Map...)
246 private Type rawType;
249 public Type[] getActualTypeArguments() {
255 public Type getRawType() {
260 * Creates instance of this class with concrete rawType and array of
264 * JAVA <code>Type</code> for raw type
266 * array of actual parameters
268 public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {
269 super(rawType.getPackageName(), rawType.getName());
270 this.rawType = rawType;
271 this.actualTypes = actTypes;
278 * Represents JAVA bounded wildcard type.
281 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
283 * Creates instance of this class with concrete package and type name.
286 * string with the package name
288 * string with the name of type
290 public WildcardTypeImpl(String packageName, String typeName) {
291 super(packageName, typeName);