2 * Copyright (c) 2017 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 java.util.IdentityHashMap;
12 import javax.annotation.concurrent.NotThreadSafe;
13 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
14 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
18 * Lazily-computed JSONCodecFactory. This is a non-thread-safe factory, which performs caching of codecs. It is most
19 * appropriate for one-off encodings of repetitive data.
21 * @author Robert Varga
24 final class LazyJSONCodecFactory extends JSONCodecFactory {
25 private final Map<TypedSchemaNode, JSONCodec<?>> complexCodecs = new IdentityHashMap<>();
26 private final Map<TypeDefinition<?>, JSONCodec<?>> simpleCodecs = new IdentityHashMap<>();
28 LazyJSONCodecFactory(final SchemaContext context) {
33 JSONCodec<?> getComplex(final TypedSchemaNode schema, final JSONCodec<?> codec) {
34 return getComplexCodecs().computeIfAbsent(schema, any -> codec);
38 JSONCodec<?> lookupComplex(final TypedSchemaNode schema) {
39 return getComplexCodecs().get(schema);
43 JSONCodec<?> lookupSimple(final TypeDefinition<?> type) {
44 return getSimpleCodecs().get(type);
48 JSONCodec<?> getSimple(final TypeDefinition<?> type, final JSONCodec<?> codec) {
49 return getSimpleCodecs().computeIfAbsent(type, any -> codec);
52 Map<TypeDefinition<?>, JSONCodec<?>> getSimpleCodecs() {
56 Map<TypedSchemaNode, JSONCodec<?>> getComplexCodecs() {