Drop unneeded generic type specifiers
[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 java.util.concurrent.TimeUnit;
21 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
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> 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, final CacheBuilder<Object, Object> builder) {
36         super(consumer, representation, Costs.IMMEDIATE);
37         cache = builder.build();
38     }
39
40     public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(final SchemaSourceRegistry consumer, final Class<R> representation) {
41         return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
42     }
43
44     public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(
45             final SchemaSourceRegistry consumer, final Class<R> representation, final long lifetime, final TimeUnit units) {
46         return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues()
47                 .expireAfterAccess(lifetime, units));
48     }
49
50     @Override
51     public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
52         final T present = cache.getIfPresent(sourceIdentifier);
53         if (present != null) {
54             return Futures.immediateCheckedFuture(present);
55         }
56
57         return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
58     }
59
60     @Override
61     protected void offer(final T source) {
62         final T present = cache.getIfPresent(source.getIdentifier());
63         if (present == null) {
64             cache.put(source.getIdentifier(), source);
65
66             final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
67             final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<T>(source, queue) {
68                 @Override
69                 public void finalizeReferent() {
70                     reg.close();
71                     regs.remove(this);
72                 }
73             };
74
75             regs.add(ref);
76         }
77     }
78
79     @Override
80     public void close() {
81         while (!regs.isEmpty()) {
82             final FinalizablePhantomReference<?> ref = regs.get(0);
83             ref.finalizeReferent();
84         }
85
86         cache.invalidateAll();
87         queue.close();
88     }
89 }