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 java.util.Arrays;
11 import java.util.List;
14 import java.util.concurrent.Future;
15 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
16 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
17 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
18 import org.opendaylight.yangtools.sal.binding.model.api.Type;
19 import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
20 import org.opendaylight.yangtools.yang.binding.Augmentable;
21 import org.opendaylight.yangtools.yang.binding.Augmentation;
23 public final class Types {
24 private static final Type SET_TYPE = typeForClass(Set.class);
25 private static final Type LIST_TYPE = typeForClass(List.class);
26 private static final Type MAP_TYPE = typeForClass(Map.class);
28 public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
29 public static final ConcreteType FUTURE = typeForClass(Future.class);
30 public static final ConcreteType STRING = typeForClass(String.class);
31 public static final ConcreteType VOID = typeForClass(Void.class);
34 * It is not desirable to create instance of this class
40 * Creates the instance of type
41 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
42 * ConcreteType} which represents JAVA <code>void</code> type.
44 * @return <code>ConcreteType</code> instance which represents JAVA
47 public static ConcreteType voidType() {
52 * Creates the instance of type
53 * {@link org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
54 * ConcreteType} which represents primitive JAVA type for which package
57 * @param primitiveType
58 * string containing programaticall construction based on
59 * primitive type (e.g byte[])
60 * @return <code>ConcreteType</code> instance which represents programatic
61 * construction with primitive JAVA type
63 public static Type primitiveType(final String primitiveType, final Restrictions restrictions) {
64 return new ConcreteTypeImpl("", primitiveType, restrictions);
68 * Returns an instance of {@link ConcreteType} describing the class
72 * @return Description of class
74 public static ConcreteType typeForClass(Class<?> cls) {
75 return typeForClass(cls, null);
78 public static ConcreteType typeForClass(Class<?> cls, Restrictions restrictions) {
79 return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
83 * Returns an instance of {@link ParameterizedType} describing the typed
90 * @return Description of generic type instance
92 public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {
93 return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
97 * Returns an instance of {@link ParameterizedType} describing the typed
98 * {@link Set}<V> with concrete type of value.
102 * @return Description of generic type instance of Set
104 public static ParameterizedType setTypeFor(Type valueType) {
105 return parameterizedTypeFor(SET_TYPE, valueType);
109 * Returns an instance of {@link ParameterizedType} describing the typed
110 * {@link List}<V> with concrete type of value.
114 * @return Description of type instance of List
116 public static ParameterizedType listTypeFor(Type valueType) {
117 return parameterizedTypeFor(LIST_TYPE, valueType);
121 * Creates instance of type
122 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
126 * JAVA <code>Type</code> for raw type
128 * JAVA <code>Type</code>s for actual parameter types
129 * @return <code>ParametrizedType</code> reprezentation of <code>type</code>
130 * and its parameters <code>parameters</code>
132 public static ParameterizedType parameterizedTypeFor(Type type, Type... parameters) {
133 return new ParametrizedTypeImpl(type, parameters);
137 * Creates instance of type
138 * {@link org.opendaylight.yangtools.sal.binding.model.api.WildcardType
142 * string with the package name
144 * string with the type name
145 * @return <code>WildcardType</code> reprezentation of
146 * <code>packageName</code> and <code>typeName</code>
148 public static WildcardType wildcardTypeFor(String packageName, String typeName) {
149 return new WildcardTypeImpl(packageName, typeName);
153 * Creates instance of
154 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
155 * ParameterizedType} where raw type is
156 * {@link org.opendaylight.yangtools.yang.binding.Augmentable} and actual
157 * parameter is <code>valueType</code>.
160 * JAVA <code>Type</code> with actual parameter
161 * @return <code>ParametrizedType</code> reprezentation of raw type
162 * <code>Augmentable</code> with actual parameter
163 * <code>valueType</code>
165 public static ParameterizedType augmentableTypeFor(Type valueType) {
166 final Type augmentable = typeForClass(Augmentable.class);
167 return parameterizedTypeFor(augmentable, valueType);
171 * Creates instance of
172 * {@link org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType
173 * ParameterizedType} where raw type is
174 * {@link org.opendaylight.yangtools.yang.binding.Augmentation} and actual
175 * parameter is <code>valueType</code>.
178 * JAVA <code>Type</code> with actual parameter
179 * @return <code>ParametrizedType</code> reprezentation of raw type
180 * <code>Augmentation</code> with actual parameter
181 * <code>valueType</code>
183 public static ParameterizedType augmentationTypeFor(Type valueType) {
184 final Type augmentation = typeForClass(Augmentation.class);
185 return parameterizedTypeFor(augmentation, valueType);
190 * Represents concrete JAVA type.
193 private static final class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
194 private final Restrictions restrictions;
197 * Creates instance of this class with package <code>pkName</code> and
198 * with the type name <code>name</code>.
201 * string with package name
203 * string with the name of the type
205 private ConcreteTypeImpl(String pkName, String name, Restrictions restrictions) {
207 this.restrictions = restrictions;
211 public Restrictions getRestrictions() {
218 * Represents parametrized JAVA type.
221 private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
223 * Array of JAVA actual type parameters.
225 private Type[] actualTypes;
228 * JAVA raw type (like List, Set, Map...)
230 private Type rawType;
233 public Type[] getActualTypeArguments() {
239 public Type getRawType() {
244 * Creates instance of this class with concrete rawType and array of
248 * JAVA <code>Type</code> for raw type
250 * array of actual parameters
252 public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {
253 super(rawType.getPackageName(), rawType.getName());
254 this.rawType = rawType;
255 this.actualTypes = Arrays.copyOf(actTypes, actTypes.length);
262 * Represents JAVA bounded wildcard type.
265 private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
267 * Creates instance of this class with concrete package and type name.
270 * string with the package name
272 * string with the name of type
274 public WildcardTypeImpl(String packageName, String typeName) {
275 super(packageName, typeName);