BUG-997: Introduce SchemaSource respository and related APIs
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YangTextSchemaSource.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.repo.api;
9
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;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17
18 import org.opendaylight.yangtools.concepts.Delegator;
19
20 /**
21  * YANG text schema source representation. Exposes an RFC6020 text representation
22  * as an {@link InputStream}.
23  */
24 public abstract class YangTextSchemaSource extends ByteSource implements SchemaSourceRepresentation {
25     private final SourceIdentifier identifier;
26
27     protected YangTextSchemaSource(final SourceIdentifier identifier) {
28         this.identifier = Preconditions.checkNotNull(identifier);
29     }
30
31     /**
32      * {@inheritDoc}
33      */
34     @Override
35     public final SourceIdentifier getIdentifier() {
36         return identifier;
37     }
38
39     /**
40      * {@inheritDoc}
41      */
42     @Override
43     public Class<? extends YangTextSchemaSource> getType() {
44         return YangTextSchemaSource.class;
45     }
46
47     @Override
48     public final String toString() {
49         return addToStringAttributes(Objects.toStringHelper(this).add("identifier", identifier)).toString();
50     }
51
52     /**
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}.
57      *
58      * @param toStringHelper ToStringHelper onto the attributes can be added
59      * @return ToStringHelper supplied as input argument.
60      */
61     protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
62
63     /**
64      * Create a new YangTextSchemaSource with a specific source identifier and backed
65      * by ByteSource, which provides the actual InputStreams.
66      *
67      * @param identifier SourceIdentifier of the resulting schema source
68      * @param delegate Backing ByteSource instance
69      * @return A new YangTextSchemaSource
70      */
71     public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
72         return new DelegatedYangTextSchemaSource(identifier, delegate);
73     }
74
75     private static final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<ByteSource> {
76         private final ByteSource delegate;
77
78         private DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
79             super(identifier);
80             this.delegate = Preconditions.checkNotNull(delegate);
81         }
82
83         @Override
84         public final ByteSource getDelegate() {
85             return delegate;
86         }
87
88         @Override
89         public InputStream openStream() throws IOException {
90             return delegate.openStream();
91         }
92
93         @Override
94         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
95             return toStringHelper.add("delegate", delegate);
96         }
97     }
98 }