2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.util.codec;
10 import com.google.common.annotations.Beta;
11 import java.util.IdentityHashMap;
13 import javax.annotation.concurrent.NotThreadSafe;
14 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
18 * Lazily-populated CodecCache. 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
25 public final class LazyCodecCache<T> extends CodecCache<T> {
26 private final Map<TypedSchemaNode, T> complexCodecs = new IdentityHashMap<>();
27 private final Map<TypeDefinition<?>, T> simpleCodecs = new IdentityHashMap<>();
30 public T getComplex(final TypedSchemaNode schema, final T codec) {
31 return complexCodecs.computeIfAbsent(schema, any -> codec);
35 public T lookupComplex(final TypedSchemaNode schema) {
36 return complexCodecs.get(schema);
40 public T lookupSimple(final TypeDefinition<?> type) {
41 return simpleCodecs.get(type);
45 public T getSimple(final TypeDefinition<?> type, final T codec) {
46 return simpleCodecs.computeIfAbsent(type, any -> codec);
49 public PrecomputedCodecCache<T> toPrecomputed() {
50 return new PrecomputedCodecCache<>(simpleCodecs, complexCodecs);