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.base.Preconditions;
10 import com.google.common.cache.Cache;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.RemovalListener;
13 import com.google.common.cache.RemovalNotification;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
17 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
18 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
19 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
20 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
21 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
22 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
23 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
25 public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T> {
26 private static final class CacheEntry<T extends SchemaSourceRepresentation> {
27 private final SchemaSourceRegistration<T> reg;
28 private final T source;
30 public CacheEntry(final T source, final SchemaSourceRegistration<T> reg) {
31 this.source = Preconditions.checkNotNull(source);
32 this.reg = Preconditions.checkNotNull(reg);
36 private static final RemovalListener<SourceIdentifier, CacheEntry<?>> LISTENER = new RemovalListener<SourceIdentifier, CacheEntry<?>>() {
38 public void onRemoval(final RemovalNotification<SourceIdentifier, CacheEntry<?>> notification) {
39 notification.getValue().reg.close();
43 private final Cache<SourceIdentifier, CacheEntry<T>> cache;
45 protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation, final int maxSize) {
46 super(consumer, representation, Costs.IMMEDIATE);
47 cache = CacheBuilder.newBuilder().softValues().maximumSize(maxSize).removalListener(LISTENER).build();
51 public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
52 final CacheEntry<T> present = cache.getIfPresent(sourceIdentifier);
53 if (present != null) {
54 return Futures.immediateCheckedFuture(present.source);
57 return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
61 protected void offer(final T source) {
62 final CacheEntry<T> present = cache.getIfPresent(source.getIdentifier());
63 if (present == null) {
64 final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
65 cache.put(source.getIdentifier(), new CacheEntry<T>(source, reg));