2966246601ed3c8c0fe2ede8b1720a3dde8aed50
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-util / src / main / java / org / opendaylight / controller / binding / generator / util / Types.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.binding.generator.util;\r
9 \r
10 import java.util.List;\r
11 import java.util.Map;\r
12 import java.util.Set;\r
13 \r
14 import org.opendaylight.controller.sal.binding.model.api.ConcreteType;\r
15 import org.opendaylight.controller.sal.binding.model.api.ParameterizedType;\r
16 import org.opendaylight.controller.sal.binding.model.api.Type;\r
17 import org.opendaylight.controller.yang.binding.Augmentable;\r
18 import org.opendaylight.controller.yang.binding.Augmentation;\r
19 import org.opendaylight.controller.yang.binding.DataObject;\r
20 \r
21 public final class Types {\r
22     private static final Type SET_TYPE = typeForClass(Set.class);\r
23     private static final Type LIST_TYPE = typeForClass(List.class);\r
24     private static final Type MAP_TYPE = typeForClass(Map.class);\r
25     \r
26     public static final Type DATA_OBJECT = typeForClass(DataObject.class);\r
27     \r
28     private Types() {\r
29     }\r
30 \r
31     public static ConcreteType voidType() {\r
32         return new ConcreteTypeImpl(Void.class.getPackage().getName(),\r
33                 Void.class.getSimpleName());\r
34     }\r
35 \r
36     /**\r
37      * Returns an instance of {@link ConcreteType} describing the class\r
38      * \r
39      * @param cls\r
40      *            Class to describe\r
41      * @return Description of class\r
42      */\r
43     public static ConcreteType typeForClass(Class<?> cls) {\r
44         return new ConcreteTypeImpl(cls.getPackage().getName(),\r
45                 cls.getSimpleName());\r
46     }\r
47 \r
48     /**\r
49      * Returns an instance of {@link ParameterizedType} describing the typed\r
50      * {@link Map}<K,V>\r
51      * \r
52      * @param keyType\r
53      *            Key Type\r
54      * @param valueType\r
55      *            Value Type\r
56      * @return Description of generic type instance\r
57      */\r
58     public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {\r
59         return parameterizedTypeFor(MAP_TYPE, keyType, valueType);\r
60     }\r
61 \r
62     /**\r
63      * Returns an instance of {@link ParameterizedType} describing the typed\r
64      * {@link Set}<V> with concrete type of value.\r
65      * \r
66      * @param valueType\r
67      *            Value Type\r
68      * @return Description of generic type instance of Set\r
69      */\r
70     public static ParameterizedType setTypeFor(Type valueType) {\r
71         return parameterizedTypeFor(SET_TYPE, valueType);\r
72     }\r
73 \r
74     /**\r
75      * Returns an instance of {@link ParameterizedType} describing the typed\r
76      * {@link List}<V> with concrete type of value.\r
77      * \r
78      * @param valueType\r
79      *            Value Type\r
80      * @return Description of type instance of List\r
81      */\r
82     public static ParameterizedType listTypeFor(Type valueType) {\r
83         return parameterizedTypeFor(LIST_TYPE, valueType);\r
84     }\r
85 \r
86     /**\r
87      * \r
88      * @param type\r
89      * @param parameters\r
90      * @return\r
91      */\r
92     public static ParameterizedType parameterizedTypeFor(Type type,\r
93             Type... parameters) {\r
94         return new ParametrizedTypeImpl(type, parameters);\r
95     }\r
96     \r
97     public static ParameterizedType augmentableTypeFor(Type valueType) {\r
98         final Type augmentable = typeForClass(Augmentable.class);\r
99         return parameterizedTypeFor(augmentable, valueType);\r
100     }\r
101     \r
102     public static ParameterizedType augmentationTypeFor(Type valueType) {\r
103         final Type augmentation = typeForClass(Augmentation.class);\r
104         return parameterizedTypeFor(augmentation, valueType);\r
105     }\r
106     \r
107     private static class ConcreteTypeImpl extends AbstractBaseType implements\r
108             ConcreteType {\r
109         private ConcreteTypeImpl(String pkName, String name) {\r
110             super(pkName, name);\r
111         }\r
112     }\r
113 \r
114     private static class ParametrizedTypeImpl extends AbstractBaseType\r
115             implements ParameterizedType {\r
116         private Type[] actualTypes;\r
117         private Type rawType;\r
118 \r
119         @Override\r
120         public Type[] getActualTypeArguments() {\r
121 \r
122             return actualTypes;\r
123         }\r
124 \r
125         @Override\r
126         public Type getRawType() {\r
127             return rawType;\r
128         }\r
129 \r
130         public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {\r
131             super(rawType.getPackageName(), rawType.getName());\r
132             this.rawType = rawType;\r
133             this.actualTypes = actTypes;\r
134         }\r
135 \r
136     }\r
137 }\r