1f0226b82cc2d97feeda0efa1fc378dfbbbfa715
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / mdsal / binding / model / 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.mdsal.binding.model.util;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14 import com.google.common.collect.ImmutableRangeSet;
15 import com.google.common.collect.Range;
16 import com.google.common.collect.RangeSet;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.io.Serializable;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.Set;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.mdsal.binding.model.api.BaseTypeWithRestrictions;
26 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
27 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
28 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
29 import org.opendaylight.mdsal.binding.model.api.Restrictions;
30 import org.opendaylight.mdsal.binding.model.api.Type;
31 import org.opendaylight.mdsal.binding.model.api.WildcardType;
32 import org.opendaylight.yangtools.yang.binding.Augmentable;
33 import org.opendaylight.yangtools.yang.binding.Augmentation;
34 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
35 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
36 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
37
38 public final class Types {
39     private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
40             new CacheLoader<Class<?>, ConcreteType>() {
41                 @Override
42                 public ConcreteType load(final Class<?> key) {
43                     return new ConcreteTypeImpl(JavaTypeName.create(key), null);
44                 }
45     };
46     private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
47             CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
48
49     public static final Type SET_TYPE = typeForClass(Set.class);
50     public static final Type LIST_TYPE = typeForClass(List.class);
51     public static final Type MAP_TYPE = typeForClass(Map.class);
52
53     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
54     public static final ConcreteType FUTURE = typeForClass(ListenableFuture.class);
55     public static final ConcreteType STRING = typeForClass(String.class);
56     public static final ConcreteType VOID = typeForClass(Void.class);
57     public static final ConcreteType BYTE_ARRAY = typeForClass(byte[].class);
58     public static final ConcreteType CHAR_ARRAY = typeForClass(char[].class);
59
60     private static final ConcreteType CLASS = typeForClass(Class.class);
61     private static final ConcreteType OBJECT = typeForClass(Object.class);
62     private static final ConcreteType PRIMITIVE_VOID = typeForClass(void.class);
63     private static final ConcreteType SERIALIZABLE = typeForClass(Serializable.class);
64
65     /**
66      * It is not desirable to create instance of this class
67      */
68     private Types() {
69     }
70
71     /**
72      * Returns an instance of {@link ParameterizedType} which represents JAVA <code>java.lang.Class</code> type
73      * specialized to specified type.
74      *
75      * @param type Type for which to specialize
76      * @return A parameterized type corresponding to {@code Class<Type>}
77      * @throws NullPointerException if {@code type} is null
78      */
79     public static ParameterizedType classType(final Type type) {
80         return parameterizedTypeFor(CLASS, type);
81     }
82
83     /**
84      * Returns an instance of {@link ConcreteType} which represents JAVA <code>java.lang.Void</code> type.
85      *
86      * @return <code>ConcreteType</code> instance which represents JAVA <code>java.lang.Void</code>
87      */
88     public static ConcreteType voidType() {
89         return VOID;
90     }
91
92     /**
93      * Returns an instance of {@link ConcreteType} which represents {@link Object} type.
94      *
95      * @return <code>ConcreteType</code> instance which represents {@link Object}
96      */
97     public static ConcreteType objectType() {
98         return OBJECT;
99     }
100
101     /**
102      * Returns an instance of {@link ConcreteType} which represents JAVA <code>void</code> type.
103      *
104      * @return <code>ConcreteType</code> instance which represents JAVA <code>void</code>
105      */
106     public static ConcreteType primitiveVoidType() {
107         return PRIMITIVE_VOID;
108     }
109
110     /**
111      * Returns an instance of {@link ConcreteType} which represents {@link Serializable} type.
112      *
113      * @return <code>ConcreteType</code> instance which represents JAVA <code>{@link Serializable}</code>
114      */
115     public static ConcreteType serializableType() {
116         return SERIALIZABLE;
117     }
118
119     /**
120      * Returns an instance of {@link ConcreteType} describing the class
121      *
122      * @param cls
123      *            Class to describe
124      * @return Description of class
125      */
126     public static ConcreteType typeForClass(final Class<?> cls) {
127         return TYPE_CACHE.getUnchecked(cls);
128     }
129
130     public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
131         if (restrictions == null) {
132             return typeForClass(cls);
133         }
134
135         final JavaTypeName identifier = JavaTypeName.create(cls);
136         if (restrictions instanceof DefaultRestrictions) {
137             return new ConcreteTypeImpl(identifier, restrictions);
138         }
139         return new BaseTypeWithRestrictionsImpl(identifier, restrictions);
140     }
141
142     /**
143      * Returns an instance of {@link ParameterizedType} describing the typed
144      * {@link Map}&lt;K,V&gt;
145      *
146      * @param keyType
147      *            Key Type
148      * @param valueType
149      *            Value Type
150      * @return Description of generic type instance
151      */
152     public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
153         return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
154     }
155
156     /**
157      * Returns an instance of {@link ParameterizedType} describing the typed
158      * {@link Set}&lt;V&gt; with concrete type of value.
159      *
160      * @param valueType
161      *            Value Type
162      * @return Description of generic type instance of Set
163      */
164     public static ParameterizedType setTypeFor(final Type valueType) {
165         return parameterizedTypeFor(SET_TYPE, valueType);
166     }
167
168     /**
169      * Returns an instance of {@link ParameterizedType} describing the typed
170      * {@link List}&lt;V&gt; with concrete type of value.
171      *
172      * @param valueType
173      *            Value Type
174      * @return Description of type instance of List
175      */
176     public static ParameterizedType listTypeFor(final Type valueType) {
177         return parameterizedTypeFor(LIST_TYPE, valueType);
178     }
179
180     /**
181      * Creates instance of type
182      * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
183      * ParameterizedType}
184      *
185      * @param type
186      *            JAVA <code>Type</code> for raw type
187      * @param parameters
188      *            JAVA <code>Type</code>s for actual parameter types
189      * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
190      *         and its parameters <code>parameters</code>
191      * @throws NullPointerException if any argument is null
192      */
193     public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
194         return new ParametrizedTypeImpl(type, parameters);
195     }
196
197     /**
198      * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
199      *
200      * @param identifier JavaTypeName of the type
201      * @return <code>WildcardType</code> representation of specified identifier
202      */
203     public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
204         return new WildcardTypeImpl(identifier);
205     }
206
207     /**
208      * Creates instance of
209      * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
210      * ParameterizedType} where raw type is
211      * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
212      * parameter is <code>valueType</code>.
213      *
214      * @param valueType
215      *            JAVA <code>Type</code> with actual parameter
216      * @return <code>ParametrizedType</code> representation of raw type
217      *         <code>Augmentable</code> with actual parameter
218      *         <code>valueType</code>
219      */
220     public static ParameterizedType augmentableTypeFor(final Type valueType) {
221         final Type augmentable = typeForClass(Augmentable.class);
222         return parameterizedTypeFor(augmentable, valueType);
223     }
224
225     /**
226      * Creates instance of
227      * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
228      * ParameterizedType} where raw type is
229      * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
230      * parameter is <code>valueType</code>.
231      *
232      * @param valueType
233      *            JAVA <code>Type</code> with actual parameter
234      * @return <code>ParametrizedType</code> reprezentation of raw type
235      *         <code>Augmentation</code> with actual parameter
236      *         <code>valueType</code>
237      */
238     public static ParameterizedType augmentationTypeFor(final Type valueType) {
239         final Type augmentation = typeForClass(Augmentation.class);
240         return parameterizedTypeFor(augmentation, valueType);
241     }
242
243     public static @Nullable String getOuterClassName(final Type valueType) {
244         return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
245     }
246
247     /**
248      *
249      * Represents concrete JAVA type.
250      *
251      */
252     private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
253         private final Restrictions restrictions;
254
255         /**
256          * Creates instance of this class with package <code>pkName</code> and
257          * with the type name <code>name</code>.
258          *
259          * @param pkName
260          *            string with package name
261          * @param name
262          *            string with the name of the type
263          */
264         ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
265             super(identifier);
266             this.restrictions = restrictions;
267         }
268
269         @Override
270         public Restrictions getRestrictions() {
271             return this.restrictions;
272         }
273     }
274
275     /**
276      * Represents concrete JAVA type with changed restriction values.
277      */
278     private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
279         private final Restrictions restrictions;
280
281         /**
282          * Creates instance of this class with package <code>pkName</code> and
283          * with the type name <code>name</code>.
284          *
285          * @param pkName
286          *            string with package name
287          * @param name
288          *            string with the name of the type
289          */
290         BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
291             super(identifier);
292             this.restrictions = Preconditions.checkNotNull(restrictions);
293         }
294
295         @Override
296         public Restrictions getRestrictions() {
297             return this.restrictions;
298         }
299     }
300
301     /**
302      * Represents parametrized JAVA type.
303      */
304     private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
305         /**
306          * Array of JAVA actual type parameters.
307          */
308         private final Type[] actualTypes;
309
310         /**
311          * JAVA raw type (like List, Set, Map...)
312          */
313         private final Type rawType;
314
315         @Override
316         public Type[] getActualTypeArguments() {
317
318             return this.actualTypes;
319         }
320
321         @Override
322         public Type getRawType() {
323             return this.rawType;
324         }
325
326         /**
327          * Creates instance of this class with concrete rawType and array of
328          * actual parameters.
329          *
330          * @param rawType
331          *            JAVA <code>Type</code> for raw type
332          * @param actTypes
333          *            array of actual parameters
334          */
335         public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
336             super(rawType.getIdentifier());
337             this.rawType = rawType;
338             this.actualTypes = actTypes.clone();
339         }
340     }
341
342     /**
343      * Represents JAVA bounded wildcard type.
344      */
345     private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
346         /**
347          * Creates instance of this class with concrete package and type name.
348          *
349          * @param packageName
350          *            string with the package name
351          * @param typeName
352          *            string with the name of type
353          */
354         WildcardTypeImpl(final JavaTypeName identifier) {
355             super(identifier);
356         }
357     }
358
359     public static <T extends Number& Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
360             final T max) {
361         return new DefaultRestrictions<>(min, max);
362     }
363
364     private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
365         private final T min;
366         private final T max;
367         private final RangeConstraint<?> rangeConstraint;
368
369         private DefaultRestrictions(final T min, final T max) {
370             this.min = Preconditions.checkNotNull(min);
371             this.max = Preconditions.checkNotNull(max);
372
373             this.rangeConstraint = new RangeConstraint<T>() {
374
375                 @Override
376                 public Optional<String> getErrorAppTag() {
377                     return Optional.empty();
378                 }
379
380                 @Override
381                 public Optional<String> getErrorMessage() {
382                     return Optional.empty();
383                 }
384
385                 @Override
386                 public Optional<String> getDescription() {
387                     return Optional.empty();
388                 }
389
390                 @Override
391                 public Optional<String> getReference() {
392                     return Optional.empty();
393                 }
394
395                 @Override
396                 public RangeSet<T> getAllowedRanges() {
397                     return ImmutableRangeSet.of(Range.closed(min, max));
398                 }
399             };
400         }
401
402         @Override
403         public boolean isEmpty() {
404             return false;
405         }
406
407         @Override
408         public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
409             return Optional.of(rangeConstraint);
410         }
411
412         @Override
413         public List<PatternConstraint> getPatternConstraints() {
414             return Collections.emptyList();
415         }
416
417         @Override
418         public Optional<LengthConstraint> getLengthConstraint() {
419             return Optional.empty();
420         }
421     }
422 }