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