32199d3d5afb2200504f8b9879d52aaa3d713cee
[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      * @throws NullPointerException if any argument is null
158      */
159     public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
160         return new ParametrizedTypeImpl(type, parameters);
161     }
162
163     /**
164      * Creates instance of type {@link org.opendaylight.mdsal.binding.model.api.WildcardType}.
165      *
166      * @param identifier JavaTypeName of the type
167      * @return <code>WildcardType</code> representation of specified identifier
168      */
169     public static WildcardType wildcardTypeFor(final JavaTypeName identifier) {
170         return new WildcardTypeImpl(identifier);
171     }
172
173     /**
174      * Creates instance of
175      * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
176      * ParameterizedType} where raw type is
177      * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
178      * parameter is <code>valueType</code>.
179      *
180      * @param valueType
181      *            JAVA <code>Type</code> with actual parameter
182      * @return <code>ParametrizedType</code> representation of raw type
183      *         <code>Augmentable</code> with actual parameter
184      *         <code>valueType</code>
185      */
186     public static ParameterizedType augmentableTypeFor(final Type valueType) {
187         final Type augmentable = typeForClass(Augmentable.class);
188         return parameterizedTypeFor(augmentable, valueType);
189     }
190
191     /**
192      * Creates instance of
193      * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
194      * ParameterizedType} where raw type is
195      * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
196      * parameter is <code>valueType</code>.
197      *
198      * @param valueType
199      *            JAVA <code>Type</code> with actual parameter
200      * @return <code>ParametrizedType</code> reprezentation of raw type
201      *         <code>Augmentation</code> with actual parameter
202      *         <code>valueType</code>
203      */
204     public static ParameterizedType augmentationTypeFor(final Type valueType) {
205         final Type augmentation = typeForClass(Augmentation.class);
206         return parameterizedTypeFor(augmentation, valueType);
207     }
208
209     public static @Nullable String getOuterClassName(final Type valueType) {
210         return valueType.getIdentifier().immediatelyEnclosingClass().map(Object::toString).orElse(null);
211     }
212
213     /**
214      *
215      * Represents concrete JAVA type.
216      *
217      */
218     private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
219         private final Restrictions restrictions;
220
221         /**
222          * Creates instance of this class with package <code>pkName</code> and
223          * with the type name <code>name</code>.
224          *
225          * @param pkName
226          *            string with package name
227          * @param name
228          *            string with the name of the type
229          */
230         ConcreteTypeImpl(final JavaTypeName identifier, final Restrictions restrictions) {
231             super(identifier);
232             this.restrictions = restrictions;
233         }
234
235         @Override
236         public Restrictions getRestrictions() {
237             return this.restrictions;
238         }
239     }
240
241     /**
242      * Represents concrete JAVA type with changed restriction values.
243      */
244     private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
245         private final Restrictions restrictions;
246
247         /**
248          * Creates instance of this class with package <code>pkName</code> and
249          * with the type name <code>name</code>.
250          *
251          * @param pkName
252          *            string with package name
253          * @param name
254          *            string with the name of the type
255          */
256         BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
257             super(identifier);
258             this.restrictions = Preconditions.checkNotNull(restrictions);
259         }
260
261         @Override
262         public Restrictions getRestrictions() {
263             return this.restrictions;
264         }
265     }
266
267     /**
268      * Represents parametrized JAVA type.
269      */
270     private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
271         /**
272          * Array of JAVA actual type parameters.
273          */
274         private final Type[] actualTypes;
275
276         /**
277          * JAVA raw type (like List, Set, Map...)
278          */
279         private final Type rawType;
280
281         @Override
282         public Type[] getActualTypeArguments() {
283
284             return this.actualTypes;
285         }
286
287         @Override
288         public Type getRawType() {
289             return this.rawType;
290         }
291
292         /**
293          * Creates instance of this class with concrete rawType and array of
294          * actual parameters.
295          *
296          * @param rawType
297          *            JAVA <code>Type</code> for raw type
298          * @param actTypes
299          *            array of actual parameters
300          */
301         public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
302             super(rawType.getIdentifier());
303             this.rawType = rawType;
304             this.actualTypes = actTypes.clone();
305         }
306     }
307
308     /**
309      * Represents JAVA bounded wildcard type.
310      */
311     private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
312         /**
313          * Creates instance of this class with concrete package and type name.
314          *
315          * @param packageName
316          *            string with the package name
317          * @param typeName
318          *            string with the name of type
319          */
320         WildcardTypeImpl(final JavaTypeName identifier) {
321             super(identifier);
322         }
323     }
324
325     public static <T extends Number& Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
326             final T max) {
327         return new DefaultRestrictions<>(min, max);
328     }
329
330     private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
331         private final T min;
332         private final T max;
333         private final RangeConstraint<?> rangeConstraint;
334
335         private DefaultRestrictions(final T min, final T max) {
336             this.min = Preconditions.checkNotNull(min);
337             this.max = Preconditions.checkNotNull(max);
338
339             this.rangeConstraint = new RangeConstraint<T>() {
340
341                 @Override
342                 public Optional<String> getErrorAppTag() {
343                     return Optional.empty();
344                 }
345
346                 @Override
347                 public Optional<String> getErrorMessage() {
348                     return Optional.empty();
349                 }
350
351                 @Override
352                 public Optional<String> getDescription() {
353                     return Optional.empty();
354                 }
355
356                 @Override
357                 public Optional<String> getReference() {
358                     return Optional.empty();
359                 }
360
361                 @Override
362                 public RangeSet<T> getAllowedRanges() {
363                     return ImmutableRangeSet.of(Range.closed(min, max));
364                 }
365             };
366         }
367
368         @Override
369         public boolean isEmpty() {
370             return false;
371         }
372
373         @Override
374         public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
375             return Optional.of(rangeConstraint);
376         }
377
378         @Override
379         public List<PatternConstraint> getPatternConstraints() {
380             return Collections.emptyList();
381         }
382
383         @Override
384         public Optional<LengthConstraint> getLengthConstraint() {
385             return Optional.empty();
386         }
387     }
388 }