Remove deprecated JSONCodecFactory methods
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONCodecFactory.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import java.util.List;
14 import java.util.function.BiFunction;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
17 import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
18 import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
19 import org.opendaylight.yangtools.yang.data.impl.codec.BooleanStringCodec;
20 import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
21 import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
22 import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
23 import org.opendaylight.yangtools.yang.data.util.codec.AbstractCodecFactory;
24 import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
45
46 /**
47  * Factory for creating JSON equivalents of codecs. Each instance of this object is bound to
48  * a particular {@link SchemaContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
49  */
50 @Beta
51 public final class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
52     private final JSONCodec<?> iidCodec;
53
54     JSONCodecFactory(final SchemaContext context, final CodecCache<JSONCodec<?>> cache,
55             final BiFunction<SchemaContext, JSONCodecFactory, JSONInstanceIdentifierCodec> iidCodecSupplier) {
56         super(context, cache);
57         iidCodec = verifyNotNull(iidCodecSupplier.apply(context, this));
58     }
59
60     @Override
61     protected JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
62         return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
63     }
64
65     @Override
66     protected JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
67         return new BooleanJSONCodec(BooleanStringCodec.from(type));
68     }
69
70     @Override
71     protected JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
72         return new QuotedJSONCodec<>(BitsStringCodec.from(type));
73     }
74
75     @Override
76     protected JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
77         return new NumberJSONCodec<>(DecimalStringCodec.from(type));
78     }
79
80     @Override
81     protected JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
82         return EmptyJSONCodec.INSTANCE;
83     }
84
85     @Override
86     protected JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
87         return new QuotedJSONCodec<>(EnumStringCodec.from(type));
88     }
89
90     @Override
91     protected JSONCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
92         return new IdentityrefJSONCodec(getSchemaContext(), module);
93     }
94
95     @Override
96     protected JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
97         return iidCodec;
98     }
99
100     @Override
101     protected JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
102         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
103     }
104
105     @Override
106     protected JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
107         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
108     }
109
110     @Override
111     protected JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
112         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
113     }
114
115     @Override
116     protected JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
117         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
118     }
119
120     @Override
121     protected JSONCodec<?> stringCodec(final StringTypeDefinition type) {
122         return new QuotedJSONCodec<>(StringStringCodec.from(type));
123     }
124
125     @Override
126     protected JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
127         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
128     }
129
130     @Override
131     protected JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
132         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
133     }
134
135     @Override
136     protected JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
137         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
138     }
139
140     @Override
141     protected JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
142         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
143     }
144
145     @Override
146     protected JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
147         return UnionJSONCodec.create(type, codecs);
148     }
149
150     @Override
151     protected JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
152         return NullJSONCodec.INSTANCE;
153     }
154 }