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