2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.repo.util;
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 org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
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;
29 public class InMemorySchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T> implements AutoCloseable {
30 private final List<FinalizablePhantomReference<T>> regs = Collections.synchronizedList(new ArrayList<FinalizablePhantomReference<T>>());
31 private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
32 private final Cache<SourceIdentifier, T> cache;
34 protected InMemorySchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation, final CacheBuilder<Object, Object> builder) {
35 super(consumer, representation, Costs.IMMEDIATE);
36 cache = builder.build();
39 public static <R extends SchemaSourceRepresentation> InMemorySchemaSourceCache<R> createSoftCache(final SchemaSourceRegistry consumer, final Class<R> representation) {
40 return new InMemorySchemaSourceCache<>(consumer, representation, CacheBuilder.newBuilder().softValues());
44 public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
45 final T present = cache.getIfPresent(sourceIdentifier);
46 if (present != null) {
47 return Futures.immediateCheckedFuture(present);
50 return Futures.<T, SchemaSourceException>immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
54 protected void offer(final T source) {
55 final T present = cache.getIfPresent(source.getIdentifier());
56 if (present == null) {
57 cache.put(source.getIdentifier(), source);
59 final SchemaSourceRegistration<T> reg = register(source.getIdentifier());
60 final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<T>(source, queue) {
62 public void finalizeReferent() {
74 while (!regs.isEmpty()) {
75 final FinalizablePhantomReference<?> ref = regs.get(0);
76 ref.finalizeReferent();
79 cache.invalidateAll();