BUG-997 Add SourceIdentifier to MissingSchemaSourceException
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / repo / util / InMemorySchemaSourceCache.java
1 /*
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
6  */
7 package org.opendaylight.yangtools.yang.model.repo.util;
8
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;
16
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;
24
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;
29
30         public CacheEntry(final T source, final SchemaSourceRegistration<T> reg) {
31             this.source = Preconditions.checkNotNull(source);
32             this.reg = Preconditions.checkNotNull(reg);
33         }
34     }
35
36     private static final RemovalListener<SourceIdentifier, CacheEntry<?>> LISTENER = new RemovalListener<SourceIdentifier, CacheEntry<?>>() {
37         @Override
38         public void onRemoval(final RemovalNotification<SourceIdentifier, CacheEntry<?>> notification) {
39             notification.getValue().reg.close();
40         }
41     };
42
43     private final Cache<SourceIdentifier, CacheEntry<T>> cache;
44
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();
48     }
49
50     @Override
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);
55         }
56
57         return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
58     }
59
60     @Override
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));
66         }
67     }
68 }