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 java.io.ByteArrayInputStream;
11 import java.io.InputStream;
13 import org.opendaylight.yangtools.concepts.Delegator;
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;
22 * Utility functions for {@link SchemaSourceProvider}
25 public final class SchemaSourceProviders {
27 @SuppressWarnings("rawtypes")
28 private static final SchemaSourceProvider NOOP_PROVIDER = new AdvancedSchemaSourceProvider() {
31 public Optional getSchemaSource(final String moduleName, final Optional revision) {
32 return Optional.absent();
36 public Optional getSchemaSource(final SourceIdentifier sourceIdentifier) {
37 return Optional.absent();
42 @SuppressWarnings("rawtypes")
43 private static final SchemaSourceTransformation IDENTITY_TRANFORMATION = new IdentityTransformation();
45 private static final StringToInputStreamTransformation STRING_TO_INPUTSTREAM_TRANSFORMATION = new StringToInputStreamTransformation();
47 private SchemaSourceProviders() {
48 throw new UnsupportedOperationException("Utility class.");
52 * Returns a noop schema source provider.
54 * Noop schema provider returns {@link Optional#absent()} for each call to
55 * query schema source.
57 * @return A reusable no-operation provider.
59 @SuppressWarnings("unchecked")
60 public static <T> SchemaSourceProvider<T> noopProvider() {
66 * Returns delegating schema source provider which returns InputStream from
67 * supplied String based schema source provider.
70 * @return InputStream-based source provider.
72 public static SchemaSourceProvider<InputStream> inputStreamProviderfromStringProvider(
73 final AdvancedSchemaSourceProvider<String> delegate) {
74 return TransformingSourceProvider.create(delegate, STRING_TO_INPUTSTREAM_TRANSFORMATION);
78 * Returns identity implementation of SchemaSourceTransformation
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.
84 * This implementation is really simple <code>return input;</code>.
86 * @return Identity transformation.
88 @SuppressWarnings("unchecked")
89 public static <I> SchemaSourceTransformation<I, I> identityTransformation() {
90 return IDENTITY_TRANFORMATION;
93 public static <I, O> SchemaSourceTransformation<I, O> schemaSourceTransformationFrom(
94 final Function<I, O> transformation) {
95 return new FunctionBasedSchemaSourceTransformation<I, O>(transformation);
100 * Casts {@link SchemaSourceProvider} to
101 * {@link AdvancedSchemaSourceProvider} or wraps it with utility
102 * implementation if supplied delegate does not implement
103 * {@link AdvancedSchemaSourceProvider}.
105 * @param schemaSourceProvider
107 public static <O> AdvancedSchemaSourceProvider<O> toAdvancedSchemaSourceProvider(
108 final SchemaSourceProvider<O> schemaSourceProvider) {
109 if (schemaSourceProvider instanceof AdvancedSchemaSourceProvider<?>) {
110 return (AdvancedSchemaSourceProvider<O>) schemaSourceProvider;
112 return new SchemaSourceCompatibilityWrapper<O>(schemaSourceProvider);
115 private static final class FunctionBasedSchemaSourceTransformation<I, O> implements
116 SchemaSourceTransformation<I, O> {
119 private final Function<I, O> delegate;
121 protected FunctionBasedSchemaSourceTransformation(final Function<I, O> delegate) {
123 this.delegate = Preconditions.checkNotNull(delegate, "delegate MUST NOT be null.");
127 public O transform(final I input) {
128 return delegate.apply(input);
132 public String toString() {
133 return "FunctionBasedSchemaSourceTransformation [delegate=" + delegate + "]";
137 private final static class SchemaSourceCompatibilityWrapper<O> implements //
138 AdvancedSchemaSourceProvider<O>, //
139 Delegator<SchemaSourceProvider<O>> {
141 private final SchemaSourceProvider<O> delegate;
143 public SchemaSourceCompatibilityWrapper(final SchemaSourceProvider<O> delegate) {
144 this.delegate = delegate;
148 public SchemaSourceProvider<O> getDelegate() {
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.
159 * @see org.opendaylight.yangtools.yang.model.util.repo.AdvancedSchemaSourceProvider#getSchemaSource(org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier)
161 @SuppressWarnings("deprecation")
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 @SuppressWarnings("deprecation")
180 public Optional<O> getSchemaSource(final String moduleName, final Optional<String> revision) {
181 return delegate.getSchemaSource(moduleName, revision);
185 @SuppressWarnings("rawtypes")
186 private static class IdentityTransformation implements SchemaSourceTransformation {
189 public Object transform(final Object input) {
194 private static class StringToInputStreamTransformation implements SchemaSourceTransformation<String, InputStream> {
197 public InputStream transform(final String input) {
198 return new ByteArrayInputStream(input.getBytes(Charsets.UTF_8));