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.spi;
10 import static java.util.Objects.requireNonNull;
12 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
13 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
14 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
17 * Abstract base class for cache-type SchemaSourceListeners. It needs to be
18 * registered with a {@link SchemaSourceRegistry}, where it gets notifications
19 * from. It performs filtering and {@link #offer(SchemaSourceRepresentation)}s
20 * conforming sources to the subclass.
22 * @param <T> Cached schema source type.
24 public abstract class AbstractSchemaSourceCache<T extends SchemaSourceRepresentation>
25 implements SchemaSourceListener, SchemaSourceProvider<T> {
26 private final SchemaSourceRegistry consumer;
27 private final Class<T> representation;
28 private final Costs cost;
30 protected AbstractSchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation,
32 this.consumer = requireNonNull(consumer);
33 this.representation = requireNonNull(representation);
34 this.cost = requireNonNull(cost);
38 * Offer a schema source in requested representation for caching. Subclasses
39 * need to implement this method to store the schema source. Once they have
40 * determined to cache the source, they should call {@link #register(SourceIdentifier)}.
42 * @param source schema source
44 protected abstract void offer(T source);
47 * Register the presence of a cached schema source with the consumer. Subclasses
48 * need to call this method once they have cached a schema source representation,
49 * or when they have determined they have a schema source is available -- like
50 * when a persistent cache reads its cache index.
52 * @param sourceIdentifier Source identifier
53 * @return schema source registration, which the subclass needs to
54 * {@link SchemaSourceRegistration#close()} once it expunges the source
57 protected final SchemaSourceRegistration<T> register(final SourceIdentifier sourceIdentifier) {
58 final PotentialSchemaSource<T> src = PotentialSchemaSource.create(sourceIdentifier, representation,
60 return consumer.registerSchemaSource(this, src);
64 public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
65 if (representation.isAssignableFrom(source.getType())) {
66 @SuppressWarnings("unchecked")
67 final T src = (T)source;
73 public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
78 public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {