2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util.repo;
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;
20 * Utility functions for {@link SchemaSourceProvider}
23 * @deprecated Utility classes for deprecated APIs.
26 public final class SchemaSourceProviders {
28 @SuppressWarnings("rawtypes")
29 private static final SchemaSourceProvider NOOP_PROVIDER = new AdvancedSchemaSourceProvider() {
32 public Optional getSchemaSource(final String moduleName, final Optional revision) {
33 return Optional.absent();
37 public Optional getSchemaSource(final SourceIdentifier sourceIdentifier) {
38 return Optional.absent();
43 @SuppressWarnings("rawtypes")
44 private static final SchemaSourceTransformation IDENTITY_TRANFORMATION = new IdentityTransformation();
46 private static final StringToInputStreamTransformation STRING_TO_INPUTSTREAM_TRANSFORMATION = new StringToInputStreamTransformation();
48 private SchemaSourceProviders() {
49 throw new UnsupportedOperationException("Utility class.");
53 * Returns a noop schema source provider.
55 * Noop schema provider returns {@link Optional#absent()} for each call to
56 * query schema source.
58 * @return A reusable no-operation provider.
60 @SuppressWarnings("unchecked")
61 public static <T> SchemaSourceProvider<T> noopProvider() {
67 * Returns delegating schema source provider which returns InputStream from
68 * supplied String based schema source provider.
71 * @return InputStream-based source provider.
73 public static SchemaSourceProvider<InputStream> inputStreamProviderfromStringProvider(
74 final AdvancedSchemaSourceProvider<String> delegate) {
75 return TransformingSourceProvider.create(delegate, STRING_TO_INPUTSTREAM_TRANSFORMATION);
79 * Returns identity implementation of SchemaSourceTransformation
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.
85 * This implementation is really simple <code>return input;</code>.
87 * @return Identity transformation.
89 @SuppressWarnings("unchecked")
90 public static <I> SchemaSourceTransformation<I, I> identityTransformation() {
91 return IDENTITY_TRANFORMATION;
94 public static <I, O> SchemaSourceTransformation<I, O> schemaSourceTransformationFrom(
95 final Function<I, O> transformation) {
96 return new FunctionBasedSchemaSourceTransformation<I, O>(transformation);
101 * Casts {@link SchemaSourceProvider} to
102 * {@link AdvancedSchemaSourceProvider} or wraps it with utility
103 * implementation if supplied delegate does not implement
104 * {@link AdvancedSchemaSourceProvider}.
106 * @param schemaSourceProvider
108 public static <O> AdvancedSchemaSourceProvider<O> toAdvancedSchemaSourceProvider(
109 final SchemaSourceProvider<O> schemaSourceProvider) {
110 if (schemaSourceProvider instanceof AdvancedSchemaSourceProvider<?>) {
111 return (AdvancedSchemaSourceProvider<O>) schemaSourceProvider;
113 return new SchemaSourceCompatibilityWrapper<O>(schemaSourceProvider);
116 private static final class FunctionBasedSchemaSourceTransformation<I, O> implements
117 SchemaSourceTransformation<I, O> {
120 private final Function<I, O> delegate;
122 protected FunctionBasedSchemaSourceTransformation(final Function<I, O> delegate) {
124 this.delegate = Preconditions.checkNotNull(delegate, "delegate MUST NOT be null.");
128 public O transform(final I input) {
129 return delegate.apply(input);
133 public String toString() {
134 return "FunctionBasedSchemaSourceTransformation [delegate=" + delegate + "]";
138 private final static class SchemaSourceCompatibilityWrapper<O> implements //
139 AdvancedSchemaSourceProvider<O>, //
140 Delegator<SchemaSourceProvider<O>> {
142 private final SchemaSourceProvider<O> delegate;
144 public SchemaSourceCompatibilityWrapper(final SchemaSourceProvider<O> delegate) {
145 this.delegate = delegate;
149 public SchemaSourceProvider<O> getDelegate() {
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.
160 * @see org.opendaylight.yangtools.yang.model.util.repo.AdvancedSchemaSourceProvider#getSchemaSource(org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier)
163 public Optional<O> getSchemaSource(final SourceIdentifier sourceIdentifier) {
165 final String moduleName = sourceIdentifier.getName();
166 Optional<String> revision = Optional.fromNullable(sourceIdentifier.getRevision());
167 return delegate.getSchemaSource(moduleName, revision);
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.
176 * @see org.opendaylight.yangtools.yang.model.util.repo.AdvancedSchemaSourceProvider#getSchemaSource(org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier)
179 public Optional<O> getSchemaSource(final String moduleName, final Optional<String> revision) {
180 return delegate.getSchemaSource(moduleName, revision);
184 @SuppressWarnings("rawtypes")
185 private static class IdentityTransformation implements SchemaSourceTransformation {
188 public Object transform(final Object input) {
193 private static class StringToInputStreamTransformation implements SchemaSourceTransformation<String, InputStream> {
196 public InputStream transform(final String input) {
197 return new ByteArrayInputStream(input.getBytes(Charsets.UTF_8));