Merge "Added reusable string codecs to yang-data-impl"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / TypeDefinitionAwareCodec.java
1 package org.opendaylight.yangtools.yang.data.impl.codec;
2
3 import java.math.BigDecimal;
4 import java.math.BigInteger;
5 import java.util.Set;
6
7 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
8
9 import com.google.common.base.Joiner;
10 import com.google.common.collect.ImmutableSet;
11
12 import static org.opendaylight.yangtools.yang.model.util.BaseTypes.*;
13
14 public abstract class TypeDefinitionAwareCodec<T> implements StringCodec<T> {
15
16     private final TypeDefinition<?> typeDefinition;
17     private final Class<T> inputClass;
18
19     public Class<T> getInputClass() {
20         return inputClass;
21     }
22
23     protected TypeDefinitionAwareCodec(TypeDefinition<?> typeDefinition, Class<T> outputClass) {
24         this.typeDefinition = typeDefinition;
25         this.inputClass = outputClass;
26     }
27
28     public TypeDefinition<?> getTypeDefinition() {
29         return typeDefinition;
30     }
31
32     @Override
33     public String serialize(T data) {
34         return data.toString();
35     }
36
37     public static final TypeDefinitionAwareCodec<?> from(TypeDefinition<?> typeDefinition) {
38         while(typeDefinition.getBaseType() != null) {
39             typeDefinition = typeDefinition.getBaseType();
40         }
41         
42         if (BINARY_QNAME.equals(typeDefinition.getQName())) {
43             return new BinaryCodec(typeDefinition);
44         }
45         if (BITS_QNAME.equals(typeDefinition.getQName())) {
46             return new BitsCodec(typeDefinition);
47         }
48         if (BOOLEAN_QNAME.equals(typeDefinition.getQName())) {
49             return new BooleanCodec(typeDefinition);
50         }
51         if (DECIMAL64_QNAME.equals(typeDefinition.getQName())) {
52             return new DecimalCodec(typeDefinition);
53         }
54         if (EMPTY_QNAME.equals(typeDefinition.getQName())) {
55             return new EmptyCodec(typeDefinition);
56         }
57         if (ENUMERATION_QNAME.equals(typeDefinition.getQName())) {
58             return new EnumCodec(typeDefinition);
59         }
60         if (INT8_QNAME.equals(typeDefinition.getQName())) {
61             return new Int8Codec(typeDefinition);
62         }
63         if (INT16_QNAME.equals(typeDefinition.getQName())) {
64             return new Int16Codec(typeDefinition);
65         }
66         if (INT32_QNAME.equals(typeDefinition.getQName())) {
67             return new Int32Codec(typeDefinition);
68         }
69         if (INT64_QNAME.equals(typeDefinition.getQName())) {
70             return new Int64Codec(typeDefinition);
71         }
72         if (STRING_QNAME.equals(typeDefinition.getQName())) {
73             return new StringCodec(typeDefinition);
74         }
75         if (UINT8_QNAME.equals(typeDefinition.getQName())) {
76             return new Uint8Codec(typeDefinition);
77         }
78         if (UINT16_QNAME.equals(typeDefinition.getQName())) {
79             return new Uint16Codec(typeDefinition);
80         }
81         if (UINT32_QNAME.equals(typeDefinition.getQName())) {
82             return new Uint32Codec(typeDefinition);
83         }
84         if (UINT64_QNAME.equals(typeDefinition.getQName())) {
85             return new Uint64Codec(typeDefinition);
86         }
87         return null;
88     }
89
90     public static class BooleanCodec extends TypeDefinitionAwareCodec<Boolean> {
91
92         protected BooleanCodec(TypeDefinition<?> typeDefinition) {
93             super(typeDefinition, Boolean.class);
94         }
95
96         @Override
97         public String serialize(Boolean data) {
98             return data.toString();
99         }
100
101         @Override
102         public Boolean deserialize(String stringRepresentation) {
103             return Boolean.parseBoolean(stringRepresentation);
104         }
105     };
106
107     public static class Uint8Codec extends TypeDefinitionAwareCodec<Short> {
108
109         protected Uint8Codec(TypeDefinition<?> typeDefinition) {
110             super(typeDefinition, Short.class);
111         }
112
113         @Override
114         public String serialize(Short data) {
115             return data.toString();
116         }
117
118         @Override
119         public Short deserialize(String stringRepresentation) {
120             return Short.parseShort(stringRepresentation);
121         }
122     };
123
124     public static class Uint16Codec extends TypeDefinitionAwareCodec<Integer> {
125         protected Uint16Codec(TypeDefinition<?> typeDefinition) {
126             super(typeDefinition, Integer.class);
127         }
128
129         @Override
130         public Integer deserialize(String stringRepresentation) {
131             return Integer.parseInt(stringRepresentation);
132         }
133     };
134
135     public static class Uint32Codec extends TypeDefinitionAwareCodec<Long> {
136
137         protected Uint32Codec(TypeDefinition<?> typeDefinition) {
138             super(typeDefinition, Long.class);
139         }
140
141         @Override
142         public Long deserialize(String stringRepresentation) {
143             return Long.parseLong(stringRepresentation);
144         }
145     };
146
147     public static class Uint64Codec extends TypeDefinitionAwareCodec<BigInteger> {
148
149         protected Uint64Codec(TypeDefinition<?> typeDefinition) {
150             super(typeDefinition, BigInteger.class);
151         }
152
153         @Override
154         public BigInteger deserialize(String stringRepresentation) {
155             // FIXME: Implement codec correctly
156             return BigInteger.valueOf(Long.valueOf(stringRepresentation));
157         }
158     };
159
160     public static class StringCodec extends TypeDefinitionAwareCodec<String> {
161
162         protected StringCodec(TypeDefinition<?> typeDefinition) {
163             super(typeDefinition, String.class);
164         }
165
166         @Override
167         public String deserialize(String stringRepresentation) {
168             return stringRepresentation;
169         }
170     };
171
172     public static class Int16Codec extends TypeDefinitionAwareCodec<Short> {
173
174         protected Int16Codec(TypeDefinition<?> typeDefinition) {
175             super(typeDefinition, Short.class);
176         }
177
178         @Override
179         public Short deserialize(String stringRepresentation) {
180             return Short.valueOf(stringRepresentation);
181         }
182     };
183
184     public static class Int32Codec extends TypeDefinitionAwareCodec<Integer> {
185
186         protected Int32Codec(TypeDefinition<?> typeDefinition) {
187             super(typeDefinition, Integer.class);
188         }
189
190         @Override
191         public Integer deserialize(String stringRepresentation) {
192             return Integer.valueOf(stringRepresentation);
193         }
194     };
195
196     public static class Int64Codec extends TypeDefinitionAwareCodec<Long> {
197
198         protected Int64Codec(TypeDefinition<?> typeDefinition) {
199             super(typeDefinition, Long.class);
200         }
201
202         @Override
203         public Long deserialize(String stringRepresentation) {
204             return Long.parseLong(stringRepresentation);
205         }
206     };
207
208     public static class Int8Codec extends TypeDefinitionAwareCodec<Byte> {
209
210         protected Int8Codec(TypeDefinition<?> typeDefinition) {
211             super(typeDefinition, Byte.class);
212         }
213
214         @Override
215         public Byte deserialize(String stringRepresentation) {
216             return Byte.parseByte(stringRepresentation);
217         }
218     };
219
220     public static class EmptyCodec extends TypeDefinitionAwareCodec<Void> {
221
222         protected EmptyCodec(TypeDefinition<?> typeDefinition) {
223             super(typeDefinition, Void.class);
224         }
225
226         @Override
227         public String serialize(Void data) {
228             return "";
229         }
230
231         @Override
232         public Void deserialize(String stringRepresentation) {
233             return null;
234         }
235     };
236
237     public static final class BinaryCodec extends TypeDefinitionAwareCodec<byte[]> {
238
239         protected BinaryCodec(TypeDefinition<?> typeDefinition) {
240             super(typeDefinition, byte[].class);
241         }
242
243         @Override
244         public String serialize(byte[] data) {
245             // FIXME By YANG Spec
246             return null;
247         }
248
249         @Override
250         public byte[] deserialize(String stringRepresentation) {
251             // FIXME By YANG Spec
252             return null;
253         }
254     };
255
256     public static final class BitsCodec extends TypeDefinitionAwareCodec<Set<String>> {
257
258         @SuppressWarnings("unchecked")
259         protected BitsCodec(TypeDefinition<?> typeDefinition) {
260             super(typeDefinition, (Class<Set<String>>) ((Class<?>) Set.class));
261         }
262
263         @Override
264         public String serialize(Set<String> data) {
265             return Joiner.on(" ").join(data).toString();
266         }
267
268         @Override
269         public Set<String> deserialize(String stringRepresentation) {
270             String[] strings = stringRepresentation.split(" ");
271             return ImmutableSet.copyOf(strings);
272         }
273     };
274
275     public static class EnumCodec extends TypeDefinitionAwareCodec<String> {
276
277         protected EnumCodec(TypeDefinition<?> typeDefinition) {
278             super(typeDefinition, String.class);
279         }
280
281         @Override
282         public String deserialize(String stringRepresentation) {
283             return stringRepresentation;
284         }
285     };
286
287     public static class DecimalCodec extends TypeDefinitionAwareCodec<BigDecimal> {
288
289         protected DecimalCodec(TypeDefinition<?> typeDefinition) {
290             super(typeDefinition, BigDecimal.class);
291         }
292
293         @Override
294         public String serialize(BigDecimal data) {
295             return data.toString();
296         }
297
298         @Override
299         public BigDecimal deserialize(String stringRepresentation) {
300             return new BigDecimal(stringRepresentation);
301         }
302     };
303 }