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
7 package org.opendaylight.yangtools.yang.model.repo.util;
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;
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;
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();
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());
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);
49 return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
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);
58 final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
59 final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<T>(source, queue) {
61 public void finalizeReferent() {
73 while (!regs.isEmpty()) {
74 final FinalizablePhantomReference<?> ref = regs.get(0);
75 ref.finalizeReferent();
78 cache.invalidateAll();