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.yangtools.binding.generator.util;
10 import com.google.common.base.CharMatcher;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Splitter;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import com.google.common.collect.Iterables;
18 import java.util.Collections;
19 import java.util.List;
22 import java.util.concurrent.Future;
23 import javax.annotation.Nullable;
24 import org.opendaylight.yangtools.sal.binding.model.api.BaseTypeWithRestrictions;
25 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
26 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
27 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
28 import org.opendaylight.yangtools.sal.binding.model.api.Type;
29 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
30 import org.opendaylight.yangtools.yang.binding.Augmentable;
31 import org.opendaylight.yangtools.yang.binding.Augmentation;
32 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
33 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
34 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
35 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
38 * @deprecated Use {@link org.opendaylight.mdsal.binding.generator.util.Types} instead.
41 public final class Types {
42 private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
43 new CacheLoader<Class<?>, ConcreteType>() {
45 public ConcreteType load(final Class<?> key) {
46 return new ConcreteTypeImpl(key.getPackage().getName(), key.getSimpleName(), null);
49 private static final LoadingCache<Class<?>, ConcreteType> TYPE_CACHE =
50 CacheBuilder.newBuilder().weakKeys().build(TYPE_LOADER);
52 private static final Type SET_TYPE = typeForClass(Set.class);
53 private static final Type LIST_TYPE = typeForClass(List.class);
54 private static final Type MAP_TYPE = typeForClass(Map.class);
56 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
57 public static final ConcreteType FUTURE = typeForClass(Future.class);
58 public static final ConcreteType STRING = typeForClass(String.class);
59 public static final ConcreteType VOID = typeForClass(Void.class);
60 public static final ConcreteType BYTE_ARRAY = primitiveType("byte[]", null);
61 public static final ConcreteType CHAR_ARRAY = primitiveType("char[]", null);
62 private static final Splitter DOT_SPLITTER = Splitter.on('.');
65 * It is not desirable to create instance of this class
71 * Creates the instance of type
72 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
73 * ConcreteType} which represents JAVA <code>void</code> type.
75 * @return <code>ConcreteType</code> instance which represents JAVA
78 public static ConcreteType voidType() {
83 * Creates the instance of type
84 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
85 * ConcreteType} which represents primitive JAVA type for which package
88 * @param primitiveType
89 * string containing programmatic construction based on
90 * primitive type (e.g byte[])
93 * @return <code>ConcreteType</code> instance which represents programmatic
94 * construction with primitive JAVA type
96 public static ConcreteType primitiveType(final String primitiveType, final Restrictions restrictions) {
97 return new ConcreteTypeImpl("", primitiveType, restrictions);
101 * Returns an instance of {@link ConcreteType} describing the class
105 * @return Description of class
107 public static ConcreteType typeForClass(final Class<?> cls) {
108 return TYPE_CACHE.getUnchecked(cls);
111 public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
112 if (restrictions != null) {
113 if (restrictions instanceof DefaultRestrictions) {
114 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
116 return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
119 return typeForClass(cls);
124 * Returns an instance of {@link ParameterizedType} describing the typed
125 * {@link Map}<K,V>
131 * @return Description of generic type instance
133 public static ParameterizedType mapTypeFor(final Type keyType, final Type valueType) {
134 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
138 * Returns an instance of {@link ParameterizedType} describing the typed
139 * {@link Set}<V> with concrete type of value.
143 * @return Description of generic type instance of Set
145 public static ParameterizedType setTypeFor(final Type valueType) {
146 return parameterizedTypeFor(SET_TYPE, valueType);
150 * Returns an instance of {@link ParameterizedType} describing the typed
151 * {@link List}<V> with concrete type of value.
155 * @return Description of type instance of List
157 public static ParameterizedType listTypeFor(final Type valueType) {
158 return parameterizedTypeFor(LIST_TYPE, valueType);
162 * Creates instance of type
163 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
167 * JAVA <code>Type</code> for raw type
169 * JAVA <code>Type</code>s for actual parameter types
170 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
171 * and its parameters <code>parameters</code>
173 public static ParameterizedType parameterizedTypeFor(final Type type, final Type... parameters) {
174 return new ParametrizedTypeImpl(type, parameters);
178 * Creates instance of type
179 * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
183 * string with the package name
185 * string with the type name
186 * @return <code>WildcardType</code> representation of
187 * <code>packageName</code> and <code>typeName</code>
189 public static WildcardType wildcardTypeFor(final String packageName, final String typeName) {
190 return new WildcardTypeImpl(packageName, typeName);
194 * Creates instance of
195 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
196 * ParameterizedType} where raw type is
197 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
198 * parameter is <code>valueType</code>.
201 * JAVA <code>Type</code> with actual parameter
202 * @return <code>ParametrizedType</code> representation of raw type
203 * <code>Augmentable</code> with actual parameter
204 * <code>valueType</code>
206 public static ParameterizedType augmentableTypeFor(final Type valueType) {
207 final Type augmentable = typeForClass(Augmentable.class);
208 return parameterizedTypeFor(augmentable, valueType);
212 * Creates instance of
213 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
214 * ParameterizedType} where raw type is
215 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
216 * parameter is <code>valueType</code>.
219 * JAVA <code>Type</code> with actual parameter
220 * @return <code>ParametrizedType</code> reprezentation of raw type
221 * <code>Augmentation</code> with actual parameter
222 * <code>valueType</code>
224 public static ParameterizedType augmentationTypeFor(final Type valueType) {
225 final Type augmentation = typeForClass(Augmentation.class);
226 return parameterizedTypeFor(augmentation, valueType);
230 public static @Nullable String getOuterClassName(final Type valueType) {
231 final String pkgName = valueType.getPackageName();
232 if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) {
233 // It is inner class.
234 return Iterables.getLast(DOT_SPLITTER.split(pkgName));
241 * Represents concrete JAVA type.
244 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
245 private final Restrictions restrictions;
248 * Creates instance of this class with package <code>pkName</code> and
249 * with the type name <code>name</code>.
252 * string with package name
254 * string with the name of the type
256 private ConcreteTypeImpl(final String pkName, final String name, final Restrictions restrictions) {
258 this.restrictions = restrictions;
262 public Restrictions getRestrictions() {
269 * Represents concrete JAVA type with changed restriction values.
272 private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
273 private final Restrictions restrictions;
276 * Creates instance of this class with package <code>pkName</code> and
277 * with the type name <code>name</code>.
280 * string with package name
282 * string with the name of the type
284 private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
286 this.restrictions = Preconditions.checkNotNull(restrictions);
290 public Restrictions getRestrictions() {
297 * Represents parametrized JAVA type.
300 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
302 * Array of JAVA actual type parameters.
304 private final Type[] actualTypes;
307 * JAVA raw type (like List, Set, Map...)
309 private final Type rawType;
312 public Type[] getActualTypeArguments() {
318 public Type getRawType() {
323 * Creates instance of this class with concrete rawType and array of
327 * JAVA <code>Type</code> for raw type
329 * array of actual parameters
331 public ParametrizedTypeImpl(final Type rawType, final Type[] actTypes) {
332 super(rawType.getPackageName(), rawType.getName());
333 this.rawType = rawType;
334 this.actualTypes = actTypes.clone();
341 * Represents JAVA bounded wildcard type.
344 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
346 * Creates instance of this class with concrete package and type name.
349 * string with the package name
351 * string with the name of type
353 public WildcardTypeImpl(final String packageName, final String typeName) {
354 super(packageName, typeName);
358 public static <T extends Number> DefaultRestrictions<T> getDefaultRestrictions(final T min, final T max) {
359 return new DefaultRestrictions<>(min, max);
362 private static final class DefaultRestrictions<T extends Number> implements Restrictions {
365 private final List<RangeConstraint> rangeConstraints;
367 private DefaultRestrictions(final T min, final T max) {
368 this.min = Preconditions.checkNotNull(min);
369 this.max = Preconditions.checkNotNull(max);
370 this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
371 .<String>absent(), Optional.<String>absent()));
375 public boolean isEmpty() {
380 public List<RangeConstraint> getRangeConstraints() {
381 return rangeConstraints;
385 public List<PatternConstraint> getPatternConstraints() {
386 return Collections.emptyList();
390 public List<LengthConstraint> getLengthConstraints() {
391 return Collections.emptyList();