2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.model.repo.util;
9 import com.google.common.annotations.Beta;
10 import com.google.common.base.Preconditions;
11 import com.google.common.cache.Cache;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.RemovalListener;
14 import com.google.common.cache.RemovalNotification;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
18 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
19 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
21 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
23 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
24 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
27 public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T> {
28 private static final class CacheEntry<T extends SchemaSourceRepresentation> {
29 private final SchemaSourceRegistration<T> reg;
30 private final T source;
32 public CacheEntry(final T source, final SchemaSourceRegistration<T> reg) {
33 this.source = Preconditions.checkNotNull(source);
34 this.reg = Preconditions.checkNotNull(reg);
38 private static final RemovalListener<SourceIdentifier, CacheEntry<?>> LISTENER = new RemovalListener<SourceIdentifier, CacheEntry<?>>() {
40 public void onRemoval(final RemovalNotification<SourceIdentifier, CacheEntry<?>> notification) {
41 notification.getValue().reg.close();
45 private final Cache<SourceIdentifier, CacheEntry<T>> cache;
47 protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation, final CacheBuilder<Object, Object> builder) {
48 super(consumer, representation, Costs.IMMEDIATE);
49 cache = builder.removalListener(LISTENER).build();
52 public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(final SchemaSourceRegistry consumer, final Class<R> representation) {
53 return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
57 public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
58 final CacheEntry<T> present = cache.getIfPresent(sourceIdentifier);
59 if (present != null) {
60 return Futures.immediateCheckedFuture(present.source);
63 return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
67 protected void offer(final T source) {
68 final CacheEntry<T> present = cache.getIfPresent(source.getIdentifier());
69 if (present == null) {
70 final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
71 cache.put(source.getIdentifier(), new CacheEntry<T>(source, reg));