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