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