- public static final <T extends TypeDefinition<T>> TypeDefinitionAwareCodec<?, T> fromType(T typeDefinition) {
- T superType = typeDefinition;
- while (superType.getBaseType() != null) {
- superType = superType.getBaseType();
- }
-
- @SuppressWarnings("rawtypes")
- TypeDefinitionAwareCodec codec = null;
-
- if (superType instanceof BinaryTypeDefinition) {
- codec = BINARY_DEFAULT_CODEC;
- } else if (superType instanceof BitsTypeDefinition) {
- codec = BITS_DEFAULT_CODEC;
- } else if (superType instanceof BooleanTypeDefinition) {
- codec = BOOLEAN_DEFAULT_CODEC;
- } else if (superType instanceof DecimalTypeDefinition) {
- codec = DECIMAL64_DEFAULT_CODEC;
- } else if (superType instanceof EmptyTypeDefinition) {
- codec = EMPTY_DEFAULT_CODEC;
- } else if (superType instanceof EnumTypeDefinition) {
- codec = ENUMERATION_DEFAULT_CODEC;
- } else if (superType instanceof IntegerTypeDefinition) {
- if (INT8_QNAME.equals(superType.getQName())) {
- codec = INT8_DEFAULT_CODEC;
- } else if (INT16_QNAME.equals(superType.getQName())) {
- codec = INT16_DEFAULT_CODEC;
- } else if (INT32_QNAME.equals(superType.getQName())) {
- codec = INT32_DEFAULT_CODEC;
- } else if (INT64_QNAME.equals(superType.getQName())) {
- codec = INT64_DEFAULT_CODEC;
- }
- } else if (superType instanceof StringTypeDefinition) {
- codec = STRING_DEFAULT_CODEC;
- } else if (superType instanceof UnionTypeDefinition) {
- codec = UNION_DEFAULT_CODEC;
- } else if (superType instanceof UnsignedIntegerTypeDefinition) {
- if (UINT8_QNAME.equals(superType.getQName())) {
- codec = UINT8_DEFAULT_CODEC;
- }
- if (UINT16_QNAME.equals(superType.getQName())) {
- codec = UINT16_DEFAULT_CODEC;
- }
- if (UINT32_QNAME.equals(superType.getQName())) {
- codec = UINT32_DEFAULT_CODEC;
- }
- if (UINT64_QNAME.equals(superType.getQName())) {
- codec = UINT64_DEFAULT_CODEC;
- }
- }
- @SuppressWarnings("unchecked")
- TypeDefinitionAwareCodec<?, T> ret = (TypeDefinitionAwareCodec<?, T>) codec;
- return ret;
- }
-
- public static class BooleanCodecStringImpl extends TypeDefinitionAwareCodec<Boolean, BooleanTypeDefinition>
- implements BooleanCodec<String> {
-
- protected BooleanCodecStringImpl(Optional<BooleanTypeDefinition> typeDef) {
- super(typeDef, Boolean.class);
- }
-
- @Override
- public String serialize(Boolean data) {
- return data.toString();
- }
-
- @Override
- public Boolean deserialize(String stringRepresentation) {
- return Boolean.parseBoolean(stringRepresentation);
- }
- };
-
- public static class Uint8CodecStringImpl extends TypeDefinitionAwareCodec<Short, UnsignedIntegerTypeDefinition>
- implements Uint8Codec<String> {
-
- protected Uint8CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
- super(typeDef, Short.class);
- }
-
- @Override
- public String serialize(Short data) {
- return data.toString();
- }
-
- @Override
- public Short deserialize(String stringRepresentation) {
- return Short.parseShort(stringRepresentation);
- }
- };
-
- public static class Uint16CodecStringImpl extends TypeDefinitionAwareCodec<Integer, UnsignedIntegerTypeDefinition>
- implements Uint16Codec<String> {
- protected Uint16CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
- super(typeDef, Integer.class);
- }
-
- @Override
- public Integer deserialize(String stringRepresentation) {
- return Integer.parseInt(stringRepresentation);
- }
-
- @Override
- public String serialize(Integer data) {
- return data.toString();
- }
- };
-
- public static class Uint32CodecStringImpl extends TypeDefinitionAwareCodec<Long, UnsignedIntegerTypeDefinition>
- implements Uint32Codec<String> {
-
- protected Uint32CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
- super(typeDef, Long.class);
- }
-
- @Override
- public Long deserialize(String stringRepresentation) {
- return Long.parseLong(stringRepresentation);
- }
-
- @Override
- public String serialize(Long data) {
- return data.toString();
- }
- };
-
- public static class Uint64CodecStringImpl extends
- TypeDefinitionAwareCodec<BigInteger, UnsignedIntegerTypeDefinition> implements Uint64Codec<String> {
-
- protected Uint64CodecStringImpl(Optional<UnsignedIntegerTypeDefinition> typeDef) {
- super(typeDef, BigInteger.class);
- }
-
- @Override
- public BigInteger deserialize(String stringRepresentation) {
- // FIXME: Implement codec correctly
- return BigInteger.valueOf(Long.valueOf(stringRepresentation));
- }
-
- @Override
- public String serialize(BigInteger data) {
- return data.toString();
- }
- };
-
- public static class StringCodecStringImpl extends TypeDefinitionAwareCodec<String, StringTypeDefinition> implements
- StringCodec<String> {
-
- protected StringCodecStringImpl(Optional<StringTypeDefinition> typeDef) {
- super(typeDef, String.class);
- }
-
- @Override
- public String deserialize(String stringRepresentation) {
- return stringRepresentation;
- }
-
- @Override
- public String serialize(String data) {
- return data.toString();
- }
- };
-
- public static class Int16CodecStringImpl extends TypeDefinitionAwareCodec<Short, IntegerTypeDefinition> implements
- Int16Codec<String> {
-
- protected Int16CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
- super(typeDef, Short.class);
- }
-
- @Override
- public Short deserialize(String stringRepresentation) {
- return Short.valueOf(stringRepresentation);
- }
-
- @Override
- public String serialize(Short data) {
- return data.toString();
- }
- };
-
- public static class Int32CodecStringImpl extends TypeDefinitionAwareCodec<Integer, IntegerTypeDefinition> implements
- Int32Codec<String> {
-
- protected Int32CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
- super(typeDef, Integer.class);
- }
-
- @Override
- public Integer deserialize(String stringRepresentation) {
- return Integer.valueOf(stringRepresentation);
- }
-
- @Override
- public String serialize(Integer data) {
- return data.toString();
- }
- };
-
- public static class Int64CodecStringImpl extends TypeDefinitionAwareCodec<Long, IntegerTypeDefinition> implements
- Int64Codec<String> {
-
- protected Int64CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
- super(typeDef, Long.class);
- }
-
- @Override
- public Long deserialize(String stringRepresentation) {
- return Long.parseLong(stringRepresentation);
- }
-
- @Override
- public String serialize(Long data) {
- return data.toString();
- }
- };
-
- public static class Int8CodecStringImpl extends TypeDefinitionAwareCodec<Byte, IntegerTypeDefinition> implements
- Int8Codec<String> {
-
- protected Int8CodecStringImpl(Optional<IntegerTypeDefinition> typeDef) {
- super(typeDef, Byte.class);
- }
-
- @Override
- public Byte deserialize(String stringRepresentation) {
- return Byte.parseByte(stringRepresentation);
- }
-
- @Override
- public String serialize(Byte data) {
- return data.toString();
- }
- };
-
- public static class EmptyCodecStringImpl extends TypeDefinitionAwareCodec<Void, EmptyTypeDefinition> implements
- EmptyCodec<String> {
-
- protected EmptyCodecStringImpl(Optional<EmptyTypeDefinition> typeDef) {
- super(typeDef, Void.class);
- }
-
- @Override
- public String serialize(Void data) {
- return "";
- }
-
- @Override
- public Void deserialize(String stringRepresentation) {
+ // FIXME: do we want an Optional or a throws instead of @Nullable here?
+ public static @Nullable TypeDefinitionAwareCodec<?, ?> fromType(final TypeDefinition<?> typeDefinition) {
+ if (typeDefinition instanceof BinaryTypeDefinition) {
+ return BinaryStringCodec.from((BinaryTypeDefinition)typeDefinition);
+ } else if (typeDefinition instanceof BitsTypeDefinition) {
+ return BitsStringCodec.from((BitsTypeDefinition)typeDefinition);
+ } else if (typeDefinition instanceof BooleanTypeDefinition) {
+ return BooleanStringCodec.from((BooleanTypeDefinition)typeDefinition);
+ } else if (typeDefinition instanceof DecimalTypeDefinition) {
+ return DecimalStringCodec.from((DecimalTypeDefinition)typeDefinition);
+ } else if (typeDefinition instanceof EmptyTypeDefinition) {
+ return EmptyStringCodec.INSTANCE;
+ } else if (typeDefinition instanceof EnumTypeDefinition) {
+ return EnumStringCodec.from((EnumTypeDefinition)typeDefinition);
+ } else if (typeDefinition instanceof Int8TypeDefinition) {
+ return AbstractIntegerStringCodec.from((Int8TypeDefinition) typeDefinition);
+ } else if (typeDefinition instanceof Int16TypeDefinition) {
+ return AbstractIntegerStringCodec.from((Int16TypeDefinition) typeDefinition);
+ } else if (typeDefinition instanceof Int32TypeDefinition) {
+ return AbstractIntegerStringCodec.from((Int32TypeDefinition) typeDefinition);
+ } else if (typeDefinition instanceof Int64TypeDefinition) {
+ return AbstractIntegerStringCodec.from((Int64TypeDefinition) typeDefinition);
+ } else if (typeDefinition instanceof StringTypeDefinition) {
+ return StringStringCodec.from((StringTypeDefinition)typeDefinition);
+ } else if (typeDefinition instanceof UnionTypeDefinition) {
+ return UnionStringCodec.from((UnionTypeDefinition)typeDefinition);
+ } else if (typeDefinition instanceof Uint8TypeDefinition) {
+ return AbstractIntegerStringCodec.from((Uint8TypeDefinition) typeDefinition);
+ } else if (typeDefinition instanceof Uint16TypeDefinition) {
+ return AbstractIntegerStringCodec.from((Uint16TypeDefinition) typeDefinition);
+ } else if (typeDefinition instanceof Uint32TypeDefinition) {
+ return AbstractIntegerStringCodec.from((Uint32TypeDefinition) typeDefinition);
+ } else if (typeDefinition instanceof Uint64TypeDefinition) {
+ return AbstractIntegerStringCodec.from((Uint64TypeDefinition) typeDefinition);
+ } else {