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 fromType(typeDefinition);
49 @SuppressWarnings("unchecked")
50 public static final <T extends TypeDefinition<T>> TypeDefinitionAwareCodec<?, T> fromType(final T typeDefinition) {
51 final T normalizedType = (T) DerivedType.from(typeDefinition);
52 @SuppressWarnings("rawtypes")
53 final TypeDefinitionAwareCodec codec;
55 if (normalizedType instanceof BinaryTypeDefinition) {
56 codec = BinaryStringCodec.from((BinaryTypeDefinition)normalizedType);
57 } else if (normalizedType instanceof BitsTypeDefinition) {
58 codec = BitsStringCodec.from((BitsTypeDefinition)normalizedType);
59 } else if (normalizedType instanceof BooleanTypeDefinition) {
60 codec = BooleanStringCodec.from((BooleanTypeDefinition)normalizedType);
61 } else if (normalizedType instanceof DecimalTypeDefinition) {
62 codec = DecimalStringCodec.from((DecimalTypeDefinition)normalizedType);
63 } else if (normalizedType instanceof EmptyTypeDefinition) {
64 codec = EmptyStringCodec.INSTANCE;
65 } else if (normalizedType instanceof EnumTypeDefinition) {
66 codec = EnumStringCodec.from((EnumTypeDefinition)normalizedType);
67 } else if (normalizedType instanceof IntegerTypeDefinition) {
68 codec = AbstractIntegerStringCodec.from((IntegerTypeDefinition) normalizedType);
69 } else if (normalizedType instanceof StringTypeDefinition) {
70 codec = StringStringCodec.from((StringTypeDefinition)normalizedType);
71 } else if (normalizedType instanceof UnionTypeDefinition) {
72 codec = UnionStringCodec.from((UnionTypeDefinition)normalizedType);
73 } else if (normalizedType instanceof UnsignedIntegerTypeDefinition) {
74 codec = AbstractIntegerStringCodec.from((UnsignedIntegerTypeDefinition) normalizedType);