Promote SchemaSourceRepresentation
[yangtools.git] / yang / yang-repo-spi / src / main / java / org / opendaylight / yangtools / yang / model / repo / spi / AbstractSchemaSourceCache.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.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.opendaylight.yangtools.concepts.Registration;
13 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
14 import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
15 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
16
17 /**
18  * Abstract base class for cache-type SchemaSourceListeners. It needs to be registered with a
19  * {@link SchemaSourceRegistry}, where it gets notifications from. It performs filtering and
20  * {@link #offer(SourceRepresentation)}s conforming sources to the subclass.
21  *
22  * @param <T> Cached schema source type.
23  */
24 public abstract class AbstractSchemaSourceCache<T extends SourceRepresentation>
25         implements SchemaSourceListener, SchemaSourceProvider<T> {
26     private final SchemaSourceRegistry consumer;
27     private final Class<T> representation;
28     private final Costs cost;
29
30     protected AbstractSchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation,
31             final Costs cost) {
32         this.consumer = requireNonNull(consumer);
33         this.representation = requireNonNull(representation);
34         this.cost = requireNonNull(cost);
35     }
36
37     /**
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)}.
41      *
42      * @param source schema source
43      */
44     protected abstract void offer(T source);
45
46     /**
47      * Register the presence of a cached schema source with the consumer. Subclasses need to call this method once they
48      * have cached a schema source representation, or when they have determined they have a schema source is available
49      * -- like when a persistent cache reads its cache index.
50      *
51      * @param sourceId Source identifier
52      * @return schema source registration, which the subclass needs to {@link Registration#close()} once it expunges the
53      *         source from the cache.
54      */
55     protected final Registration register(final SourceIdentifier sourceId) {
56         return consumer.registerSchemaSource(this, PotentialSchemaSource.create(sourceId, representation,
57             cost.getValue()));
58     }
59
60     @Override
61     public void schemaSourceEncountered(final SourceRepresentation source) {
62         if (representation.isAssignableFrom(source.getType())) {
63             offer(representation.cast(source));
64         }
65     }
66
67     @Override
68     public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
69         // Not interesting
70     }
71
72     @Override
73     public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
74         // Not interesting
75     }
76 }