Promote SchemaSourceRepresentation
[yangtools.git] / parser / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / SettableSchemaProvider.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
9 package org.opendaylight.yangtools.yang.parser.repo;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.SettableFuture;
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;
16 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
17 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
18
19 class SettableSchemaProvider<T extends SourceRepresentation> implements SchemaSourceProvider<T> {
20
21     private final SettableFuture<T> future = SettableFuture.create();
22     private final T schemaSourceRepresentation;
23     private final PotentialSchemaSource<T> potentialSchemaSource;
24
25     SettableSchemaProvider(final T schemaSourceRepresentation, final SourceIdentifier sourceIdentifier,
26             final Class<T> representation, final int cost) {
27         this.schemaSourceRepresentation = schemaSourceRepresentation;
28         potentialSchemaSource = PotentialSchemaSource.create(sourceIdentifier, representation, cost);
29     }
30
31     public static <T extends SourceRepresentation> SettableSchemaProvider<T> createRemote(
32             final T schemaSourceRepresentation, final Class<T> representation) {
33         return new SettableSchemaProvider<>(schemaSourceRepresentation, schemaSourceRepresentation.sourceId(),
34                 representation, PotentialSchemaSource.Costs.REMOTE_IO.getValue());
35     }
36
37     public static <T extends SourceRepresentation> SettableSchemaProvider<T> createImmediate(
38             final T schemaSourceRepresentation, final Class<T> representation) {
39         return new SettableSchemaProvider<>(schemaSourceRepresentation, schemaSourceRepresentation.sourceId(),
40                 representation, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
41     }
42
43     @Override
44     public ListenableFuture<T> getSource(final SourceIdentifier sourceIdentifier) {
45         return future;
46     }
47
48     public T getSchemaSourceRepresentation() {
49         return schemaSourceRepresentation;
50     }
51
52     public SourceIdentifier getId() {
53         return schemaSourceRepresentation.sourceId();
54     }
55
56     public void setResult() {
57         future.set(schemaSourceRepresentation);
58     }
59
60     public void setException(final Throwable ex) {
61         future.setException(ex);
62     }
63
64     public void register(final SchemaSourceRegistry repo) {
65         repo.registerSchemaSource(this, potentialSchemaSource);
66     }
67 }