Initial opendaylight infrastructure commit!!
[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 \r
18 public class Types {\r
19     private static final Type SET_TYPE = typeForClass(Set.class);\r
20     private static final Type LIST_TYPE = typeForClass(List.class);\r
21     private static final Type MAP_TYPE = typeForClass(Map.class);\r
22 \r
23     private Types() {\r
24     }\r
25 \r
26     public static ConcreteType voidType() {\r
27         return new ConcreteTypeImpl(Void.class.getPackage().getName(),\r
28                 Void.class.getSimpleName());\r
29     }\r
30 \r
31     /**\r
32      * Returns an instance of {@link ConcreteType} describing the class\r
33      * \r
34      * @param cls\r
35      *            Class to describe\r
36      * @return Description of class\r
37      */\r
38     public static ConcreteType typeForClass(Class<?> cls) {\r
39         return new ConcreteTypeImpl(cls.getPackage().getName(),\r
40                 cls.getSimpleName());\r
41     }\r
42 \r
43     /**\r
44      * Returns an instance of {@link ParameterizedType} describing the typed\r
45      * {@link Map}<K,V>\r
46      * \r
47      * @param keyType\r
48      *            Key Type\r
49      * @param valueType\r
50      *            Value Type\r
51      * @return Description of generic type instance\r
52      */\r
53     public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {\r
54         return parameterizedTypeFor(MAP_TYPE, keyType, valueType);\r
55     }\r
56 \r
57     /**\r
58      * Returns an instance of {@link ParameterizedType} describing the typed\r
59      * {@link Set}<V> with concrete type of value.\r
60      * \r
61      * @param valueType\r
62      *            Value Type\r
63      * @return Description of generic type instance of Set\r
64      */\r
65     public static ParameterizedType setTypeFor(Type valueType) {\r
66         return parameterizedTypeFor(SET_TYPE, valueType);\r
67     }\r
68 \r
69     /**\r
70      * Returns an instance of {@link ParameterizedType} describing the typed\r
71      * {@link List}<V> with concrete type of value.\r
72      * \r
73      * @param valueType\r
74      *            Value Type\r
75      * @return Description of type instance of List\r
76      */\r
77     public static ParameterizedType listTypeFor(Type valueType) {\r
78         return parameterizedTypeFor(LIST_TYPE, valueType);\r
79     }\r
80 \r
81     /**\r
82      * \r
83      * @param type\r
84      * @param parameters\r
85      * @return\r
86      */\r
87     public static ParameterizedType parameterizedTypeFor(Type type,\r
88             Type... parameters) {\r
89         return new ParametrizedTypeImpl(type, parameters);\r
90     }\r
91 \r
92     private static class ConcreteTypeImpl extends AbstractBaseType implements\r
93             ConcreteType {\r
94         private ConcreteTypeImpl(String pkName, String name) {\r
95             super(pkName, name);\r
96         }\r
97     }\r
98 \r
99     private static class ParametrizedTypeImpl extends AbstractBaseType\r
100             implements ParameterizedType {\r
101         private Type[] actualTypes;\r
102         private Type rawType;\r
103 \r
104         @Override\r
105         public Type[] getActualTypeArguments() {\r
106 \r
107             return actualTypes;\r
108         }\r
109 \r
110         @Override\r
111         public Type getRawType() {\r
112             return rawType;\r
113         }\r
114 \r
115         public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {\r
116             super(rawType.getPackageName(), rawType.getName());\r
117             this.rawType = rawType;\r
118             this.actualTypes = actTypes;\r
119         }\r
120 \r
121     }\r
122 }\r