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.math.BigDecimal;
11 import java.math.BigInteger;
12 import java.util.List;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.Future;
18 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
19 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
20 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
22 import org.opendaylight.yangtools.sal.binding.model.api.Type;
23 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
24 import org.opendaylight.yangtools.yang.binding.Augmentable;
25 import org.opendaylight.yangtools.yang.binding.Augmentation;
26 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
29 public final class Types {
30 private static final Type SET_TYPE = typeForClass(Set.class);
31 private static final Type LIST_TYPE = typeForClass(List.class);
32 private static final Type MAP_TYPE = typeForClass(Map.class);
33 public static final ConcreteType NUMBER = typeForClass(Number.class);
34 public static final ConcreteType BIG_DECIMAL = typeForClass(BigDecimal.class);
35 public static final ConcreteType BIG_INTEGER = typeForClass(BigInteger.class);
36 public static final ConcreteType BYTE = typeForClass(Byte.class);
37 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
38 public static final ConcreteType DOUBLE = typeForClass(Double.class);
39 public static final ConcreteType FLOAT = typeForClass(Float.class);
40 public static final ConcreteType INTEGER = typeForClass(Integer.class);
41 public static final ConcreteType LONG = typeForClass(Long.class);
42 public static final ConcreteType SHORT = typeForClass(Short.class);
43 public static final ConcreteType STRING = typeForClass(String.class);
44 public static final ConcreteType CHAR_SEQUENCE = typeForClass(CharSequence.class);
45 public static final ConcreteType THREAD = typeForClass(Thread.class);
46 public static final ConcreteType FUTURE = typeForClass(Future.class);
47 public static final ConcreteType CALLABLE = typeForClass(Callable.class);
48 public static final ConcreteType VOID = typeForClass(Void.class);
49 public static final ConcreteType THROWABLE = typeForClass(Throwable.class);
50 public static final ConcreteType EXCEPTION = typeForClass(Exception.class);
53 * Creates the instance of type
54 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
55 * ConcreteType} which represents JAVA <code>void</code> type.
57 * @return <code>ConcreteType</code> instance which represents JAVA
60 public static ConcreteType voidType() {
65 * Creates the instance of type
66 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
67 * ConcreteType} which represents primitive JAVA type for which package
70 * @param primitiveType
71 * string containing programaticall construction based on
72 * primitive type (e.g byte[])
73 * @return <code>ConcreteType</code> instance which represents programatic
74 * construction with primitive JAVA type
76 public static final Type primitiveType(final String primitiveType) {
77 return new ConcreteTypeImpl("", primitiveType);
81 * Returns an instance of {@link ConcreteType} describing the class
85 * @return Description of class
87 public static ConcreteType typeForClass(Class<?> cls) {
88 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName());
92 * Returns an instance of {@link ParameterizedType} describing the typed
99 * @return Description of generic type instance
101 public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {
102 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
106 * Returns an instance of {@link ParameterizedType} describing the typed
107 * {@link Set}<V> with concrete type of value.
111 * @return Description of generic type instance of Set
113 public static ParameterizedType setTypeFor(Type valueType) {
114 return parameterizedTypeFor(SET_TYPE, valueType);
118 * Returns an instance of {@link ParameterizedType} describing the typed
119 * {@link List}<V> with concrete type of value.
123 * @return Description of type instance of List
125 public static ParameterizedType listTypeFor(Type valueType) {
126 return parameterizedTypeFor(LIST_TYPE, valueType);
130 * Creates generated transfer object for
131 * {@link org.opendaylight.yangtools.yang.binding.BaseIdentity BaseIdentity}
133 * @return generated transfer object which is used as extension when YANG
134 * <code>identity</code> is mapped to generated TO
136 public static GeneratedTransferObject getBaseIdentityTO() {
137 Class<BaseIdentity> cls = BaseIdentity.class;
138 GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(cls.getPackage().getName(), cls.getSimpleName());
139 return gto.toInstance();
143 * Creates instance of type
144 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
148 * JAVA <code>Type</code> for raw type
150 * JAVA <code>Type</code>s for actual parameter types
151 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
152 * and its parameters <code>parameters</code>
154 public static ParameterizedType parameterizedTypeFor(Type type, Type... parameters) {
155 return new ParametrizedTypeImpl(type, parameters);
159 * Creates instance of type
160 * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
164 * string with the package name
166 * string with the type name
167 * @return <code>WildcardType</code> reprezentation of
168 * <code>packageName</code> and <code>typeName</code>
170 public static WildcardType wildcardTypeFor(String packageName, String typeName) {
171 return new WildcardTypeImpl(packageName, typeName);
175 * Creates instance of
176 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
177 * ParameterizedType} where raw type is
178 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
179 * parameter is <code>valueType</code>.
182 * JAVA <code>Type</code> with actual parameter
183 * @return <code>ParametrizedType</code> reprezentation of raw type
184 * <code>Augmentable</code> with actual parameter
185 * <code>valueType</code>
187 public static ParameterizedType augmentableTypeFor(Type valueType) {
188 final Type augmentable = typeForClass(Augmentable.class);
189 return parameterizedTypeFor(augmentable, valueType);
193 * Creates instance of
194 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
195 * ParameterizedType} where raw type is
196 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
197 * parameter is <code>valueType</code>.
200 * JAVA <code>Type</code> with actual parameter
201 * @return <code>ParametrizedType</code> reprezentation of raw type
202 * <code>Augmentation</code> with actual parameter
203 * <code>valueType</code>
205 public static ParameterizedType augmentationTypeFor(Type valueType) {
206 final Type augmentation = typeForClass(Augmentation.class);
207 return parameterizedTypeFor(augmentation, valueType);
212 * Represents concrete JAVA type.
215 private static class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
217 * Creates instance of this class with package <code>pkName</code> and
218 * with the type name <code>name</code>.
221 * string with package name
223 * string with the name of the type
225 private ConcreteTypeImpl(String pkName, String name) {
232 * Represents parametrized JAVA type.
235 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
237 * Array of JAVA actual type parameters.
239 private Type[] actualTypes;
242 * JAVA raw type (like List, Set, Map...)
244 private Type rawType;
247 public Type[] getActualTypeArguments() {
253 public Type getRawType() {
258 * Creates instance of this class with concrete rawType and array of
262 * JAVA <code>Type</code> for raw type
264 * array of actual parameters
266 public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {
267 super(rawType.getPackageName(), rawType.getName());
268 this.rawType = rawType;
269 this.actualTypes = actTypes;
276 * Represents JAVA bounded wildcard type.
279 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
281 * Creates instance of this class with concrete package and type name.
284 * string with the package name
286 * string with the name of type
288 public WildcardTypeImpl(String packageName, String typeName) {
289 super(packageName, typeName);