Increased version of binding-generator to 0.5.5-SNAPSHOT.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-util / src / main / java / org / opendaylight / controller / binding / generator / util / Types.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.binding.generator.util;
9
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13
14 import org.opendaylight.controller.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
15 import org.opendaylight.controller.sal.binding.model.api.ConcreteType;
16 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
17 import org.opendaylight.controller.sal.binding.model.api.ParameterizedType;
18 import org.opendaylight.controller.sal.binding.model.api.Type;
19 import org.opendaylight.controller.sal.binding.model.api.WildcardType;
20 import org.opendaylight.yangtools.yang.binding.Augmentable;
21 import org.opendaylight.yangtools.yang.binding.Augmentation;
22 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24
25 public final class Types {
26     private static final Type SET_TYPE = typeForClass(Set.class);
27     private static final Type LIST_TYPE = typeForClass(List.class);
28     private static final Type MAP_TYPE = typeForClass(Map.class);
29     public static final Type DATA_OBJECT = typeForClass(DataObject.class);
30
31     public static ConcreteType voidType() {
32         return new ConcreteTypeImpl(Void.class.getPackage().getName(), Void.class.getSimpleName());
33     }
34
35     public static final Type primitiveType(final String primitiveType) {
36         return new ConcreteTypeImpl("", primitiveType);
37     }
38
39     /**
40      * Returns an instance of {@link ConcreteType} describing the class
41      *
42      * @param cls
43      *            Class to describe
44      * @return Description of class
45      */
46     public static ConcreteType typeForClass(Class<?> cls) {
47         return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName());
48     }
49
50     /**
51      * Returns an instance of {@link ParameterizedType} describing the typed
52      * {@link Map}<K,V>
53      *
54      * @param keyType
55      *            Key Type
56      * @param valueType
57      *            Value Type
58      * @return Description of generic type instance
59      */
60     public static ParameterizedType mapTypeFor(Type keyType, Type valueType) {
61         return parameterizedTypeFor(MAP_TYPE, keyType, valueType);
62     }
63
64     /**
65      * Returns an instance of {@link ParameterizedType} describing the typed
66      * {@link Set}<V> with concrete type of value.
67      *
68      * @param valueType
69      *            Value Type
70      * @return Description of generic type instance of Set
71      */
72     public static ParameterizedType setTypeFor(Type valueType) {
73         return parameterizedTypeFor(SET_TYPE, valueType);
74     }
75
76     /**
77      * Returns an instance of {@link ParameterizedType} describing the typed
78      * {@link List}<V> with concrete type of value.
79      *
80      * @param valueType
81      *            Value Type
82      * @return Description of type instance of List
83      */
84     public static ParameterizedType listTypeFor(Type valueType) {
85         return parameterizedTypeFor(LIST_TYPE, valueType);
86     }
87
88     public static GeneratedTransferObject getBaseIdentityTO() {
89         Class<BaseIdentity> cls = BaseIdentity.class;
90         GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(cls.getPackage().getName(), cls.getSimpleName());
91         return gto.toInstance();
92     }
93
94     /**
95      *
96      * @param type
97      * @param parameters
98      * @return
99      */
100     public static ParameterizedType parameterizedTypeFor(Type type, Type... parameters) {
101         return new ParametrizedTypeImpl(type, parameters);
102     }
103
104     public static WildcardType wildcardTypeFor(String packageName, String typeName) {
105         return new WildcardTypeImpl(packageName, typeName);
106     }
107
108     public static ParameterizedType augmentableTypeFor(Type valueType) {
109         final Type augmentable = typeForClass(Augmentable.class);
110         return parameterizedTypeFor(augmentable, valueType);
111     }
112
113     public static ParameterizedType augmentationTypeFor(Type valueType) {
114         final Type augmentation = typeForClass(Augmentation.class);
115         return parameterizedTypeFor(augmentation, valueType);
116     }
117
118     private static class ConcreteTypeImpl extends AbstractBaseType implements ConcreteType {
119         private ConcreteTypeImpl(String pkName, String name) {
120             super(pkName, name);
121         }
122     }
123
124     private static class ParametrizedTypeImpl extends AbstractBaseType implements ParameterizedType {
125         private Type[] actualTypes;
126         private Type rawType;
127
128         @Override
129         public Type[] getActualTypeArguments() {
130
131             return actualTypes;
132         }
133
134         @Override
135         public Type getRawType() {
136             return rawType;
137         }
138
139         public ParametrizedTypeImpl(Type rawType, Type[] actTypes) {
140             super(rawType.getPackageName(), rawType.getName());
141             this.rawType = rawType;
142             this.actualTypes = actTypes;
143         }
144
145     }
146
147     private static class WildcardTypeImpl extends AbstractBaseType implements WildcardType {
148         public WildcardTypeImpl(String packageName, String typeName) {
149             super(packageName, typeName);
150         }
151     }
152
153 }