YANGTOOLS-766: introduce JSONCodecFactorySupplier
[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.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;
46
47 /**
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.
50  *
51  * <p>
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.
55  */
56 @Beta
57 public final class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
58     private final JSONCodec<?> iidCodec;
59
60     JSONCodecFactory(final SchemaContext context, final CodecCache<JSONCodec<?>> cache,
61             final BiFunction<SchemaContext, JSONCodecFactory, JSONStringInstanceIdentifierCodec> iidCodecSupplier) {
62         super(context, cache);
63         iidCodec = verifyNotNull(iidCodecSupplier.apply(context, this));
64     }
65
66     /**
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.
71      *
72      * <p>
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.
77      *
78      * <p>
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)}.
82      *
83      * @param context SchemaContext instance
84      * @return A sharable {@link JSONCodecFactory}
85      * @throws NullPointerException if context is null
86      *
87      * @deprecated Use {@link JSONCodecFactorySupplier#getPrecomputed(SchemaContext)} instead.
88      */
89     @Deprecated
90     public static JSONCodecFactory getPrecomputed(final SchemaContext context) {
91         return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getPrecomputed(context);
92     }
93
94     /**
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.
100      *
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
104      *
105      * @deprecated Use {@link JSONCodecFactorySupplier#getPrecomputedIfAvailable(SchemaContext)} instead.
106      */
107     @Deprecated
108     public static Optional<JSONCodecFactory> getPrecomputedIfAvailable(final SchemaContext context) {
109         return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getPrecomputedIfAvailable(context);
110     }
111
112     /**
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.
116      *
117      * <p>
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.
120      *
121      * @param context SchemaContext instance
122      * @return A sharable {@link JSONCodecFactory}
123      * @throws NullPointerException if context is null
124      *
125      * @deprecated Use {@link JSONCodecFactorySupplier#getShared(SchemaContext)} instead.
126      */
127     @Deprecated
128     public static JSONCodecFactory getShared(final SchemaContext context) {
129         return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context);
130     }
131
132     /**
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
135      * concurrently.
136      *
137      * <p>
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.
140      *
141      * @param context SchemaContext instance
142      * @return A non-sharable {@link JSONCodecFactory}
143      * @throws NullPointerException if context is null
144      *
145      * @deprecated Use {@link JSONCodecFactorySupplier#createLazy(SchemaContext)} instead.
146      */
147     @Deprecated
148     public static JSONCodecFactory createLazy(final SchemaContext context) {
149         return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.createLazy(context);
150     }
151
152     /**
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.
155      *
156      * <p>
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.
160      *
161      * @param context SchemaContext instance
162      * @return A non-sharable {@link JSONCodecFactory}
163      * @throws NullPointerException if context is null.
164      *
165      * @deprecated Use {@link JSONCodecFactorySupplier#createSimple(SchemaContext)} instead.
166      */
167     @Deprecated
168     public static JSONCodecFactory createSimple(final SchemaContext context) {
169         return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.createSimple(context);
170     }
171
172     @Override
173     protected JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
174         return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
175     }
176
177     @Override
178     protected JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
179         return new BooleanJSONCodec(BooleanStringCodec.from(type));
180     }
181
182     @Override
183     protected JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
184         return new QuotedJSONCodec<>(BitsStringCodec.from(type));
185     }
186
187     @Override
188     protected JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
189         return new NumberJSONCodec<>(DecimalStringCodec.from(type));
190     }
191
192     @Override
193     protected JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
194         return EmptyJSONCodec.INSTANCE;
195     }
196
197     @Override
198     protected JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
199         return new QuotedJSONCodec<>(EnumStringCodec.from(type));
200     }
201
202     @Override
203     protected JSONCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
204         return new IdentityrefJSONCodec(getSchemaContext(), module);
205     }
206
207     @Override
208     protected JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
209         return iidCodec;
210     }
211
212     @Override
213     protected JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
214         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
215     }
216
217     @Override
218     protected JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
219         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
220     }
221
222     @Override
223     protected JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
224         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
225     }
226
227     @Override
228     protected JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
229         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
230     }
231
232     @Override
233     protected JSONCodec<?> stringCodec(final StringTypeDefinition type) {
234         return new QuotedJSONCodec<>(StringStringCodec.from(type));
235     }
236
237     @Override
238     protected JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
239         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
240     }
241
242     @Override
243     protected JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
244         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
245     }
246
247     @Override
248     protected JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
249         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
250     }
251
252     @Override
253     protected JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
254         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
255     }
256
257     @Override
258     protected JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
259         return UnionJSONCodec.create(type, codecs);
260     }
261
262     @Override
263     protected JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
264         return NullJSONCodec.INSTANCE;
265     }
266 }