2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.parser.repo;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.SettableFuture;
13 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
14 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
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;
19 class SettableSchemaProvider<T extends SchemaSourceRepresentation> implements SchemaSourceProvider<T> {
21 private final SettableFuture<T> future = SettableFuture.create();
22 private final T schemaSourceRepresentation;
23 private final PotentialSchemaSource<T> potentialSchemaSource;
25 SettableSchemaProvider(final T schemaSourceRepresentation, final SourceIdentifier sourceIdentifier,
26 final Class<T> representation, final int cost) {
27 this.schemaSourceRepresentation = schemaSourceRepresentation;
28 this.potentialSchemaSource = PotentialSchemaSource.create(sourceIdentifier, representation, cost);
31 public static <T extends SchemaSourceRepresentation> SettableSchemaProvider<T> createRemote(
32 final T schemaSourceRepresentation, final Class<T> representation) {
33 return new SettableSchemaProvider<>(schemaSourceRepresentation, schemaSourceRepresentation.getIdentifier(),
34 representation, PotentialSchemaSource.Costs.REMOTE_IO.getValue());
37 public static <T extends SchemaSourceRepresentation> SettableSchemaProvider<T> createImmediate(
38 final T schemaSourceRepresentation, final Class<T> representation) {
39 return new SettableSchemaProvider<>(schemaSourceRepresentation, schemaSourceRepresentation.getIdentifier(),
40 representation, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
44 public ListenableFuture<T> getSource(final SourceIdentifier sourceIdentifier) {
48 public T getSchemaSourceRepresentation() {
49 return schemaSourceRepresentation;
52 public SourceIdentifier getId() {
53 return schemaSourceRepresentation.getIdentifier();
56 public void setResult() {
57 future.set(schemaSourceRepresentation);
60 public void setException(final Throwable ex) {
61 future.setException(ex);
64 public void register(final SchemaSourceRegistry repo) {
65 repo.registerSchemaSource(this, potentialSchemaSource);