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 javax.xml.bind.DatatypeConverter;
11 import org.opendaylight.yangtools.yang.data.api.codec.*;
12 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
13 import org.opendaylight.yangtools.yang.model.api.type.*;
15 import com.google.common.base.*;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.io.BaseEncoding;
19 public abstract class TypeDefinitionAwareCodec<J, T extends TypeDefinition<T>> implements DataStringCodec<J> {
21 private final Optional<T> typeDefinition;
22 private final Class<J> inputClass;
24 public static final BinaryCodecStringImpl BINARY_DEFAULT_CODEC = new BinaryCodecStringImpl(
25 Optional.<BinaryTypeDefinition> absent());
27 public static final BitsCodecStringImpl BITS_DEFAULT_CODEC = new BitsCodecStringImpl(
28 Optional.<BitsTypeDefinition> absent());
30 public static final BooleanCodecStringImpl BOOLEAN_DEFAULT_CODEC = new BooleanCodecStringImpl(
31 Optional.<BooleanTypeDefinition> absent());
33 public static final DecimalCodecStringImpl DECIMAL64_DEFAULT_CODEC = new DecimalCodecStringImpl(
34 Optional.<DecimalTypeDefinition> absent());
36 public static final EmptyCodecStringImpl EMPTY_DEFAULT_CODEC = new EmptyCodecStringImpl(
37 Optional.<EmptyTypeDefinition> absent());
39 public static final EnumCodecStringImpl ENUMERATION_DEFAULT_CODEC = new EnumCodecStringImpl(
40 Optional.<EnumTypeDefinition> absent());
42 public static final Int8CodecStringImpl INT8_DEFAULT_CODEC = new Int8CodecStringImpl(
43 Optional.<IntegerTypeDefinition> absent());
45 public static final Int16CodecStringImpl INT16_DEFAULT_CODEC = new Int16CodecStringImpl(
46 Optional.<IntegerTypeDefinition> absent());
48 public static final Int32CodecStringImpl INT32_DEFAULT_CODEC = new Int32CodecStringImpl(
49 Optional.<IntegerTypeDefinition> absent());
51 public static final Int64CodecStringImpl INT64_DEFAULT_CODEC = new Int64CodecStringImpl(
52 Optional.<IntegerTypeDefinition> absent());
54 public static final StringCodecStringImpl STRING_DEFAULT_CODEC = new StringCodecStringImpl(
55 Optional.<StringTypeDefinition> absent());
57 public static final Uint8CodecStringImpl UINT8_DEFAULT_CODEC = new Uint8CodecStringImpl(
58 Optional.<UnsignedIntegerTypeDefinition> absent());
60 public static final Uint16CodecStringImpl UINT16_DEFAULT_CODEC = new Uint16CodecStringImpl(
61 Optional.<UnsignedIntegerTypeDefinition> absent());
63 public static final Uint32CodecStringImpl UINT32_DEFAULT_CODEC = new Uint32CodecStringImpl(
64 Optional.<UnsignedIntegerTypeDefinition> absent());
66 public static final Uint64CodecStringImpl UINT64_DEFAULT_CODEC = new Uint64CodecStringImpl(
67 Optional.<UnsignedIntegerTypeDefinition> absent());
69 public Class<J> getInputClass() {
73 protected TypeDefinitionAwareCodec(Optional<T> typeDefinition, Class<J> outputClass) {
74 Preconditions.checkArgument(outputClass != null, "Output class must be specified.");
75 this.typeDefinition = typeDefinition;
76 this.inputClass = outputClass;
79 public Optional<T> getTypeDefinition() {
80 return typeDefinition;
83 @SuppressWarnings({ "rawtypes", "unchecked" })
84 public static final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> from(TypeDefinition typeDefinition) {
85 final TypeDefinitionAwareCodec codec = fromType(typeDefinition);
86 return (TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>>) codec;
89 public static final <T extends TypeDefinition<T>> TypeDefinitionAwareCodec<?, T> fromType(T typeDefinition) {
90 T superType = typeDefinition;
91 while (superType.getBaseType() != null) {
92 superType = superType.getBaseType();
95 @SuppressWarnings("rawtypes")
96 TypeDefinitionAwareCodec codec = null;
98 if (superType instanceof BinaryTypeDefinition) {
99 codec = BINARY_DEFAULT_CODEC;
100 } else if (superType instanceof BitsTypeDefinition) {
101 codec = BITS_DEFAULT_CODEC;
102 } else if (superType instanceof BooleanTypeDefinition) {
103 codec = BOOLEAN_DEFAULT_CODEC;
104 } else if (superType instanceof DecimalTypeDefinition) {
105 codec = DECIMAL64_DEFAULT_CODEC;
106 } else if (superType instanceof EmptyTypeDefinition) {
107 codec = EMPTY_DEFAULT_CODEC;
108 } else if (superType instanceof EnumTypeDefinition) {
109 codec = ENUMERATION_DEFAULT_CODEC;
110 } else if (superType instanceof IntegerTypeDefinition) {
111 if (INT8_QNAME.equals(superType.getQName())) {
112 codec = INT8_DEFAULT_CODEC;
113 } else if (INT16_QNAME.equals(superType.getQName())) {
114 codec = INT16_DEFAULT_CODEC;
115 } else if (INT32_QNAME.equals(superType.getQName())) {
116 codec = INT32_DEFAULT_CODEC;
117 } else if (INT64_QNAME.equals(superType.getQName())) {
118 codec = INT64_DEFAULT_CODEC;
120 } else if (superType instanceof StringTypeDefinition) {
121 codec = STRING_DEFAULT_CODEC;
122 } else if (superType instanceof UnsignedIntegerTypeDefinition) {
123 if (UINT8_QNAME.equals(superType.getQName())) {
124 codec = UINT8_DEFAULT_CODEC;
126 if (UINT16_QNAME.equals(superType.getQName())) {
127 codec = UINT16_DEFAULT_CODEC;
129 if (UINT32_QNAME.equals(superType.getQName())) {
130 codec = UINT32_DEFAULT_CODEC;
132 if (UINT64_QNAME.equals(superType.getQName())) {
133 codec = UINT64_DEFAULT_CODEC;
136 return (TypeDefinitionAwareCodec<?, T>) codec;
139 public static class BooleanCodecStringImpl extends TypeDefinitionAwareCodec<Boolean, BooleanTypeDefinition>
140 implements BooleanCodec<String> {
142 protected BooleanCodecStringImpl(Optional<BooleanTypeDefinition> typeDef) {
143 super(typeDef, Boolean.class);
147 public String serialize(Boolean data) {
148 return data.toString();
152 public Boolean deserialize(String stringRepresentation) {
153 return Boolean.parseBoolean(stringRepresentation);
157 public static class Uint8CodecStringImpl extends TypeDefinitionAwareCodec<Short, UnsignedIntegerTypeDefinition>
158 implements Uint8Codec<String> {
160 protected Uint8CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
161 super(typeDef, Short.class);
165 public String serialize(Short data) {
166 return data.toString();
170 public Short deserialize(String stringRepresentation) {
171 return Short.parseShort(stringRepresentation);
175 public static class Uint16CodecStringImpl extends TypeDefinitionAwareCodec<Integer, UnsignedIntegerTypeDefinition>
176 implements Uint16Codec<String> {
177 protected Uint16CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
178 super(typeDef, Integer.class);
182 public Integer deserialize(String stringRepresentation) {
183 return Integer.parseInt(stringRepresentation);
187 public String serialize(Integer data) {
188 return data.toString();
192 public static class Uint32CodecStringImpl extends TypeDefinitionAwareCodec<Long, UnsignedIntegerTypeDefinition>
193 implements Uint32Codec<String> {
195 protected Uint32CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
196 super(typeDef, Long.class);
200 public Long deserialize(String stringRepresentation) {
201 return Long.parseLong(stringRepresentation);
205 public String serialize(Long data) {
206 return data.toString();
210 public static class Uint64CodecStringImpl extends
211 TypeDefinitionAwareCodec<BigInteger, UnsignedIntegerTypeDefinition> implements Uint64Codec<String> {
213 protected Uint64CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
214 super(typeDef, BigInteger.class);
218 public BigInteger deserialize(String stringRepresentation) {
219 // FIXME: Implement codec correctly
220 return BigInteger.valueOf(Long.valueOf(stringRepresentation));
224 public String serialize(BigInteger data) {
225 return data.toString();
229 public static class StringCodecStringImpl extends TypeDefinitionAwareCodec<String, StringTypeDefinition> implements
230 StringCodec<String> {
232 protected StringCodecStringImpl(Optional<StringTypeDefinition> typeDef) {
233 super(typeDef, String.class);
237 public String deserialize(String stringRepresentation) {
238 return stringRepresentation;
242 public String serialize(String data) {
243 return data.toString();
247 public static class Int16CodecStringImpl extends TypeDefinitionAwareCodec<Short, IntegerTypeDefinition> implements
250 protected Int16CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
251 super(typeDef, Short.class);
255 public Short deserialize(String stringRepresentation) {
256 return Short.valueOf(stringRepresentation);
260 public String serialize(Short data) {
261 return data.toString();
265 public static class Int32CodecStringImpl extends TypeDefinitionAwareCodec<Integer, IntegerTypeDefinition> implements
268 protected Int32CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
269 super(typeDef, Integer.class);
273 public Integer deserialize(String stringRepresentation) {
274 return Integer.valueOf(stringRepresentation);
278 public String serialize(Integer data) {
279 return data.toString();
283 public static class Int64CodecStringImpl extends TypeDefinitionAwareCodec<Long, IntegerTypeDefinition> implements
286 protected Int64CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
287 super(typeDef, Long.class);
291 public Long deserialize(String stringRepresentation) {
292 return Long.parseLong(stringRepresentation);
296 public String serialize(Long data) {
297 return data.toString();
301 public static class Int8CodecStringImpl extends TypeDefinitionAwareCodec<Byte, IntegerTypeDefinition> implements
304 protected Int8CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
305 super(typeDef, Byte.class);
309 public Byte deserialize(String stringRepresentation) {
310 return Byte.parseByte(stringRepresentation);
314 public String serialize(Byte data) {
315 return data.toString();
319 public static class EmptyCodecStringImpl extends TypeDefinitionAwareCodec<Void, EmptyTypeDefinition> implements
322 protected EmptyCodecStringImpl(Optional<EmptyTypeDefinition> typeDef) {
323 super(typeDef, Void.class);
327 public String serialize(Void data) {
332 public Void deserialize(String stringRepresentation) {
337 public static final class BinaryCodecStringImpl extends TypeDefinitionAwareCodec<byte[], BinaryTypeDefinition>
338 implements BinaryCodec<String> {
340 protected BinaryCodecStringImpl(Optional<BinaryTypeDefinition> typeDef) {
341 super(typeDef, byte[].class);
345 public String serialize(byte[] data) {
346 return BaseEncoding.base64().encode(data);
350 public byte[] deserialize(String stringRepresentation) {
351 return BaseEncoding.base64().decode(stringRepresentation);
355 public static final class BitsCodecStringImpl extends TypeDefinitionAwareCodec<Set<String>, BitsTypeDefinition>
356 implements BitsCodec<String> {
358 @SuppressWarnings("unchecked")
359 protected BitsCodecStringImpl(Optional<BitsTypeDefinition> typeDef) {
360 super(typeDef, (Class<Set<String>>) ((Class<?>) Set.class));
364 public String serialize(Set<String> data) {
365 return Joiner.on(" ").join(data).toString();
369 public Set<String> deserialize(String stringRepresentation) {
370 String[] strings = stringRepresentation.split(" ");
371 return ImmutableSet.copyOf(strings);
375 public static class EnumCodecStringImpl extends TypeDefinitionAwareCodec<String, EnumTypeDefinition> implements
378 protected EnumCodecStringImpl(Optional<EnumTypeDefinition> typeDef) {
379 super(typeDef, String.class);
383 public String deserialize(String stringRepresentation) {
384 return stringRepresentation;
388 public String serialize(String data) {
389 return data.toString();
393 public static class DecimalCodecStringImpl extends TypeDefinitionAwareCodec<BigDecimal, DecimalTypeDefinition>
394 implements DecimalCodec<String> {
396 protected DecimalCodecStringImpl(Optional<DecimalTypeDefinition> typeDef) {
397 super(typeDef, BigDecimal.class);
401 public String serialize(BigDecimal data) {
402 return data.toString();
406 public BigDecimal deserialize(String stringRepresentation) {
407 return new BigDecimal(stringRepresentation);