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 com.google.common.base.Preconditions;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
15 import javax.annotation.concurrent.ThreadSafe;
16 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
22 * Pre-computed JSONCodecFactory. All possible codecs are created upfront at instantiation time, after which they
23 * are available for the cost of a constant lookup.
25 * @author Robert Varga
28 final class EagerJSONCodecFactory extends JSONCodecFactory {
29 // Weak keys to retire the entry when SchemaContext goes away
30 // Soft values to keep unreferenced factories around for a bit
31 private static final LoadingCache<SchemaContext, EagerJSONCodecFactory> CACHE = CacheBuilder.newBuilder()
32 .weakKeys().softValues().build(new CacheLoader<SchemaContext, EagerJSONCodecFactory>() {
34 public EagerJSONCodecFactory load(final SchemaContext key) {
35 return new EagerJSONCodecFactory(key);
39 private final Map<TypedSchemaNode, JSONCodec<?>> codecs;
41 EagerJSONCodecFactory(final SchemaContext context) {
43 this.codecs = constructCodecs(context);
46 static EagerJSONCodecFactory getIfPresent(final SchemaContext context) {
47 return CACHE.getIfPresent(context);
50 static EagerJSONCodecFactory get(final SchemaContext context) {
51 return CACHE.getUnchecked(context);
55 JSONCodec<?> codecFor(final TypedSchemaNode schema) {
56 final JSONCodec<?> ret = codecs.get(schema);
57 Preconditions.checkArgument(ret != null, "No codec available for schema %s", schema);
61 private static Map<TypedSchemaNode, JSONCodec<?>> constructCodecs(final SchemaContext context) {
62 final LazyJSONCodecFactory lazy = new LazyJSONCodecFactory(context);
63 requestCodecsForChildren(lazy, context);
64 return lazy.getCodecs();
67 private static void requestCodecsForChildren(final LazyJSONCodecFactory factory, final DataNodeContainer parent) {
68 for (DataSchemaNode child : parent.getChildNodes()) {
69 if (child instanceof TypedSchemaNode) {
70 factory.codecFor(child);
71 } else if (child instanceof DataNodeContainer) {
72 requestCodecsForChildren(factory, (DataNodeContainer)child);