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