2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.codec;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
13 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
14 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.util.DerivedType;
25 public abstract class TypeDefinitionAwareCodec<J, T extends TypeDefinition<T>> implements DataStringCodec<J> {
26 private final Optional<T> typeDefinition;
27 private final Class<J> inputClass;
30 public Class<J> getInputClass() {
34 protected TypeDefinitionAwareCodec(final Optional<T> typeDefinition, final Class<J> outputClass) {
35 Preconditions.checkArgument(outputClass != null, "Output class must be specified.");
36 this.typeDefinition = typeDefinition;
37 this.inputClass = outputClass;
40 public Optional<T> getTypeDefinition() {
41 return typeDefinition;
44 @SuppressWarnings({ "rawtypes", "unchecked" })
45 public static final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> from(final TypeDefinition typeDefinition) {
46 return (TypeDefinitionAwareCodec)fromType(typeDefinition);
49 @SuppressWarnings("unchecked")
50 public static final <T extends TypeDefinition<T>> TypeDefinitionAwareCodec<?, T> fromType(final T typeDefinition) {
51 // FIXME: this is not necessary with yang.model.util.type
52 final T normalizedType = (T) DerivedType.from(typeDefinition);
53 @SuppressWarnings("rawtypes")
54 final TypeDefinitionAwareCodec codec;
56 if (normalizedType instanceof BinaryTypeDefinition) {
57 codec = BinaryStringCodec.from((BinaryTypeDefinition)normalizedType);
58 } else if (normalizedType instanceof BitsTypeDefinition) {
59 codec = BitsStringCodec.from((BitsTypeDefinition)normalizedType);
60 } else if (normalizedType instanceof BooleanTypeDefinition) {
61 codec = BooleanStringCodec.from((BooleanTypeDefinition)normalizedType);
62 } else if (normalizedType instanceof DecimalTypeDefinition) {
63 codec = DecimalStringCodec.from((DecimalTypeDefinition)normalizedType);
64 } else if (normalizedType instanceof EmptyTypeDefinition) {
65 codec = EmptyStringCodec.INSTANCE;
66 } else if (normalizedType instanceof EnumTypeDefinition) {
67 codec = EnumStringCodec.from((EnumTypeDefinition)normalizedType);
68 } else if (normalizedType instanceof IntegerTypeDefinition) {
69 codec = AbstractIntegerStringCodec.from((IntegerTypeDefinition) normalizedType);
70 } else if (normalizedType instanceof StringTypeDefinition) {
71 codec = StringStringCodec.from((StringTypeDefinition)normalizedType);
72 } else if (normalizedType instanceof UnionTypeDefinition) {
73 codec = UnionStringCodec.from((UnionTypeDefinition)normalizedType);
74 } else if (normalizedType instanceof UnsignedIntegerTypeDefinition) {
75 codec = AbstractIntegerStringCodec.from((UnsignedIntegerTypeDefinition) normalizedType);