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