e2f3ba708dc7e14d2769d8dac0a372565d3a1279
[yangtools.git] / yang / 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.base.Function;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.SettableFuture;
15 import javax.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
17 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
18 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
19 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
20 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
21 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
22
23 class SettableSchemaProvider<T extends SchemaSourceRepresentation> implements SchemaSourceProvider<T> {
24
25     private final SettableFuture<T> future = SettableFuture.create();
26     private final T schemaSourceRepresentation;
27     private final PotentialSchemaSource<T> potentialSchemaSource;
28
29     SettableSchemaProvider(final T schemaSourceRepresentation, final SourceIdentifier sourceIdentifier,
30             final Class<T> representation, final int cost) {
31         this.schemaSourceRepresentation = schemaSourceRepresentation;
32         this.potentialSchemaSource = PotentialSchemaSource.create(sourceIdentifier, representation, cost);
33     }
34
35     public static <T extends SchemaSourceRepresentation> SettableSchemaProvider<T> createRemote(
36             final T schemaSourceRepresentation, final Class<T> representation) {
37         return new SettableSchemaProvider<>(schemaSourceRepresentation, schemaSourceRepresentation.getIdentifier(),
38                 representation, PotentialSchemaSource.Costs.REMOTE_IO.getValue());
39     }
40
41     public static <T extends SchemaSourceRepresentation> SettableSchemaProvider<T> createImmediate(
42             final T schemaSourceRepresentation, final Class<T> representation) {
43         return new SettableSchemaProvider<>(schemaSourceRepresentation, schemaSourceRepresentation.getIdentifier(),
44                 representation, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
45     }
46
47     @Override
48     public CheckedFuture<T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
49         return Futures.makeChecked(future, new Function<Exception, SchemaSourceException>() {
50             @Nullable
51             @Override
52             public SchemaSourceException apply(@Nullable final Exception input) {
53                 return new SchemaSourceException("Failed", input);
54             }
55         });
56     }
57
58     public T getSchemaSourceRepresentation() {
59         return schemaSourceRepresentation;
60     }
61
62     public SourceIdentifier getId() {
63         return schemaSourceRepresentation.getIdentifier();
64     }
65
66     public void setResult() {
67         future.set(schemaSourceRepresentation);
68     }
69
70     public void setException(final Throwable ex) {
71         future.setException(ex);
72     }
73
74     public void register(final SchemaSourceRegistry repo) {
75         repo.registerSchemaSource(this, potentialSchemaSource);
76     }
77 }