Fixup more javadocs
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / repo / SchemaSourceProviders.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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/eplv10.html
7  */
8 package org.opendaylight.yangtools.yang.model.util.repo;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.InputStream;
12
13 import org.opendaylight.yangtools.concepts.Delegator;
14
15 import com.google.common.base.Charsets;
16 import com.google.common.base.Function;
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19
20 /**
21  *
22  * Utility functions for {@link SchemaSourceProvider}
23  *
24  */
25 public final class SchemaSourceProviders {
26
27     @SuppressWarnings("rawtypes")
28     private static final SchemaSourceProvider NOOP_PROVIDER = new AdvancedSchemaSourceProvider() {
29
30         @Override
31         public Optional getSchemaSource(final String moduleName, final Optional revision) {
32             return Optional.absent();
33         }
34
35         @Override
36         public Optional getSchemaSource(final SourceIdentifier sourceIdentifier) {
37             return Optional.absent();
38         }
39
40     };
41
42     @SuppressWarnings("rawtypes")
43     private static final SchemaSourceTransformation IDENTITY_TRANFORMATION = new IdentityTransformation();
44
45     private static final StringToInputStreamTransformation STRING_TO_INPUTSTREAM_TRANSFORMATION = new StringToInputStreamTransformation();
46
47     private SchemaSourceProviders() {
48         throw new UnsupportedOperationException("Utility class.");
49     }
50
51     /**
52      * Returns a noop schema source provider.
53      *
54      * Noop schema provider returns {@link Optional#absent()} for each call to
55      * query schema source.
56      *
57      * @return A reusable no-operation provider.
58      */
59     @SuppressWarnings("unchecked")
60     public static <T> SchemaSourceProvider<T> noopProvider() {
61         return NOOP_PROVIDER;
62     }
63
64     /**
65      *
66      * Returns delegating schema source provider which returns InputStream from
67      * supplied String based schema source provider.
68      *
69      * @param delegate
70      * @return InputStream-based source provider.
71      */
72     public static SchemaSourceProvider<InputStream> inputStreamProviderfromStringProvider(
73             final AdvancedSchemaSourceProvider<String> delegate) {
74         return TransformingSourceProvider.create(delegate, STRING_TO_INPUTSTREAM_TRANSFORMATION);
75     }
76
77     /**
78      * Returns identity implementation of SchemaSourceTransformation
79      *
80      * Identity implementation of SchemaSourceTransformation is useful
81      * for usecases where Input and Output of SchemaSourceTransformation
82      * are identitcal, and you want to reuse input as an output.
83      *
84      * This implementation is really simple <code>return input;</code>.
85      *
86      * @return Identity transformation.
87      */
88     @SuppressWarnings("unchecked")
89     public static <I> SchemaSourceTransformation<I, I> identityTransformation() {
90         return IDENTITY_TRANFORMATION;
91     }
92
93     public static <I, O> SchemaSourceTransformation<I, O> schemaSourceTransformationFrom(
94             final Function<I, O> transformation) {
95         return new FunctionBasedSchemaSourceTransformation<I, O>(transformation);
96     }
97
98     /**
99      *
100      * Casts {@link SchemaSourceProvider} to
101      * {@link AdvancedSchemaSourceProvider} or wraps it with utility
102      * implementation if supplied delegate does not implement
103      * {@link AdvancedSchemaSourceProvider}.
104      *
105      * @param schemaSourceProvider
106      */
107     public static <O> AdvancedSchemaSourceProvider<O> toAdvancedSchemaSourceProvider(
108             final SchemaSourceProvider<O> schemaSourceProvider) {
109         if (schemaSourceProvider instanceof AdvancedSchemaSourceProvider<?>) {
110             return (AdvancedSchemaSourceProvider<O>) schemaSourceProvider;
111         }
112         return new SchemaSourceCompatibilityWrapper<O>(schemaSourceProvider);
113     }
114
115     private static final class FunctionBasedSchemaSourceTransformation<I, O> implements
116             SchemaSourceTransformation<I, O> {
117
118
119         private final Function<I, O> delegate;
120
121         protected FunctionBasedSchemaSourceTransformation(final Function<I, O> delegate) {
122             super();
123             this.delegate = Preconditions.checkNotNull(delegate, "delegate MUST NOT be null.");
124         }
125
126         @Override
127         public O transform(final I input) {
128             return delegate.apply(input);
129         }
130
131         @Override
132         public String toString() {
133             return "FunctionBasedSchemaSourceTransformation [delegate=" + delegate + "]";
134         }
135     }
136
137     private final static class SchemaSourceCompatibilityWrapper<O> implements //
138             AdvancedSchemaSourceProvider<O>, //
139             Delegator<SchemaSourceProvider<O>> {
140
141         private final SchemaSourceProvider<O> delegate;
142
143         public SchemaSourceCompatibilityWrapper(final SchemaSourceProvider<O> delegate) {
144             this.delegate = delegate;
145         }
146
147         @Override
148         public SchemaSourceProvider<O> getDelegate() {
149             return delegate;
150         }
151
152
153         /*
154          * Deprecation warnings are suppresed, since this implementation
155          * needs to invoke deprecated method in order to provide
156          * implementation of non-deprecated APIs using legacy ones.
157          *
158          * (non-Javadoc)
159          * @see org.opendaylight.yangtools.yang.model.util.repo.AdvancedSchemaSourceProvider#getSchemaSource(org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier)
160          */
161         @SuppressWarnings("deprecation")
162         @Override
163         public Optional<O> getSchemaSource(final SourceIdentifier sourceIdentifier) {
164
165             final String moduleName = sourceIdentifier.getName();
166             Optional<String> revision = Optional.fromNullable(sourceIdentifier.getRevision());
167             return delegate.getSchemaSource(moduleName, revision);
168         }
169
170         /*
171          * Deprecation warnings are suppresed, since this implementation
172          * needs to invoke deprecated method in order to provide
173          * implementation of non-deprecated APIs using legacy ones.
174          *
175          * (non-Javadoc)
176          * @see org.opendaylight.yangtools.yang.model.util.repo.AdvancedSchemaSourceProvider#getSchemaSource(org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier)
177          */
178         @Override
179         @SuppressWarnings("deprecation")
180         public Optional<O> getSchemaSource(final String moduleName, final Optional<String> revision) {
181             return delegate.getSchemaSource(moduleName, revision);
182         }
183     }
184
185     @SuppressWarnings("rawtypes")
186     private static class  IdentityTransformation implements SchemaSourceTransformation {
187
188         @Override
189         public Object transform(final Object input) {
190             return input;
191         }
192     }
193
194     private static class StringToInputStreamTransformation implements SchemaSourceTransformation<String, InputStream> {
195
196         @Override
197         public InputStream transform(final String input) {
198             return new ByteArrayInputStream(input.getBytes(Charsets.UTF_8));
199         }
200
201     }
202
203 }