Do not subclass YangTextSchemaSource in yang-repo-fs
[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.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 FluentFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
55         final T present = cache.getIfPresent(sourceIdentifier);
56         return present != null ? FluentFutures.immediateFluentFuture(present)
57                 : FluentFutures.immediateFailedFluentFuture(new MissingSchemaSourceException("Source not found",
58                     sourceIdentifier));
59     }
60
61     @Override
62     protected void offer(final T source) {
63         final T present = cache.getIfPresent(source.getIdentifier());
64         if (present == null) {
65             cache.put(source.getIdentifier(), source);
66
67             final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
68             final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<T>(source, queue) {
69                 @Override
70                 public void finalizeReferent() {
71                     reg.close();
72                     regs.remove(this);
73                 }
74             };
75
76             regs.add(ref);
77         }
78     }
79
80     @Override
81     public void close() {
82         while (!regs.isEmpty()) {
83             final FinalizablePhantomReference<?> ref = regs.get(0);
84             ref.finalizeReferent();
85         }
86
87         cache.invalidateAll();
88         queue.close();
89     }
90 }