Cleanup use of Guava library
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / codec / PrecomputedCodecCache.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.util.codec;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import java.util.Map;
15 import javax.annotation.concurrent.ThreadSafe;
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
18
19 /**
20  * Pre-computed CodecCache. All possible codecs are created upfront at instantiation time, after which they are
21  * available for the cost of a constant lookup.
22  *
23  * <p>
24  * Instantiation needs to occur through {@link LazyCodecCache#toPrecomputed()} after the lazy cache has been fully
25  * populated.
26  *
27  * @author Robert Varga
28  */
29 @Beta
30 @ThreadSafe
31 public final class PrecomputedCodecCache<T> extends CodecCache<T> {
32     private final Map<TypeDefinition<?>, T> simpleCodecs;
33     private final Map<TypedSchemaNode, T> complexCodecs;
34
35     PrecomputedCodecCache(final Map<TypeDefinition<?>, T> simpleCodecs, final Map<TypedSchemaNode, T> complexCodecs) {
36         this.simpleCodecs = requireNonNull(simpleCodecs);
37         this.complexCodecs = requireNonNull(complexCodecs);
38     }
39
40     @Override
41     T lookupComplex(final TypedSchemaNode schema) {
42         final T ret = complexCodecs.get(schema);
43         checkArgument(ret != null, "No codec available for schema %s", schema);
44         return ret;
45     }
46
47     @Override
48     T lookupSimple(final TypeDefinition<?> type) {
49         return simpleCodecs.get(type);
50     }
51
52     @Override
53     T getComplex(final TypedSchemaNode schema, final T codec) {
54         throw new IllegalStateException("Uncached codec for " + schema);
55     }
56
57     @Override
58     T getSimple(final TypeDefinition<?> type, final T codec) {
59         throw new IllegalStateException("Uncached codec for " + type);
60     }
61
62     public int complexSize() {
63         return complexCodecs.size();
64     }
65
66     public int simpleSize() {
67         return simpleCodecs.size();
68     }
69 }