+++ /dev/null
-/*
- * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import com.google.common.base.Preconditions;
-import com.google.common.base.Stopwatch;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import java.util.Map;
-import javax.annotation.concurrent.ThreadSafe;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Pre-computed JSONCodecFactory. All possible codecs are created upfront at instantiation time, after which they
- * are available for the cost of a constant lookup.
- *
- * @author Robert Varga
- */
-@ThreadSafe
-final class EagerJSONCodecFactory extends JSONCodecFactory {
- private static final Logger LOG = LoggerFactory.getLogger(EagerJSONCodecFactory.class);
-
- // Weak keys to retire the entry when SchemaContext goes away
- private static final LoadingCache<SchemaContext, EagerJSONCodecFactory> CACHE = CacheBuilder.newBuilder()
- .weakKeys().build(new CacheLoader<SchemaContext, EagerJSONCodecFactory>() {
- @Override
- public EagerJSONCodecFactory load(final SchemaContext key) {
- final Stopwatch sw = Stopwatch.createStarted();
- final LazyJSONCodecFactory lazy = new LazyJSONCodecFactory(key);
- final int visitedLeaves = requestCodecsForChildren(lazy, key);
- sw.stop();
-
- final Map<TypeDefinition<?>, JSONCodec<?>> simple = lazy.getSimpleCodecs();
- final Map<TypedSchemaNode, JSONCodec<?>> complex = lazy.getComplexCodecs();
-
- LOG.debug("{} leaf nodes resulted in {} simple and {} complex codecs in {}", visitedLeaves,
- simple.size(), complex.size(), sw);
- return new EagerJSONCodecFactory(key, simple, complex);
- }
- });
-
- private final Map<TypeDefinition<?>, JSONCodec<?>> simpleCodecs;
- private final Map<TypedSchemaNode, JSONCodec<?>> complexCodecs;
-
- private EagerJSONCodecFactory(final SchemaContext context, final Map<TypeDefinition<?>, JSONCodec<?>> simpleCodecs,
- final Map<TypedSchemaNode, JSONCodec<?>> complexCodecs) {
- super(context);
- this.simpleCodecs = Preconditions.checkNotNull(simpleCodecs);
- this.complexCodecs = Preconditions.checkNotNull(complexCodecs);
- }
-
- static EagerJSONCodecFactory get(final SchemaContext context) {
- return CACHE.getUnchecked(context);
- }
-
- static EagerJSONCodecFactory getIfPresent(final SchemaContext context) {
- return CACHE.getIfPresent(context);
- }
-
- @Override
- JSONCodec<?> lookupComplex(final TypedSchemaNode schema) {
- final JSONCodec<?> ret = complexCodecs.get(schema);
- Preconditions.checkArgument(ret != null, "No codec available for schema %s", schema);
- return ret;
- }
-
- @Override
- JSONCodec<?> lookupSimple(final TypeDefinition<?> type) {
- return simpleCodecs.get(type);
- }
-
- @Override
- JSONCodec<?> getComplex(final TypedSchemaNode schema, final JSONCodec<?> codec) {
- throw new IllegalStateException("Uncached codec for " + schema);
- }
-
- @Override
- JSONCodec<?> getSimple(final TypeDefinition<?> type, final JSONCodec<?> codec) {
- throw new IllegalStateException("Uncached codec for " + type);
- }
-
- static int requestCodecsForChildren(final LazyJSONCodecFactory lazy, final DataNodeContainer parent) {
- int ret = 0;
- for (DataSchemaNode child : parent.getChildNodes()) {
- if (child instanceof TypedSchemaNode) {
- lazy.codecFor((TypedSchemaNode) child);
- ++ret;
- } else if (child instanceof DataNodeContainer) {
- ret += requestCodecsForChildren(lazy, (DataNodeContainer) child);
- }
- }
-
- return ret;
- }
-}
import com.google.common.annotations.Beta;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
+import com.google.common.base.Stopwatch;
import com.google.common.base.Verify;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import java.util.ArrayList;
import java.util.List;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
+import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
+import org.opendaylight.yangtools.yang.data.util.codec.LazyCodecCache;
+import org.opendaylight.yangtools.yang.data.util.codec.NoopCodecCache;
+import org.opendaylight.yangtools.yang.data.util.codec.PrecomputedCodecCache;
+import org.opendaylight.yangtools.yang.data.util.codec.SharedCodecCache;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
* {@link #createLazy(SchemaContext)} and {@link #createSimple(SchemaContext)} for details.
*/
@Beta
-public abstract class JSONCodecFactory {
+public final class JSONCodecFactory {
+ private static final class EagerCacheLoader extends CacheLoader<SchemaContext, JSONCodecFactory> {
+ @Override
+ public JSONCodecFactory load(final SchemaContext key) {
+ final Stopwatch sw = Stopwatch.createStarted();
+ final LazyCodecCache<JSONCodec<?>> lazyCache = new LazyCodecCache<>();
+ final JSONCodecFactory lazy = new JSONCodecFactory(key, lazyCache);
+ final int visitedLeaves = requestCodecsForChildren(lazy, key);
+ sw.stop();
+
+ final PrecomputedCodecCache<JSONCodec<?>> cache = lazyCache.toPrecomputed();
+ LOG.debug("{} leaf nodes resulted in {} simple and {} complex codecs in {}", visitedLeaves,
+ cache.simpleSize(), cache.complexSize(), sw);
+ return new JSONCodecFactory(key, cache);
+ }
+
+ private static int requestCodecsForChildren(final JSONCodecFactory lazy, final DataNodeContainer parent) {
+ int ret = 0;
+ for (DataSchemaNode child : parent.getChildNodes()) {
+ if (child instanceof TypedSchemaNode) {
+ lazy.codecFor((TypedSchemaNode) child);
+ ++ret;
+ } else if (child instanceof DataNodeContainer) {
+ ret += requestCodecsForChildren(lazy, (DataNodeContainer) child);
+ }
+ }
+
+ return ret;
+ }
+ }
+
private static final Logger LOG = LoggerFactory.getLogger(JSONCodecFactory.class);
+ // Weak keys to retire the entry when SchemaContext goes away
+ private static final LoadingCache<SchemaContext, JSONCodecFactory> PRECOMPUTED = CacheBuilder.newBuilder()
+ .weakKeys().build(new EagerCacheLoader());
+
+ // Weak keys to retire the entry when SchemaContext goes away and to force identity-based lookup
+ private static final LoadingCache<SchemaContext, JSONCodecFactory> SHARED = CacheBuilder.newBuilder()
+ .weakKeys().build(new CacheLoader<SchemaContext, JSONCodecFactory>() {
+ @Override
+ public JSONCodecFactory load(final SchemaContext key) {
+ return new JSONCodecFactory(key, new SharedCodecCache<>());
+ }
+ });
+
+
+ private final CodecCache<JSONCodec<?>> cache;
private final SchemaContext schemaContext;
private final JSONCodec<?> iidCodec;
- JSONCodecFactory(final SchemaContext context) {
+ JSONCodecFactory(final SchemaContext context, final CodecCache<JSONCodec<?>> cache) {
this.schemaContext = Preconditions.checkNotNull(context);
+ this.cache = Preconditions.checkNotNull(cache);
iidCodec = new JSONStringInstanceIdentifierCodec(context, this);
}
* @throws NullPointerException if context is null
*/
public static JSONCodecFactory getPrecomputed(final SchemaContext context) {
- return EagerJSONCodecFactory.get(context);
+ return PRECOMPUTED.getUnchecked(context);
}
/**
* @throws NullPointerException if context is null
*/
public static Optional<JSONCodecFactory> getPrecomputedIfAvailable(final SchemaContext context) {
- return Optional.fromNullable(EagerJSONCodecFactory.getIfPresent(context));
+ return Optional.fromNullable(PRECOMPUTED.getIfPresent(context));
}
/**
* @throws NullPointerException if context is null
*/
public static JSONCodecFactory getShared(final SchemaContext context) {
- return SharedJSONCodecFactory.get(context);
+ return SHARED.getUnchecked(context);
}
/**
* @throws NullPointerException if context is null
*/
public static JSONCodecFactory createLazy(final SchemaContext context) {
- return new LazyJSONCodecFactory(context);
+ return new JSONCodecFactory(context, new LazyCodecCache<>());
}
/**
* @throws NullPointerException if context is null.
*/
public static JSONCodecFactory createSimple(final SchemaContext context) {
- return new SimpleJSONCodecFactory(context);
+ return new JSONCodecFactory(context, NoopCodecCache.getInstance());
}
final JSONCodec<?> codecFor(final TypedSchemaNode schema) {
* We assume prevalence is in above order and that caching is effective. We therefore
*/
final TypeDefinition<?> type = schema.getType();
- JSONCodec<?> ret = lookupSimple(type);
+ JSONCodec<?> ret = cache.lookupSimple(type);
if (ret != null) {
LOG.trace("Type {} hit simple {}", type, ret);
return ret;
}
- ret = lookupComplex(schema);
+ ret = cache.lookupComplex(schema);
if (ret != null) {
LOG.trace("Type {} hit complex {}", type, ret);
return ret;
// ... and complex types afterwards
ret = createComplexCodecFor(schema, type);
LOG.trace("Type {} miss complex {}", type, ret);
- return getComplex(schema, ret);
+ return cache.getComplex(schema, ret);
}
final SchemaContext getSchemaContext() {
return schemaContext;
}
- /**
- * Lookup a complex codec for schema node.
- *
- * @param schema Schema node
- * @return Cached codec, or null if no codec is cached.
- */
- @Nullable abstract JSONCodec<?> lookupComplex(TypedSchemaNode schema);
-
- /**
- * Lookup a simple codec for a type definition.
- *
- * @param type Type definitions
- * @return Cached codec, or null if no codec is cached.
- */
- @Nullable abstract JSONCodec<?> lookupSimple(TypeDefinition<?> type);
-
- /**
- * Lookup-or-store a complex codec for a particular schema node.
- *
- * @param schema Schema node
- * @param codec Codec to cache
- * @return Codec instance, either already-cached, or the codec presented as argument.
- */
- @Nonnull abstract JSONCodec<?> getComplex(TypedSchemaNode schema, JSONCodec<?> codec);
-
- /**
- * Lookup-or-store a simple codec for a particular schema node.
- *
- * @param schema Schema node
- * @param codec Codec to cache
- * @return Codec instance, either already-cached, or the codec presented as argument.
- */
- @Nonnull abstract JSONCodec<?> getSimple(TypeDefinition<?> type, JSONCodec<?> codec);
-
private JSONCodec<?> getSimpleCodecFor(final TypeDefinition<?> type) {
if (type instanceof InstanceIdentifierTypeDefinition) {
// FIXME: there really are two favors, as 'require-instance true' needs to be validated. In order to deal
return null;
}
- return getSimple(type, Verify.verifyNotNull(ret));
+ return cache.getSimple(type, Verify.verifyNotNull(ret));
}
private JSONCodec<?> createComplexCodecFor(final TypedSchemaNode schema, final TypeDefinition<?> type) {
final List<JSONCodec<?>> codecs = new ArrayList<>(types.size());
for (TypeDefinition<?> type : types) {
- JSONCodec<?> codec = lookupSimple(type);
+ JSONCodec<?> codec = cache.lookupSimple(type);
if (codec == null) {
codec = Verify.verifyNotNull(getSimpleCodecFor(type), "Type %s did not resolve to a simple codec",
type);
final List<JSONCodec<?>> codecs = new ArrayList<>(types.size());
for (TypeDefinition<?> type : types) {
- JSONCodec<?> codec = lookupSimple(type);
+ JSONCodec<?> codec = cache.lookupSimple(type);
if (codec == null) {
codec = getSimpleCodecFor(type);
if (codec == null) {
+++ /dev/null
-/*
- * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import java.util.IdentityHashMap;
-import java.util.Map;
-import javax.annotation.concurrent.NotThreadSafe;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
-
-/**
- * Lazily-computed JSONCodecFactory. This is a non-thread-safe factory, which performs caching of codecs. It is most
- * appropriate for one-off encodings of repetitive data.
- *
- * @author Robert Varga
- */
-@NotThreadSafe
-final class LazyJSONCodecFactory extends JSONCodecFactory {
- private final Map<TypedSchemaNode, JSONCodec<?>> complexCodecs = new IdentityHashMap<>();
- private final Map<TypeDefinition<?>, JSONCodec<?>> simpleCodecs = new IdentityHashMap<>();
-
- LazyJSONCodecFactory(final SchemaContext context) {
- super(context);
- }
-
- @Override
- JSONCodec<?> getComplex(final TypedSchemaNode schema, final JSONCodec<?> codec) {
- return getComplexCodecs().computeIfAbsent(schema, any -> codec);
- }
-
- @Override
- JSONCodec<?> lookupComplex(final TypedSchemaNode schema) {
- return getComplexCodecs().get(schema);
- }
-
- @Override
- JSONCodec<?> lookupSimple(final TypeDefinition<?> type) {
- return getSimpleCodecs().get(type);
- }
-
- @Override
- JSONCodec<?> getSimple(final TypeDefinition<?> type, final JSONCodec<?> codec) {
- return getSimpleCodecs().computeIfAbsent(type, any -> codec);
- }
-
- Map<TypeDefinition<?>, JSONCodec<?>> getSimpleCodecs() {
- return simpleCodecs;
- }
-
- Map<TypedSchemaNode, JSONCodec<?>> getComplexCodecs() {
- return complexCodecs;
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import com.google.common.base.Throwables;
-import com.google.common.cache.Cache;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import java.util.concurrent.ExecutionException;
-import javax.annotation.concurrent.ThreadSafe;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
-
-/**
- * A thread-safe lazily-populated codec factory. Instances are cached in an internal weak/soft cache.
- *
- * @author Robert Varga
- */
-@ThreadSafe
-final class SharedJSONCodecFactory extends JSONCodecFactory {
- // Weak keys to retire the entry when SchemaContext goes away and to force identity-based lookup
- private static final LoadingCache<SchemaContext, SharedJSONCodecFactory> CACHE = CacheBuilder.newBuilder()
- .weakKeys().build(new CacheLoader<SchemaContext, SharedJSONCodecFactory>() {
- @Override
- public SharedJSONCodecFactory load(final SchemaContext key) {
- return new SharedJSONCodecFactory(key);
- }
- });
-
- // Weak keys to force identity lookup
- // Soft values to keep unreferenced codecs around for a bit, but eventually we want them to go away
- private final Cache<TypeDefinition<?>, JSONCodec<?>> simpleCodecs = CacheBuilder.newBuilder().weakKeys()
- .softValues().build();
- private final Cache<TypedSchemaNode, JSONCodec<?>> complexCodecs = CacheBuilder.newBuilder().weakKeys().softValues()
- .build();
-
- SharedJSONCodecFactory(final SchemaContext context) {
- super(context);
- }
-
- static SharedJSONCodecFactory getIfPresent(final SchemaContext context) {
- return CACHE.getIfPresent(context);
- }
-
- static SharedJSONCodecFactory get(final SchemaContext context) {
- return CACHE.getUnchecked(context);
- }
-
- @Override
- JSONCodec<?> lookupComplex(final TypedSchemaNode schema) {
- return complexCodecs.getIfPresent(schema);
- }
-
- @Override
- JSONCodec<?> lookupSimple(final TypeDefinition<?> type) {
- return simpleCodecs.getIfPresent(type);
- }
-
- @Override
- JSONCodec<?> getComplex(final TypedSchemaNode schema, final JSONCodec<?> codec) {
- try {
- return complexCodecs.get(schema, () -> codec);
- } catch (ExecutionException e) {
- throw Throwables.propagate(e.getCause());
- }
- }
-
- @Override
- JSONCodec<?> getSimple(final TypeDefinition<?> type, final JSONCodec<?> codec) {
- try {
- return simpleCodecs.get(type, () -> codec);
- } catch (ExecutionException e) {
- throw Throwables.propagate(e.getCause());
- }
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.codec.gson;
-
-import javax.annotation.concurrent.ThreadSafe;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
-
-/**
- * A simplistic factory, which does not perform any codec caching. Minimizes resident memory footprint at the expense
- * of creating short-lived objects.
- *
- * @author Robert Varga
- */
-@ThreadSafe
-final class SimpleJSONCodecFactory extends JSONCodecFactory {
- SimpleJSONCodecFactory(final SchemaContext context) {
- super(context);
- }
-
- @Override
- JSONCodec<?> lookupComplex(final TypedSchemaNode schema) {
- return null;
- }
-
- @Override
- JSONCodec<?> lookupSimple(final TypeDefinition<?> type) {
- return null;
- }
-
- @Override
- JSONCodec<?> getSimple(final TypeDefinition<?> type, final JSONCodec<?> codec) {
- return codec;
- }
-
- @Override
- JSONCodec<?> getComplex(final TypedSchemaNode schema, final JSONCodec<?> codec) {
- return codec;
- }
-}
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.util.codec;
+
+import com.google.common.annotations.Beta;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
+
+/**
+ * Abstract cache for codecs.
+ *
+ * @author Robert Varga
+ *
+ * @param <T> Codec type
+ */
+@Beta
+public abstract class CodecCache<T> {
+ /**
+ * Lookup a complex codec for schema node.
+ *
+ * @param schema Schema node
+ * @return Cached codec, or null if no codec is cached.
+ */
+ @Nullable public abstract T lookupComplex(TypedSchemaNode schema);
+
+ /**
+ * Lookup a simple codec for a type definition.
+ *
+ * @param type Type definition
+ * @return Cached codec, or null if no codec is cached.
+ */
+ @Nullable public abstract T lookupSimple(TypeDefinition<?> type);
+
+ /**
+ * Lookup-or-store a complex codec for a particular schema node.
+ *
+ * @param schema Schema node
+ * @param codec Codec to cache
+ * @return Codec instance, either already-cached, or the codec presented as argument.
+ */
+ @Nonnull public abstract T getComplex(TypedSchemaNode schema, T codec);
+
+ /**
+ * Lookup-or-store a simple codec for a particular schema node.
+ *
+ * @param type Type definition
+ * @param codec Codec to cache
+ * @return Codec instance, either already-cached, or the codec presented as argument.
+ */
+ @Nonnull public abstract T getSimple(TypeDefinition<?> type, T codec);
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.util.codec;
+
+import com.google.common.annotations.Beta;
+import java.util.IdentityHashMap;
+import java.util.Map;
+import javax.annotation.concurrent.NotThreadSafe;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
+
+/**
+ * Lazily-populated CodecCache. This is a non-thread-safe factory, which performs caching of codecs. It is most
+ * appropriate for one-off encodings of repetitive data.
+ *
+ * @author Robert Varga
+ */
+@Beta
+@NotThreadSafe
+public final class LazyCodecCache<T> extends CodecCache<T> {
+ private final Map<TypedSchemaNode, T> complexCodecs = new IdentityHashMap<>();
+ private final Map<TypeDefinition<?>, T> simpleCodecs = new IdentityHashMap<>();
+
+ @Override
+ public T getComplex(final TypedSchemaNode schema, final T codec) {
+ return complexCodecs.computeIfAbsent(schema, any -> codec);
+ }
+
+ @Override
+ public T lookupComplex(final TypedSchemaNode schema) {
+ return complexCodecs.get(schema);
+ }
+
+ @Override
+ public T lookupSimple(final TypeDefinition<?> type) {
+ return simpleCodecs.get(type);
+ }
+
+ @Override
+ public T getSimple(final TypeDefinition<?> type, final T codec) {
+ return simpleCodecs.computeIfAbsent(type, any -> codec);
+ }
+
+ public PrecomputedCodecCache<T> toPrecomputed() {
+ return new PrecomputedCodecCache<>(simpleCodecs, complexCodecs);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.util.codec;
+
+import com.google.common.annotations.Beta;
+import javax.annotation.concurrent.ThreadSafe;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
+
+/**
+ * A no-operation codec cache.
+ *
+ * @author Robert Varga
+ *
+ * @param <T> Codec type
+ */
+@Beta
+@ThreadSafe
+public final class NoopCodecCache<T> extends CodecCache<T> {
+ private static final NoopCodecCache<?> INSTANCE = new NoopCodecCache<>();
+
+ private NoopCodecCache() {
+ // Hidden
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T> NoopCodecCache<T> getInstance() {
+ return (NoopCodecCache<T>) INSTANCE;
+ }
+
+ @Override
+ public T lookupComplex(final TypedSchemaNode schema) {
+ return null;
+ }
+
+ @Override
+ public T lookupSimple(final TypeDefinition<?> type) {
+ return null;
+ }
+
+ @Override
+ public T getSimple(final TypeDefinition<?> type, final T codec) {
+ return codec;
+ }
+
+ @Override
+ public T getComplex(final TypedSchemaNode schema, final T codec) {
+ return codec;
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.util.codec;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Preconditions;
+import java.util.Map;
+import javax.annotation.concurrent.ThreadSafe;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
+
+/**
+ * Pre-computed CodecCache. All possible codecs are created upfront at instantiation time, after which they are
+ * available for the cost of a constant lookup.
+ *
+ * Instantiation needs to occur through {@link LazyCodecCache#toPrecomputed()} after the lazy cache has been fully
+ * populated.
+ *
+ * @author Robert Varga
+ */
+@Beta
+@ThreadSafe
+public final class PrecomputedCodecCache<T> extends CodecCache<T> {
+ private final Map<TypeDefinition<?>, T> simpleCodecs;
+ private final Map<TypedSchemaNode, T> complexCodecs;
+
+ PrecomputedCodecCache(final Map<TypeDefinition<?>, T> simpleCodecs, final Map<TypedSchemaNode, T> complexCodecs) {
+ this.simpleCodecs = Preconditions.checkNotNull(simpleCodecs);
+ this.complexCodecs = Preconditions.checkNotNull(complexCodecs);
+ }
+
+ @Override
+ public T lookupComplex(final TypedSchemaNode schema) {
+ final T ret = complexCodecs.get(schema);
+ Preconditions.checkArgument(ret != null, "No codec available for schema %s", schema);
+ return ret;
+ }
+
+ @Override
+ public T lookupSimple(final TypeDefinition<?> type) {
+ return simpleCodecs.get(type);
+ }
+
+ @Override
+ public T getComplex(final TypedSchemaNode schema, final T codec) {
+ throw new IllegalStateException("Uncached codec for " + schema);
+ }
+
+ @Override
+ public T getSimple(final TypeDefinition<?> type, final T codec) {
+ throw new IllegalStateException("Uncached codec for " + type);
+ }
+
+ public int complexSize() {
+ return complexCodecs.size();
+ }
+
+ public int simpleSize() {
+ return simpleCodecs.size();
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.util.codec;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Throwables;
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import java.util.concurrent.ExecutionException;
+import javax.annotation.concurrent.ThreadSafe;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
+
+/**
+ * A thread-safe lazily-populated codec cache. Instances are cached in an internal weak/soft cache.
+ *
+ * @author Robert Varga
+ */
+@Beta
+@ThreadSafe
+public final class SharedCodecCache<T> extends CodecCache<T> {
+ // Weak keys to force identity lookup
+ // Soft values to keep unreferenced codecs around for a bit, but eventually we want them to go away
+ private final Cache<TypeDefinition<?>, T> simpleCodecs = CacheBuilder.newBuilder().weakKeys().softValues().build();
+ private final Cache<TypedSchemaNode, T> complexCodecs = CacheBuilder.newBuilder().weakKeys().softValues().build();
+
+ @Override
+ public T lookupComplex(final TypedSchemaNode schema) {
+ return complexCodecs.getIfPresent(schema);
+ }
+
+ @Override
+ public T lookupSimple(final TypeDefinition<?> type) {
+ return simpleCodecs.getIfPresent(type);
+ }
+
+ @Override
+ public T getComplex(final TypedSchemaNode schema, final T codec) {
+ try {
+ return complexCodecs.get(schema, () -> codec);
+ } catch (ExecutionException e) {
+ throw Throwables.propagate(e.getCause());
+ }
+ }
+
+ @Override
+ public T getSimple(final TypeDefinition<?> type, final T codec) {
+ try {
+ return simpleCodecs.get(type, () -> codec);
+ } catch (ExecutionException e) {
+ throw Throwables.propagate(e.getCause());
+ }
+ }
+}