BUG-1513: introduce ChoiceSchemaNode
[mdsal.git] / code-generator / binding-generator-util / src / main / java / org / opendaylight / yangtools / binding / generator / util / Types.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.binding.generator.util;
9
10 import com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.concurrent.Future;
17 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
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
25 public final class Types {
26     private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
27             new CacheLoader<Class<?>, ConcreteType>() {
28                 @Override
29                 public ConcreteType load(final Class<?> key) {
30                     return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
31                 }
32     };
33     private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
34             CacheBuilder.newBuilder().weakKeys().softValues().build(TYPE_LOADER);
35
36     private static final Type SET_TYPE = typeForClass(Set.class);
37     private static final Type LIST_TYPE = typeForClass(List.class);
38     private static final Type MAP_TYPE = typeForClass(Map.class);
39
40     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
41     public static final ConcreteType FUTURE = typeForClass(Future.class);
42     public static final ConcreteType STRING = typeForClass(String.class);
43     public static final ConcreteType VOID = typeForClass(Void.class);
44     public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
45     public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
46
47     /**
48      * It is not desirable to create instance of this class
49      */
50     private Types() {
51     }
52
53     /**
54      * Creates the instance of type
55      * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
56      * ConcreteType} which represents JAVA <code>void</code> type.
57      *
58      * @return <code>ConcreteType</code> instance which represents JAVA
59      *         <code>void</code>
60      */
61     public static ConcreteType voidType() {
62         return VOID;
63     }
64
65     /**
66      * Creates the instance of type
67      * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
68      * ConcreteType} which represents primitive JAVA type for which package
69      * doesn't exist.
70      *
71      * @param primitiveType
72      *            string containing programmatic construction based on
73      *            primitive type (e.g byte[])
74      * @return <code>ConcreteType</code> instance which represents programmatic
75      *         construction with primitive JAVA type
76      */
77     public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
78         return new ConcreteTypeImpl("", primitiveType, restrictions);
79     }
80
81     /**
82      * Returns an instance of {@link ConcreteType} describing the class
83      *
84      * @param cls
85      *            Class to describe
86      * @return Description of class
87      */
88     public static ConcreteType typeForClass(final Class<?> cls) {
89         return TYPE_CACHE.getUnchecked(cls);
90     }
91
92     public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
93         if (restrictions != null) {
94             return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
95         } else {
96             return typeForClass(cls);
97         }
98     }
99
100     /**
101      * Returns an instance of {@link ParameterizedType} describing the typed
102      * {@link Map}&lt;K,V&gt;
103      *
104      * @param keyType
105      *            Key Type
106      * @param valueType
107      *            Value Type
108      * @return Description of generic type instance
109      */
110     public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
111         return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
112     }
113
114     /**
115      * Returns an instance of {@link ParameterizedType} describing the typed
116      * {@link Set}&lt;V&gt; with concrete type of value.
117      *
118      * @param valueType
119      *            Value Type
120      * @return Description of generic type instance of Set
121      */
122     public static ParameterizedType setTypeFor(final Type valueType) {
123         return parameterizedTypeFor(SET_TYPE, valueType);
124     }
125
126     /**
127      * Returns an instance of {@link ParameterizedType} describing the typed
128      * {@link List}&lt;V&gt; with concrete type of value.
129      *
130      * @param valueType
131      *            Value Type
132      * @return Description of type instance of List
133      */
134     public static ParameterizedType listTypeFor(final Type valueType) {
135         return parameterizedTypeFor(LIST_TYPE, valueType);
136     }
137
138     /**
139      * Creates instance of type
140      * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
141      * ParameterizedType}
142      *
143      * @param type
144      *            JAVA <code>Type</code> for raw type
145      * @param parameters
146      *            JAVA <code>Type</code>s for actual parameter types
147      * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
148      *         and its parameters <code>parameters</code>
149      */
150     public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
151         return new ParametrizedTypeImpl(type, parameters);
152     }
153
154     /**
155      * Creates instance of type
156      * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
157      * WildcardType}
158      *
159      * @param packageName
160      *            string with the package name
161      * @param typeName
162      *            string with the type name
163      * @return <code>WildcardType</code> representation of
164      *         <code>packageName</code> and <code>typeName</code>
165      */
166     public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
167         return new WildcardTypeImpl(packageName, typeName);
168     }
169
170     /**
171      * Creates instance of
172      * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
173      * ParameterizedType} where raw type is
174      * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
175      * parameter is <code>valueType</code>.
176      *
177      * @param valueType
178      *            JAVA <code>Type</code> with actual parameter
179      * @return <code>ParametrizedType</code> representation of raw type
180      *         <code>Augmentable</code> with actual parameter
181      *         <code>valueType</code>
182      */
183     public static ParameterizedType augmentableTypeFor(final Type valueType) {
184         final Type augmentable = typeForClass(Augmentable.class);
185         return parameterizedTypeFor(augmentable, valueType);
186     }
187
188     /**
189      * Creates instance of
190      * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
191      * ParameterizedType} where raw type is
192      * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
193      * parameter is <code>valueType</code>.
194      *
195      * @param valueType
196      *            JAVA <code>Type</code> with actual parameter
197      * @return <code>ParametrizedType</code> reprezentation of raw type
198      *         <code>Augmentation</code> with actual parameter
199      *         <code>valueType</code>
200      */
201     public static ParameterizedType augmentationTypeFor(final Type valueType) {
202         final Type augmentation = typeForClass(Augmentation.class);
203         return parameterizedTypeFor(augmentation, valueType);
204     }
205
206     /**
207      *
208      * Represents concrete JAVA type.
209      *
210      */
211     private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
212         private final Restrictions restrictions;
213
214         /**
215          * Creates instance of this class with package <code>pkName</code> and
216          * with the type name <code>name</code>.
217          *
218          * @param pkName
219          *            string with package name
220          * @param name
221          *            string with the name of the type
222          */
223         private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
224             super(pkName, name);
225             this.restrictions = restrictions;
226         }
227
228         @Override
229         public Restrictions getRestrictions() {
230             return restrictions;
231         }
232     }
233
234     /**
235      *
236      * Represents parametrized JAVA type.
237      *
238      */
239     private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
240         /**
241          * Array of JAVA actual type parameters.
242          */
243         private final Type[] actualTypes;
244
245         /**
246          * JAVA raw type (like List, Set, Map...)
247          */
248         private final Type rawType;
249
250         @Override
251         public Type[] getActualTypeArguments() {
252
253             return actualTypes;
254         }
255
256         @Override
257         public Type getRawType() {
258             return rawType;
259         }
260
261         /**
262          * Creates instance of this class with concrete rawType and array of
263          * actual parameters.
264          *
265          * @param rawType
266          *            JAVA <code>Type</code> for raw type
267          * @param actTypes
268          *            array of actual parameters
269          */
270         public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
271             super(rawType.getPackageName(), rawType.getName());
272             this.rawType = rawType;
273             this.actualTypes = actTypes.clone();
274         }
275
276     }
277
278     /**
279      *
280      * Represents JAVA bounded wildcard type.
281      *
282      */
283     private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
284         /**
285          * Creates instance of this class with concrete package and type name.
286          *
287          * @param packageName
288          *            string with the package name
289          * @param typeName
290          *            string with the name of type
291          */
292         public WildcardTypeImpl(final String packageName, final String typeName) {
293             super(packageName, typeName);
294         }
295     }
296
297 }