BUG-9043: Remove use of CheckedFuture from YANG components
[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.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
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>
30         implements AutoCloseable {
31     private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<>());
32     private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
33     private final Cache<SourceIdentifier, T> cache;
34
35     protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation,
36             final CacheBuilder<Object, Object> builder) {
37         super(consumer, representation, Costs.IMMEDIATE);
38         cache = builder.build();
39     }
40
41     public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
42             final SchemaSourceRegistry consumer, final Class<R> representation) {
43         return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
44     }
45
46     public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
47             final SchemaSourceRegistry consumer, final Class<R> representation, final long lifetime,
48             final TimeUnit units) {
49         return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
50                 .expireAfterAccess(lifetime, units));
51     }
52
53     @Override
54     public ListenableFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
55         final T present = cache.getIfPresent(sourceIdentifier);
56         if (present != null) {
57             return Futures.immediateFuture(present);
58         }
59
60         return Futures.immediateFailedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
61     }
62
63     @Override
64     protected void offer(final T source) {
65         final T present = cache.getIfPresent(source.getIdentifier());
66         if (present == null) {
67             cache.put(source.getIdentifier(), source);
68
69             final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
70             final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<T>(source, queue) {
71                 @Override
72                 public void finalizeReferent() {
73                     reg.close();
74                     regs.remove(this);
75                 }
76             };
77
78             regs.add(ref);
79         }
80     }
81
82     @Override
83     public void close() {
84         while (!regs.isEmpty()) {
85             final FinalizablePhantomReference<?> ref = regs.get(0);
86             ref.finalizeReferent();
87         }
88
89         cache.invalidateAll();
90         queue.close();
91     }
92 }