1a41b15dbd9a9e7d8d9ea7b1aa7a98466a522f13
[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, final Class<T> representation, final int cost) {
30         this.schemaSourceRepresentation = schemaSourceRepresentation;
31         this.potentialSchemaSource = PotentialSchemaSource.create(sourceIdentifier, representation, cost);
32     }
33
34     public static <T extends SchemaSourceRepresentation> SettableSchemaProvider<T> createRemote(final T schemaSourceRepresentation, final Class<T> representation) {
35         return new SettableSchemaProvider<>(schemaSourceRepresentation, schemaSourceRepresentation.getIdentifier(), representation, PotentialSchemaSource.Costs.REMOTE_IO.getValue());
36     }
37
38     public static <T extends SchemaSourceRepresentation> SettableSchemaProvider<T> createImmediate(final T schemaSourceRepresentation, final Class<T> representation) {
39         return new SettableSchemaProvider<>(schemaSourceRepresentation, schemaSourceRepresentation.getIdentifier(), representation, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
40     }
41
42     @Override
43     public CheckedFuture<T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
44         return Futures.makeChecked(future, new Function<Exception, SchemaSourceException>() {
45             @Nullable
46             @Override
47             public SchemaSourceException apply(@Nullable final Exception input) {
48                 return new SchemaSourceException("Failed", input);
49             }
50         });
51     }
52
53     public T getSchemaSourceRepresentation() {
54         return schemaSourceRepresentation;
55     }
56
57     public SourceIdentifier getId() {
58         return schemaSourceRepresentation.getIdentifier();
59     }
60
61     public void setResult() {
62         future.set(schemaSourceRepresentation);
63     }
64     public void setException(final Throwable ex) {
65         future.setException(ex);
66     }
67
68     public void register(final SchemaSourceRegistry repo) {
69         repo.registerSchemaSource(this, potentialSchemaSource);
70     }
71 }