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 static java.util.Objects.requireNonNull;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
31 import org.slf4j.LoggerFactory;
33 public abstract class TypeDefinitionAwareCodec<J, T extends TypeDefinition<T>> extends AbstractDataStringCodec<J> {
34 private static final boolean ENABLE_UNION_CODEC =
35 !Boolean.getBoolean("org.opendaylight.yangtools.yang.data.impl.codec.disable-union");
38 if (!ENABLE_UNION_CODEC) {
39 LoggerFactory.getLogger(TypeDefinitionAwareCodec.class).info("Support for unions is disabled");
43 private final @NonNull Class<J> inputClass;
44 private final @NonNull T typeDefinition;
46 protected TypeDefinitionAwareCodec(final Class<J> outputClass, final T typeDefinition) {
47 inputClass = requireNonNull(outputClass);
48 this.typeDefinition = requireNonNull(typeDefinition);
52 public final Class<J> getInputClass() {
56 protected final @NonNull T typeDefinition() {
57 return typeDefinition;
60 @SuppressWarnings("unchecked")
61 public static TypeDefinitionAwareCodec<Object, ?> from(final TypeDefinition<?> typeDefinition) {
62 return (TypeDefinitionAwareCodec<Object, ?>) fromType(typeDefinition);
65 // FIXME: do we want an Optional or a throws instead of @Nullable here?
66 public static @Nullable TypeDefinitionAwareCodec<?, ?> fromType(final TypeDefinition<?> typeDefinition) {
67 if (typeDefinition instanceof BinaryTypeDefinition binaryType) {
68 return BinaryStringCodec.from(binaryType);
69 } else if (typeDefinition instanceof BitsTypeDefinition bitsType) {
70 return BitsStringCodec.from(bitsType);
71 } else if (typeDefinition instanceof BooleanTypeDefinition booleanType) {
72 return BooleanStringCodec.from(booleanType);
73 } else if (typeDefinition instanceof DecimalTypeDefinition decimalType) {
74 return DecimalStringCodec.from(decimalType);
75 } else if (typeDefinition instanceof EmptyTypeDefinition emptyType) {
76 return new EmptyStringCodec(emptyType);
77 } else if (typeDefinition instanceof EnumTypeDefinition enumType) {
78 return EnumStringCodec.from(enumType);
79 } else if (typeDefinition instanceof Int8TypeDefinition int8Type) {
80 return AbstractIntegerStringCodec.from(int8Type);
81 } else if (typeDefinition instanceof Int16TypeDefinition int16Type) {
82 return AbstractIntegerStringCodec.from(int16Type);
83 } else if (typeDefinition instanceof Int32TypeDefinition int32Type) {
84 return AbstractIntegerStringCodec.from(int32Type);
85 } else if (typeDefinition instanceof Int64TypeDefinition int64Type) {
86 return AbstractIntegerStringCodec.from(int64Type);
87 } else if (typeDefinition instanceof StringTypeDefinition stringType) {
88 return StringStringCodec.from(stringType);
89 } else if (typeDefinition instanceof Uint8TypeDefinition uint8Type) {
90 return AbstractIntegerStringCodec.from(uint8Type);
91 } else if (typeDefinition instanceof Uint16TypeDefinition uint16Type) {
92 return AbstractIntegerStringCodec.from(uint16Type);
93 } else if (typeDefinition instanceof Uint32TypeDefinition uint32Type) {
94 return AbstractIntegerStringCodec.from(uint32Type);
95 } else if (typeDefinition instanceof Uint64TypeDefinition uint64Type) {
96 return AbstractIntegerStringCodec.from(uint64Type);
97 } else if (ENABLE_UNION_CODEC && typeDefinition instanceof UnionTypeDefinition unionType) {
98 return UnionStringCodec.from(unionType);