2 * Copyright (c) 2014 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.codec.gson;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.annotations.Beta;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.function.BiFunction;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
18 import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
19 import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
20 import org.opendaylight.yangtools.yang.data.impl.codec.BooleanStringCodec;
21 import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
22 import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
23 import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
24 import org.opendaylight.yangtools.yang.data.util.codec.AbstractCodecFactory;
25 import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
48 * Factory for creating JSON equivalents of codecs. Each instance of this object is bound to
49 * a particular {@link SchemaContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
52 * There are multiple implementations available, each with distinct thread-safety, CPU/memory trade-offs and reuse
53 * characteristics. See {@link #getShared(SchemaContext)}, {@link #getPrecomputed(SchemaContext)},
54 * {@link #createLazy(SchemaContext)} and {@link #createSimple(SchemaContext)} for details.
57 public final class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
58 private final JSONCodec<?> iidCodec;
60 JSONCodecFactory(final SchemaContext context, final CodecCache<JSONCodec<?>> cache,
61 final BiFunction<SchemaContext, JSONCodecFactory, JSONInstanceIdentifierCodec> iidCodecSupplier) {
62 super(context, cache);
63 iidCodec = verifyNotNull(iidCodecSupplier.apply(context, this));
67 * Get a thread-safe, eagerly-caching {@link JSONCodecFactory} for a SchemaContext. This method can, and will,
68 * return the same instance as long as the associated SchemaContext is present. Returned object can be safely
69 * used by multiple threads concurrently. If the SchemaContext instance does not have a cached instance
70 * of {@link JSONCodecFactory}, it will be completely precomputed before this method will return.
73 * Choosing this implementation is appropriate when the memory overhead of keeping a full codec tree is not as
74 * great a concern as predictable performance. When compared to the implementation returned by
75 * {@link #getShared(SchemaContext)}, this implementation is expected to offer higher performance and have lower
76 * peak memory footprint when most of the SchemaContext is actually in use.
79 * For call sites which do not want to pay the CPU cost of pre-computing this implementation, but still would like
80 * to use it if is available (by being populated by some other caller), you can use
81 * {@link #getPrecomputedIfAvailable(SchemaContext)}.
83 * @param context SchemaContext instance
84 * @return A sharable {@link JSONCodecFactory}
85 * @throws NullPointerException if context is null
87 * @deprecated Use {@link JSONCodecFactorySupplier#getPrecomputed(SchemaContext)} instead.
90 public static JSONCodecFactory getPrecomputed(final SchemaContext context) {
91 return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getPrecomputed(context);
95 * Get a thread-safe, eagerly-caching {@link JSONCodecFactory} for a SchemaContext, if it is available. This
96 * method is a non-blocking equivalent of {@link #getPrecomputed(SchemaContext)} for use in code paths where
97 * the potential of having to pre-compute the implementation is not acceptable. One such scenario is when the
98 * code base wants to opportunistically take advantage of pre-computed version, but is okay with a fallback to
99 * a different implementation.
101 * @param context SchemaContext instance
102 * @return A sharable {@link JSONCodecFactory}, or absent if such an implementation is not available.
103 * @throws NullPointerException if context is null
105 * @deprecated Use {@link JSONCodecFactorySupplier#getPrecomputedIfAvailable(SchemaContext)} instead.
108 public static Optional<JSONCodecFactory> getPrecomputedIfAvailable(final SchemaContext context) {
109 return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getPrecomputedIfAvailable(context);
113 * Get a thread-safe, lazily-caching {@link JSONCodecFactory} for a SchemaContext. This method can, and will,
114 * return the same instance as long as the associated SchemaContext is present or the factory is not invalidated
115 * by memory pressure. Returned object can be safely used by multiple threads concurrently.
118 * Choosing this implementation is a safe default, as it will not incur prohibitive blocking, nor will it tie up
119 * memory in face of pressure.
121 * @param context SchemaContext instance
122 * @return A sharable {@link JSONCodecFactory}
123 * @throws NullPointerException if context is null
125 * @deprecated Use {@link JSONCodecFactorySupplier#getShared(SchemaContext)} instead.
128 public static JSONCodecFactory getShared(final SchemaContext context) {
129 return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context);
133 * Create a new thread-unsafe, lazily-caching {@link JSONCodecFactory} for a SchemaContext. This method will
134 * return distinct objects every time it is invoked. Returned object may not be used from multiple threads
138 * This implementation is appropriate for one-off serialization from a single thread. It will aggressively cache
139 * codecs for reuse and will tie them up in memory until the factory is freed.
141 * @param context SchemaContext instance
142 * @return A non-sharable {@link JSONCodecFactory}
143 * @throws NullPointerException if context is null
145 * @deprecated Use {@link JSONCodecFactorySupplier#createLazy(SchemaContext)} instead.
148 public static JSONCodecFactory createLazy(final SchemaContext context) {
149 return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.createLazy(context);
153 * Create a simplistic, thread-safe {@link JSONCodecFactory} for a {@link SchemaContext}. This method will return
154 * distinct objects every time it is invoked. Returned object may be use from multiple threads concurrently.
157 * This implementation exists mostly for completeness only, as it does not perform any caching at all and each codec
158 * is computed every time it is requested. This may be useful in extremely constrained environments, where memory
159 * footprint is more critical than performance.
161 * @param context SchemaContext instance
162 * @return A non-sharable {@link JSONCodecFactory}
163 * @throws NullPointerException if context is null.
165 * @deprecated Use {@link JSONCodecFactorySupplier#createSimple(SchemaContext)} instead.
168 public static JSONCodecFactory createSimple(final SchemaContext context) {
169 return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.createSimple(context);
173 protected JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
174 return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
178 protected JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
179 return new BooleanJSONCodec(BooleanStringCodec.from(type));
183 protected JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
184 return new QuotedJSONCodec<>(BitsStringCodec.from(type));
188 protected JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
189 return new NumberJSONCodec<>(DecimalStringCodec.from(type));
193 protected JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
194 return EmptyJSONCodec.INSTANCE;
198 protected JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
199 return new QuotedJSONCodec<>(EnumStringCodec.from(type));
203 protected JSONCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
204 return new IdentityrefJSONCodec(getSchemaContext(), module);
208 protected JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
213 protected JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
214 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
218 protected JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
219 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
223 protected JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
224 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
228 protected JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
229 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
233 protected JSONCodec<?> stringCodec(final StringTypeDefinition type) {
234 return new QuotedJSONCodec<>(StringStringCodec.from(type));
238 protected JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
239 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
243 protected JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
244 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
248 protected JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
249 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
253 protected JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
254 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
258 protected JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
259 return UnionJSONCodec.create(type, codecs);
263 protected JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
264 return NullJSONCodec.INSTANCE;