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.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;
23 class SettableSchemaProvider<T extends SchemaSourceRepresentation> implements SchemaSourceProvider<T> {
25 private final SettableFuture<T> future = SettableFuture.create();
26 private final T schemaSourceRepresentation;
27 private final PotentialSchemaSource<T> potentialSchemaSource;
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);
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());
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());
43 public CheckedFuture<T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
44 return Futures.makeChecked(future, new Function<Exception, SchemaSourceException>() {
47 public SchemaSourceException apply(@Nullable final Exception input) {
48 return new SchemaSourceException("Failed", input);
53 public T getSchemaSourceRepresentation() {
54 return schemaSourceRepresentation;
57 public SourceIdentifier getId() {
58 return schemaSourceRepresentation.getIdentifier();
61 public void setResult() {
62 future.set(schemaSourceRepresentation);
64 public void setException(final Throwable ex) {
65 future.setException(ex);
68 public void register(final SchemaSourceRegistry repo) {
69 repo.registerSchemaSource(this, potentialSchemaSource);