Fix license header violations in yang-parser-api
[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 com.google.common.base.Optional;
4 import com.google.common.base.Preconditions;
5 import org.opendaylight.yangtools.concepts.Delegator;
6
7 /**
8  *
9  * Utility Source Provider implementation which uses delegate to retrieve
10  * sources and transformation function to convert sources to different
11  * representation.
12  *
13  *
14  * @param <I>
15  *            Representation of schema sources used by delegate
16  * @param <O>
17  *            Representation of schema sources exposed by this provider
18  *
19  * @deprecated Replaced with {@link org.opendaylight.yangtools.yang.model.repo.util.SchemaSourceTransformer}
20  */
21 @Deprecated
22 public final class TransformingSourceProvider<I, O> implements //
23         AdvancedSchemaSourceProvider<O>, Delegator<AdvancedSchemaSourceProvider<I>> {
24
25     private final AdvancedSchemaSourceProvider<I> delegate;
26     private final SchemaSourceTransformation<I, O> transformation;
27
28     /**
29      * Creates instance of transforming schema source provider which uses
30      * supplied delegate to retrieve sources and transformation to change
31      * sources to different representation.
32      *
33      * @param delegate
34      *            Delegate which provides sources.
35      * @param transformation
36      *            Transformation function which converts sources
37      * @return Instance of TransformingSourceProvider
38      * @throws NullPointerException
39      *             if any of arguments is null.
40      */
41     public static <I, O> TransformingSourceProvider<I, O> create(final AdvancedSchemaSourceProvider<I> delegate,
42             final SchemaSourceTransformation<I, O> transformation) {
43         return new TransformingSourceProvider<>(delegate, transformation);
44     }
45
46     private TransformingSourceProvider(final AdvancedSchemaSourceProvider<I> delegate,
47             final SchemaSourceTransformation<I, O> transformation) {
48         this.delegate = Preconditions.checkNotNull(delegate, "delegate must not be null");
49         this.transformation = Preconditions.checkNotNull(transformation, "transformation must not be null");
50     }
51
52     @Override
53     public AdvancedSchemaSourceProvider<I> getDelegate() {
54         return delegate;
55     }
56
57     @Override
58     public Optional<O> getSchemaSource(final SourceIdentifier sourceIdentifier) {
59         Optional<I> potentialSource = getDelegate().getSchemaSource(sourceIdentifier);
60         if (potentialSource.isPresent()) {
61             I inputSource = potentialSource.get();
62             return Optional.<O> of(transformation.transform(inputSource));
63         }
64         return Optional.absent();
65     }
66
67     @Override
68     public Optional<O> getSchemaSource(final String moduleName, final Optional<String> revision) {
69         return getSchemaSource(SourceIdentifier.create(moduleName, revision));
70     }
71 }