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,
30 final Class<T> representation, final int cost) {
31 this.schemaSourceRepresentation = schemaSourceRepresentation;
32 this.potentialSchemaSource = PotentialSchemaSource.create(sourceIdentifier, representation, cost);
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());
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());
48 public CheckedFuture<T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
49 return Futures.makeChecked(future, new Function<Exception, SchemaSourceException>() {
52 public SchemaSourceException apply(@Nullable final Exception input) {
53 return new SchemaSourceException("Failed", input);
58 public T getSchemaSourceRepresentation() {
59 return schemaSourceRepresentation;
62 public SourceIdentifier getId() {
63 return schemaSourceRepresentation.getIdentifier();
66 public void setResult() {
67 future.set(schemaSourceRepresentation);
70 public void setException(final Throwable ex) {
71 future.setException(ex);
74 public void register(final SchemaSourceRegistry repo) {
75 repo.registerSchemaSource(this, potentialSchemaSource);