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