1 package org.opendaylight.yangtools.yang.data.impl.codec;
3 import static org.opendaylight.yangtools.yang.model.util.BaseTypes.*;
5 import java.math.BigDecimal;
6 import java.math.BigInteger;
9 import org.opendaylight.yangtools.yang.data.api.codec.*;
10 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
11 import org.opendaylight.yangtools.yang.model.api.type.*;
13 import com.google.common.base.*;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.io.BaseEncoding;
17 public abstract class TypeDefinitionAwareCodec<J, T extends TypeDefinition<T>> implements DataStringCodec<J> {
19 private final Optional<T> typeDefinition;
20 private final Class<J> inputClass;
22 public static final BinaryCodecStringImpl BINARY_DEFAULT_CODEC = new BinaryCodecStringImpl(
23 Optional.<BinaryTypeDefinition> absent());
25 public static final BitsCodecStringImpl BITS_DEFAULT_CODEC = new BitsCodecStringImpl(
26 Optional.<BitsTypeDefinition> absent());
28 public static final BooleanCodecStringImpl BOOLEAN_DEFAULT_CODEC = new BooleanCodecStringImpl(
29 Optional.<BooleanTypeDefinition> absent());
31 public static final DecimalCodecStringImpl DECIMAL64_DEFAULT_CODEC = new DecimalCodecStringImpl(
32 Optional.<DecimalTypeDefinition> absent());
34 public static final EmptyCodecStringImpl EMPTY_DEFAULT_CODEC = new EmptyCodecStringImpl(
35 Optional.<EmptyTypeDefinition> absent());
37 public static final EnumCodecStringImpl ENUMERATION_DEFAULT_CODEC = new EnumCodecStringImpl(
38 Optional.<EnumTypeDefinition> absent());
40 public static final Int8CodecStringImpl INT8_DEFAULT_CODEC = new Int8CodecStringImpl(
41 Optional.<IntegerTypeDefinition> absent());
43 public static final Int16CodecStringImpl INT16_DEFAULT_CODEC = new Int16CodecStringImpl(
44 Optional.<IntegerTypeDefinition> absent());
46 public static final Int32CodecStringImpl INT32_DEFAULT_CODEC = new Int32CodecStringImpl(
47 Optional.<IntegerTypeDefinition> absent());
49 public static final Int64CodecStringImpl INT64_DEFAULT_CODEC = new Int64CodecStringImpl(
50 Optional.<IntegerTypeDefinition> absent());
52 public static final StringCodecStringImpl STRING_DEFAULT_CODEC = new StringCodecStringImpl(
53 Optional.<StringTypeDefinition> absent());
55 public static final Uint8CodecStringImpl UINT8_DEFAULT_CODEC = new Uint8CodecStringImpl(
56 Optional.<UnsignedIntegerTypeDefinition> absent());
58 public static final Uint16CodecStringImpl UINT16_DEFAULT_CODEC = new Uint16CodecStringImpl(
59 Optional.<UnsignedIntegerTypeDefinition> absent());
61 public static final Uint32CodecStringImpl UINT32_DEFAULT_CODEC = new Uint32CodecStringImpl(
62 Optional.<UnsignedIntegerTypeDefinition> absent());
64 public static final Uint64CodecStringImpl UINT64_DEFAULT_CODEC = new Uint64CodecStringImpl(
65 Optional.<UnsignedIntegerTypeDefinition> absent());
67 public static final UnionCodecStringImpl UNION_DEFAULT_CODEC = new UnionCodecStringImpl(
68 Optional.<UnionTypeDefinition> absent());
70 public Class<J> getInputClass() {
74 protected TypeDefinitionAwareCodec(Optional<T> typeDefinition, Class<J> outputClass) {
75 Preconditions.checkArgument(outputClass != null, "Output class must be specified.");
76 this.typeDefinition = typeDefinition;
77 this.inputClass = outputClass;
80 public Optional<T> getTypeDefinition() {
81 return typeDefinition;
84 @SuppressWarnings({ "rawtypes", "unchecked" })
85 public static final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> from(TypeDefinition typeDefinition) {
86 final TypeDefinitionAwareCodec codec = fromType(typeDefinition);
87 return (TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>>) codec;
90 public static final <T extends TypeDefinition<T>> TypeDefinitionAwareCodec<?, T> fromType(T typeDefinition) {
91 T superType = typeDefinition;
92 while (superType.getBaseType() != null) {
93 superType = superType.getBaseType();
96 @SuppressWarnings("rawtypes")
97 TypeDefinitionAwareCodec codec = null;
99 if (superType instanceof BinaryTypeDefinition) {
100 codec = BINARY_DEFAULT_CODEC;
101 } else if (superType instanceof BitsTypeDefinition) {
102 codec = BITS_DEFAULT_CODEC;
103 } else if (superType instanceof BooleanTypeDefinition) {
104 codec = BOOLEAN_DEFAULT_CODEC;
105 } else if (superType instanceof DecimalTypeDefinition) {
106 codec = DECIMAL64_DEFAULT_CODEC;
107 } else if (superType instanceof EmptyTypeDefinition) {
108 codec = EMPTY_DEFAULT_CODEC;
109 } else if (superType instanceof EnumTypeDefinition) {
110 codec = ENUMERATION_DEFAULT_CODEC;
111 } else if (superType instanceof IntegerTypeDefinition) {
112 if (INT8_QNAME.equals(superType.getQName())) {
113 codec = INT8_DEFAULT_CODEC;
114 } else if (INT16_QNAME.equals(superType.getQName())) {
115 codec = INT16_DEFAULT_CODEC;
116 } else if (INT32_QNAME.equals(superType.getQName())) {
117 codec = INT32_DEFAULT_CODEC;
118 } else if (INT64_QNAME.equals(superType.getQName())) {
119 codec = INT64_DEFAULT_CODEC;
121 } else if (superType instanceof StringTypeDefinition) {
122 codec = STRING_DEFAULT_CODEC;
123 } else if (superType instanceof UnionTypeDefinition) {
124 codec = UNION_DEFAULT_CODEC;
125 } else if (superType instanceof UnsignedIntegerTypeDefinition) {
126 if (UINT8_QNAME.equals(superType.getQName())) {
127 codec = UINT8_DEFAULT_CODEC;
129 if (UINT16_QNAME.equals(superType.getQName())) {
130 codec = UINT16_DEFAULT_CODEC;
132 if (UINT32_QNAME.equals(superType.getQName())) {
133 codec = UINT32_DEFAULT_CODEC;
135 if (UINT64_QNAME.equals(superType.getQName())) {
136 codec = UINT64_DEFAULT_CODEC;
139 return (TypeDefinitionAwareCodec<?, T>) codec;
142 public static class BooleanCodecStringImpl extends TypeDefinitionAwareCodec<Boolean, BooleanTypeDefinition>
143 implements BooleanCodec<String> {
145 protected BooleanCodecStringImpl(Optional<BooleanTypeDefinition> typeDef) {
146 super(typeDef, Boolean.class);
150 public String serialize(Boolean data) {
151 return data.toString();
155 public Boolean deserialize(String stringRepresentation) {
156 return Boolean.parseBoolean(stringRepresentation);
160 public static class Uint8CodecStringImpl extends TypeDefinitionAwareCodec<Short, UnsignedIntegerTypeDefinition>
161 implements Uint8Codec<String> {
163 protected Uint8CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
164 super(typeDef, Short.class);
168 public String serialize(Short data) {
169 return data.toString();
173 public Short deserialize(String stringRepresentation) {
174 return Short.parseShort(stringRepresentation);
178 public static class Uint16CodecStringImpl extends TypeDefinitionAwareCodec<Integer, UnsignedIntegerTypeDefinition>
179 implements Uint16Codec<String> {
180 protected Uint16CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
181 super(typeDef, Integer.class);
185 public Integer deserialize(String stringRepresentation) {
186 return Integer.parseInt(stringRepresentation);
190 public String serialize(Integer data) {
191 return data.toString();
195 public static class Uint32CodecStringImpl extends TypeDefinitionAwareCodec<Long, UnsignedIntegerTypeDefinition>
196 implements Uint32Codec<String> {
198 protected Uint32CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
199 super(typeDef, Long.class);
203 public Long deserialize(String stringRepresentation) {
204 return Long.parseLong(stringRepresentation);
208 public String serialize(Long data) {
209 return data.toString();
213 public static class Uint64CodecStringImpl extends
214 TypeDefinitionAwareCodec<BigInteger, UnsignedIntegerTypeDefinition> implements Uint64Codec<String> {
216 protected Uint64CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
217 super(typeDef, BigInteger.class);
221 public BigInteger deserialize(String stringRepresentation) {
222 // FIXME: Implement codec correctly
223 return BigInteger.valueOf(Long.valueOf(stringRepresentation));
227 public String serialize(BigInteger data) {
228 return data.toString();
232 public static class StringCodecStringImpl extends TypeDefinitionAwareCodec<String, StringTypeDefinition> implements
233 StringCodec<String> {
235 protected StringCodecStringImpl(Optional<StringTypeDefinition> typeDef) {
236 super(typeDef, String.class);
240 public String deserialize(String stringRepresentation) {
241 return stringRepresentation;
245 public String serialize(String data) {
246 return data.toString();
250 public static class Int16CodecStringImpl extends TypeDefinitionAwareCodec<Short, IntegerTypeDefinition> implements
253 protected Int16CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
254 super(typeDef, Short.class);
258 public Short deserialize(String stringRepresentation) {
259 return Short.valueOf(stringRepresentation);
263 public String serialize(Short data) {
264 return data.toString();
268 public static class Int32CodecStringImpl extends TypeDefinitionAwareCodec<Integer, IntegerTypeDefinition> implements
271 protected Int32CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
272 super(typeDef, Integer.class);
276 public Integer deserialize(String stringRepresentation) {
277 return Integer.valueOf(stringRepresentation);
281 public String serialize(Integer data) {
282 return data.toString();
286 public static class Int64CodecStringImpl extends TypeDefinitionAwareCodec<Long, IntegerTypeDefinition> implements
289 protected Int64CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
290 super(typeDef, Long.class);
294 public Long deserialize(String stringRepresentation) {
295 return Long.parseLong(stringRepresentation);
299 public String serialize(Long data) {
300 return data.toString();
304 public static class Int8CodecStringImpl extends TypeDefinitionAwareCodec<Byte, IntegerTypeDefinition> implements
307 protected Int8CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
308 super(typeDef, Byte.class);
312 public Byte deserialize(String stringRepresentation) {
313 return Byte.parseByte(stringRepresentation);
317 public String serialize(Byte data) {
318 return data.toString();
322 public static class EmptyCodecStringImpl extends TypeDefinitionAwareCodec<Void, EmptyTypeDefinition> implements
325 protected EmptyCodecStringImpl(Optional<EmptyTypeDefinition> typeDef) {
326 super(typeDef, Void.class);
330 public String serialize(Void data) {
335 public Void deserialize(String stringRepresentation) {
340 public static final class BinaryCodecStringImpl extends TypeDefinitionAwareCodec<byte[], BinaryTypeDefinition>
341 implements BinaryCodec<String> {
343 protected BinaryCodecStringImpl(Optional<BinaryTypeDefinition> typeDef) {
344 super(typeDef, byte[].class);
348 public String serialize(byte[] data) {
349 return BaseEncoding.base64().encode(data);
353 public byte[] deserialize(String stringRepresentation) {
354 return BaseEncoding.base64().decode(stringRepresentation);
358 public static final class BitsCodecStringImpl extends TypeDefinitionAwareCodec<Set<String>, BitsTypeDefinition>
359 implements BitsCodec<String> {
361 @SuppressWarnings("unchecked")
362 protected BitsCodecStringImpl(Optional<BitsTypeDefinition> typeDef) {
363 super(typeDef, (Class<Set<String>>) ((Class<?>) Set.class));
367 public String serialize(Set<String> data) {
368 return Joiner.on(" ").join(data).toString();
372 public Set<String> deserialize(String stringRepresentation) {
373 String[] strings = stringRepresentation.split(" ");
374 return ImmutableSet.copyOf(strings);
378 public static class EnumCodecStringImpl extends TypeDefinitionAwareCodec<String, EnumTypeDefinition> implements
381 protected EnumCodecStringImpl(Optional<EnumTypeDefinition> typeDef) {
382 super(typeDef, String.class);
386 public String deserialize(String stringRepresentation) {
387 return stringRepresentation;
391 public String serialize(String data) {
392 return data.toString();
396 public static class DecimalCodecStringImpl extends TypeDefinitionAwareCodec<BigDecimal, DecimalTypeDefinition>
397 implements DecimalCodec<String> {
399 protected DecimalCodecStringImpl(Optional<DecimalTypeDefinition> typeDef) {
400 super(typeDef, BigDecimal.class);
404 public String serialize(BigDecimal data) {
405 return data.toString();
409 public BigDecimal deserialize(String stringRepresentation) {
410 return new BigDecimal(stringRepresentation);
414 public static class UnionCodecStringImpl extends TypeDefinitionAwareCodec<String, UnionTypeDefinition> implements
417 protected UnionCodecStringImpl(Optional<UnionTypeDefinition> typeDef) {
418 super(typeDef, String.class);
422 public String serialize(String data) {
427 public String deserialize(String stringRepresentation) {
428 return stringRepresentation;