BUG-7996: expose different implementations of JSONCodecFactory
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / SharedJSONCodecFactory.java
1 /*
2  * Copyright (c) 2017 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 com.google.common.base.Throwables;
11 import com.google.common.cache.Cache;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import java.util.concurrent.ExecutionException;
16 import javax.annotation.concurrent.ThreadSafe;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
20
21 /**
22  * A thread-safe lazily-populated codec factory. Instances are cached in an internal weak/soft cache.
23  *
24  * @author Robert Varga
25  */
26 @ThreadSafe
27 final class SharedJSONCodecFactory extends JSONCodecFactory {
28     // Weak keys to retire the entry when SchemaContext goes away and to force identity-based lookup
29     private static final LoadingCache<SchemaContext, SharedJSONCodecFactory> CACHE = CacheBuilder.newBuilder()
30             .weakKeys().build(new CacheLoader<SchemaContext, SharedJSONCodecFactory>() {
31                 @Override
32                 public SharedJSONCodecFactory load(final SchemaContext key) {
33                     return new SharedJSONCodecFactory(key);
34                 }
35             });
36
37     // Weak keys to force identity lookup
38     // Soft values to keep unreferenced codecs around for a bit, but eventually we want them to go away
39     private final Cache<TypeDefinition<?>, JSONCodec<?>> simpleCodecs = CacheBuilder.newBuilder().weakKeys()
40             .softValues().build();
41     private final Cache<TypedSchemaNode, JSONCodec<?>> complexCodecs = CacheBuilder.newBuilder().weakKeys().softValues()
42             .build();
43
44     SharedJSONCodecFactory(final SchemaContext context) {
45         super(context);
46     }
47
48     static SharedJSONCodecFactory getIfPresent(final SchemaContext context) {
49         return CACHE.getIfPresent(context);
50     }
51
52     static SharedJSONCodecFactory get(final SchemaContext context) {
53         return CACHE.getUnchecked(context);
54     }
55
56     @Override
57     JSONCodec<?> lookupComplex(final TypedSchemaNode schema) {
58         return complexCodecs.getIfPresent(schema);
59     }
60
61     @Override
62     JSONCodec<?> lookupSimple(final TypeDefinition<?> type) {
63         return simpleCodecs.getIfPresent(type);
64     }
65
66     @Override
67     JSONCodec<?> getComplex(final TypedSchemaNode schema, final JSONCodec<?> codec) {
68         try {
69             return complexCodecs.get(schema, () -> codec);
70         } catch (ExecutionException e) {
71             throw Throwables.propagate(e.getCause());
72         }
73     }
74
75     @Override
76     JSONCodec<?> getSimple(final TypeDefinition<?> type, final JSONCodec<?> codec) {
77         try {
78             return simpleCodecs.get(type, () -> codec);
79         } catch (ExecutionException e) {
80             throw Throwables.propagate(e.getCause());
81         }
82     }
83 }