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.repo.api;
10 import com.google.common.base.Objects;
11 import com.google.common.base.Objects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13 import com.google.common.io.ByteSource;
15 import java.io.IOException;
16 import java.io.InputStream;
18 import org.opendaylight.yangtools.concepts.Delegator;
21 * YANG text schema source representation. Exposes an RFC6020 text representation
22 * as an {@link InputStream}.
24 public abstract class YangTextSchemaSource extends ByteSource implements SchemaSourceRepresentation {
25 private final SourceIdentifier identifier;
27 protected YangTextSchemaSource(final SourceIdentifier identifier) {
28 this.identifier = Preconditions.checkNotNull(identifier);
35 public final SourceIdentifier getIdentifier() {
43 public Class<? extends YangTextSchemaSource> getType() {
44 return YangTextSchemaSource.class;
48 public final String toString() {
49 return addToStringAttributes(Objects.toStringHelper(this).add("identifier", identifier)).toString();
53 * Add subclass-specific attributes to the output {@link #toString()} output. Since
54 * subclasses are prevented from overriding {@link #toString()} for consistency
55 * reasons, they can add their specific attributes to the resulting string by attaching
56 * attributes to the supplied {@link ToStringHelper}.
58 * @param toStringHelper ToStringHelper onto the attributes can be added
59 * @return ToStringHelper supplied as input argument.
61 protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
64 * Create a new YangTextSchemaSource with a specific source identifier and backed
65 * by ByteSource, which provides the actual InputStreams.
67 * @param identifier SourceIdentifier of the resulting schema source
68 * @param delegate Backing ByteSource instance
69 * @return A new YangTextSchemaSource
71 public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
72 return new DelegatedYangTextSchemaSource(identifier, delegate);
75 private static final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<ByteSource> {
76 private final ByteSource delegate;
78 private DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
80 this.delegate = Preconditions.checkNotNull(delegate);
84 public final ByteSource getDelegate() {
89 public InputStream openStream() throws IOException {
90 return delegate.openStream();
94 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
95 return toStringHelper.add("delegate", delegate);