Merge "Improved sorting of augmentations before code generation."
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / repo / TransformingSourceProvider.java
1 package org.opendaylight.yangtools.yang.model.util.repo;
2
3 import org.opendaylight.yangtools.concepts.Delegator;
4
5 import com.google.common.base.Optional;
6 import com.google.common.base.Preconditions;
7
8 /**
9  *
10  * Utility Source Provider implementation which uses delegate to retrieve
11  * sources and transformation function to convert sources to different
12  * representation.
13  *
14  *
15  * @param <I>
16  *            Representation of schema sources used by delegate
17  * @param <O>
18  *            Representation of schema sources exposed by this provider
19  */
20 public final class TransformingSourceProvider<I, O> implements //
21         AdvancedSchemaSourceProvider<O>, Delegator<AdvancedSchemaSourceProvider<I>> {
22
23     private final AdvancedSchemaSourceProvider<I> delegate;
24     private final SchemaSourceTransformation<I, O> transformation;
25
26     /**
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.
30      *
31      * @param delegate
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.
38      */
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);
42     }
43
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");
48     }
49
50     @Override
51     public AdvancedSchemaSourceProvider<I> getDelegate() {
52         return delegate;
53     }
54
55     @Override
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));
61         }
62         return Optional.absent();
63     }
64
65     @Override
66     public Optional<O> getSchemaSource(final String moduleName, final Optional<String> revision) {
67         return getSchemaSource(SourceIdentifier.create(moduleName, revision));
68     }
69 }