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 org.opendaylight.yangtools.yang.model.api.SchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.TypeAware;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
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
24 public final class LazyCodecCache<T> extends CodecCache<T> {
25 private final Map<SchemaNode, T> complexCodecs = new IdentityHashMap<>();
26 private final Map<TypeDefinition<?>, T> simpleCodecs = new IdentityHashMap<>();
29 <S extends SchemaNode & TypeAware> T getComplex(final S schema, final T codec) {
30 return complexCodecs.computeIfAbsent(schema, any -> codec);
34 <S extends SchemaNode & TypeAware> T lookupComplex(final S schema) {
35 return complexCodecs.get(schema);
39 T lookupSimple(final TypeDefinition<?> type) {
40 return simpleCodecs.get(type);
44 T getSimple(final TypeDefinition<?> type, final T codec) {
45 return simpleCodecs.computeIfAbsent(type, any -> codec);
48 public PrecomputedCodecCache<T> toPrecomputed() {
49 return new PrecomputedCodecCache<>(simpleCodecs, complexCodecs);