Fix license header violations in yang-parser-api
[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  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7 package org.opendaylight.yangtools.yang.model.repo.util;
8
9 import com.google.common.annotations.Beta;
10 import com.google.common.base.FinalizablePhantomReference;
11 import com.google.common.base.FinalizableReferenceQueue;
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
22 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
24 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
25 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
26
27 @Beta
28 public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T> implements AutoCloseable {
29     private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<FinalizablePhantomReference<T>>());
30     private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
31     private final Cache<SourceIdentifier, T> cache;
32
33     protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation, final CacheBuilder<Object, Object> builder) {
34         super(consumer, representation, Costs.IMMEDIATE);
35         cache = builder.build();
36     }
37
38     public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(final SchemaSourceRegistry consumer, final Class<R> representation) {
39         return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
40     }
41
42     @Override
43     public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
44         final T present = cache.getIfPresent(sourceIdentifier);
45         if (present != null) {
46             return Futures.immediateCheckedFuture(present);
47         }
48
49         return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
50     }
51
52     @Override
53     protected void offer(final T source) {
54         final T present = cache.getIfPresent(source.getIdentifier());
55         if (present == null) {
56             cache.put(source.getIdentifier(), source);
57
58             final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
59             final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<T>(source, queue) {
60                 @Override
61                 public void finalizeReferent() {
62                     reg.close();
63                     regs.remove(this);
64                 }
65             };
66
67             regs.add(ref);
68         }
69     }
70
71     @Override
72     public void close() {
73         while (!regs.isEmpty()) {
74             final FinalizablePhantomReference<?> ref = regs.get(0);
75             ref.finalizeReferent();
76         }
77
78         cache.invalidateAll();
79         queue.close();
80     }
81 }