1 package org.opendaylight.yangtools.yang.model.util.repo;
3 import org.opendaylight.yangtools.concepts.Delegator;
5 import com.google.common.base.Optional;
6 import com.google.common.base.Preconditions;
10 * Utility Source Provider implementation which uses delegate to retrieve
11 * sources and transformation function to convert sources to different
16 * Representation of schema sources used by delegate
18 * Representation of schema sources exposed by this provider
20 public final class TransformingSourceProvider<I, O> implements //
21 AdvancedSchemaSourceProvider<O>, Delegator<AdvancedSchemaSourceProvider<I>> {
23 private final AdvancedSchemaSourceProvider<I> delegate;
24 private final SchemaSourceTransformation<I, O> transformation;
27 * Creates instance of transforming schema source provider which uses
28 * supplied delegate to retrieve sources and transformation to change
29 * sources to different representation.
32 * Delegate which provides sources.
33 * @param transformation
34 * Transformation function which converts sources
35 * @return Instance of TransformingSourceProvider
36 * @throws NullPointerException
37 * if any of arguments is null.
39 public static final <I, O> TransformingSourceProvider<I, O> create(final AdvancedSchemaSourceProvider<I> delegate,
40 final SchemaSourceTransformation<I, O> transformation) {
41 return new TransformingSourceProvider<>(delegate, transformation);
44 private TransformingSourceProvider(final AdvancedSchemaSourceProvider<I> delegate,
45 final SchemaSourceTransformation<I, O> transformation) {
46 this.delegate = Preconditions.checkNotNull(delegate, "delegate must not be null");
47 this.transformation = Preconditions.checkNotNull(transformation, "transformation must not be null");
51 public AdvancedSchemaSourceProvider<I> getDelegate() {
56 public Optional<O> getSchemaSource(final SourceIdentifier sourceIdentifier) {
57 Optional<I> potentialSource = getDelegate().getSchemaSource(sourceIdentifier);
58 if (potentialSource.isPresent()) {
59 I inputSource = potentialSource.get();
60 return Optional.<O> of(transformation.transform(inputSource));
62 return Optional.absent();
66 public Optional<O> getSchemaSource(final String moduleName, final Optional<String> revision) {
67 return getSchemaSource(SourceIdentifier.create(moduleName, revision));