Move model.repo.util classes to model.repo.spi
[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.FluentFuture;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.concurrent.TimeUnit;
20 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
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.AbstractSchemaSourceCache;
25 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
26 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
27 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
28
29 @Beta
30 public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T>
31         implements AutoCloseable {
32     // FIXME: 7.0.0: use a java.util.Cleaner?
33     private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<>());
34     private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
35     private final Cache<SourceIdentifier, T> cache;
36
37     protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation,
38             final CacheBuilder<Object, Object> builder) {
39         super(consumer, representation, Costs.IMMEDIATE);
40         cache = builder.build();
41     }
42
43     public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
44             final SchemaSourceRegistry consumer, final Class<R> representation) {
45         return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
46     }
47
48     public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
49             final SchemaSourceRegistry consumer, final Class<R> representation, final long lifetime,
50             final TimeUnit units) {
51         return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
52                 .expireAfterAccess(lifetime, units));
53     }
54
55     @Override
56     public FluentFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
57         final T present = cache.getIfPresent(sourceIdentifier);
58         return present != null ? FluentFutures.immediateFluentFuture(present)
59                 : FluentFutures.immediateFailedFluentFuture(new MissingSchemaSourceException("Source not found",
60                     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<>(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 }