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