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