20e203d0c10473a901c40bcb373b207e22434262
[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  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.repo.util;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.FinalizablePhantomReference;
12 import com.google.common.base.FinalizableReferenceQueue;
13 import com.google.common.cache.Cache;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
25 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
26 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
27
28 @Beta
29 public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T> implements AutoCloseable {
30     private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<FinalizablePhantomReference<T>>());
31     private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
32     private final Cache<SourceIdentifier, T> cache;
33
34     protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation, final CacheBuilder<Object, Object> builder) {
35         super(consumer, representation, Costs.IMMEDIATE);
36         cache = builder.build();
37     }
38
39     public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(final SchemaSourceRegistry consumer, final Class<R> representation) {
40         return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
41     }
42
43     @Override
44     public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
45         final T present = cache.getIfPresent(sourceIdentifier);
46         if (present != null) {
47             return Futures.immediateCheckedFuture(present);
48         }
49
50         return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
51     }
52
53     @Override
54     protected void offer(final T source) {
55         final T present = cache.getIfPresent(source.getIdentifier());
56         if (present == null) {
57             cache.put(source.getIdentifier(), source);
58
59             final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
60             final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<T>(source, queue) {
61                 @Override
62                 public void finalizeReferent() {
63                     reg.close();
64                     regs.remove(this);
65                 }
66             };
67
68             regs.add(ref);
69         }
70     }
71
72     @Override
73     public void close() {
74         while (!regs.isEmpty()) {
75             final FinalizablePhantomReference<?> ref = regs.get(0);
76             ref.finalizeReferent();
77         }
78
79         cache.invalidateAll();
80         queue.close();
81     }
82 }