2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.model.util;
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;
23 import java.util.Optional;
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;
39 public final class Types {
40 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
41 new CacheLoader<Class<?>, ConcreteType>() {
43 public ConcreteType load(final Class<?> key) {
45 if (key.getEnclosingClass() != null) {
46 return new ConcreteTypeImpl(key.getEnclosingClass().getName(), key.getSimpleName(), null);
48 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
52 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
53 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
55 public static final Type SET_TYPE = typeForClass(Set.class);
56 public static final Type LIST_TYPE = typeForClass(List.class);
57 public static final Type MAP_TYPE = typeForClass(Map.class);
59 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
60 public static final ConcreteType FUTURE = typeForClass(Future.class);
61 public static final ConcreteType STRING = typeForClass(String.class);
62 public static final ConcreteType VOID = typeForClass(Void.class);
63 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
64 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
65 private static final Splitter DOT_SPLITTER = Splitter.on('.');
68 * It is not desirable to create instance of this class
74 * Creates the instance of type
75 * {@link org.opendaylight.mdsal.binding.model.api.ConcreteType
76 * ConcreteType} which represents JAVA <code>void</code> type.
78 * @return <code>ConcreteType</code> instance which represents JAVA
81 public static ConcreteType voidType() {
86 * Creates the instance of type
87 * {@link org.opendaylight.mdsal.binding.model.api.ConcreteType
88 * ConcreteType} which represents primitive JAVA type for which package
91 * @param primitiveType
92 * string containing programmatic construction based on
93 * primitive type (e.g byte[])
96 * @return <code>ConcreteType</code> instance which represents programmatic
97 * construction with primitive JAVA type
99 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
100 return new ConcreteTypeImpl("", primitiveType, restrictions);
104 * Returns an instance of {@link ConcreteType} describing the class
108 * @return Description of class
110 public static ConcreteType typeForClass(final Class<?> cls) {
111 return TYPE_CACHE.getUnchecked(cls);
114 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
115 if (restrictions == null) {
116 return typeForClass(cls);
118 if (restrictions instanceof DefaultRestrictions) {
119 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
121 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
125 * Returns an instance of {@link ParameterizedType} describing the typed
126 * {@link Map}<K,V>
132 * @return Description of generic type instance
134 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
135 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
139 * Returns an instance of {@link ParameterizedType} describing the typed
140 * {@link Set}<V> with concrete type of value.
144 * @return Description of generic type instance of Set
146 public static ParameterizedType setTypeFor(final Type valueType) {
147 return parameterizedTypeFor(SET_TYPE, valueType);
151 * Returns an instance of {@link ParameterizedType} describing the typed
152 * {@link List}<V> with concrete type of value.
156 * @return Description of type instance of List
158 public static ParameterizedType listTypeFor(final Type valueType) {
159 return parameterizedTypeFor(LIST_TYPE, valueType);
163 * Creates instance of type
164 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
168 * JAVA <code>Type</code> for raw type
170 * JAVA <code>Type</code>s for actual parameter types
171 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
172 * and its parameters <code>parameters</code>
174 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
175 return new ParametrizedTypeImpl(type, parameters);
179 * Creates instance of type
180 * {@link org.opendaylight.mdsal.binding.model.api.WildcardType
184 * string with the package name
186 * string with the type name
187 * @return <code>WildcardType</code> representation of
188 * <code>packageName</code> and <code>typeName</code>
190 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
191 return new WildcardTypeImpl(packageName, typeName);
195 * Creates instance of
196 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
197 * ParameterizedType} where raw type is
198 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
199 * parameter is <code>valueType</code>.
202 * JAVA <code>Type</code> with actual parameter
203 * @return <code>ParametrizedType</code> representation of raw type
204 * <code>Augmentable</code> with actual parameter
205 * <code>valueType</code>
207 public static ParameterizedType augmentableTypeFor(final Type valueType) {
208 final Type augmentable = typeForClass(Augmentable.class);
209 return parameterizedTypeFor(augmentable, valueType);
213 * Creates instance of
214 * {@link org.opendaylight.mdsal.binding.model.api.ParameterizedType
215 * ParameterizedType} where raw type is
216 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
217 * parameter is <code>valueType</code>.
220 * JAVA <code>Type</code> with actual parameter
221 * @return <code>ParametrizedType</code> reprezentation of raw type
222 * <code>Augmentation</code> with actual parameter
223 * <code>valueType</code>
225 public static ParameterizedType augmentationTypeFor(final Type valueType) {
226 final Type augmentation = typeForClass(Augmentation.class);
227 return parameterizedTypeFor(augmentation, valueType);
231 public static @Nullable String getOuterClassName(final Type valueType) {
232 final String pkgName = valueType.getPackageName();
233 if (CharMatcher.javaUpperCase().indexIn(pkgName) >= 0) {
234 // It is inner class.
235 return Iterables.getLast(DOT_SPLITTER.split(pkgName));
242 * Represents concrete JAVA type.
245 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
246 private final Restrictions restrictions;
249 * Creates instance of this class with package <code>pkName</code> and
250 * with the type name <code>name</code>.
253 * string with package name
255 * string with the name of the type
257 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
259 this.restrictions = restrictions;
263 public Restrictions getRestrictions() {
264 return this.restrictions;
269 * Represents concrete JAVA type with changed restriction values.
271 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
272 private final Restrictions restrictions;
275 * Creates instance of this class with package <code>pkName</code> and
276 * with the type name <code>name</code>.
279 * string with package name
281 * string with the name of the type
283 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
285 this.restrictions = Preconditions.checkNotNull(restrictions);
289 public Restrictions getRestrictions() {
290 return this.restrictions;
295 * Represents parametrized JAVA type.
297 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
299 * Array of JAVA actual type parameters.
301 private final Type[] actualTypes;
304 * JAVA raw type (like List, Set, Map...)
306 private final Type rawType;
309 public Type[] getActualTypeArguments() {
311 return this.actualTypes;
315 public Type getRawType() {
320 * Creates instance of this class with concrete rawType and array of
324 * JAVA <code>Type</code> for raw type
326 * array of actual parameters
328 public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
329 super(rawType.getPackageName(), rawType.getName());
330 this.rawType = rawType;
331 this.actualTypes = actTypes.clone();
337 * Represents JAVA bounded wildcard type.
339 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
341 * Creates instance of this class with concrete package and type name.
344 * string with the package name
346 * string with the name of type
348 public WildcardTypeImpl(final String packageName, final String typeName) {
349 super(packageName, typeName);
353 public static <T extends Number& Comparable<T>> DefaultRestrictions<T> getDefaultRestrictions(final T min,
355 return new DefaultRestrictions<>(min, max);
358 private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
361 private final RangeConstraint<?> rangeConstraint;
363 private DefaultRestrictions(final T min, final T max) {
364 this.min = Preconditions.checkNotNull(min);
365 this.max = Preconditions.checkNotNull(max);
367 this.rangeConstraint = new RangeConstraint<T>() {
370 public Optional<String> getErrorAppTag() {
371 return Optional.empty();
375 public Optional<String> getErrorMessage() {
376 return Optional.empty();
380 public Optional<String> getDescription() {
381 return Optional.empty();
385 public Optional<String> getReference() {
386 return Optional.empty();
390 public RangeSet<T> getAllowedRanges() {
391 return ImmutableRangeSet.of(Range.closed(min, max));
397 public boolean isEmpty() {
402 public Optional<? extends RangeConstraint<?>> getRangeConstraint() {
403 return Optional.of(rangeConstraint);
407 public List<PatternConstraint> getPatternConstraints() {
408 return Collections.emptyList();
412 public Optional<LengthConstraint> getLengthConstraint() {
413 return Optional.empty();