Update Objects->MoreObjects
[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 static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.io.ByteSource;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import org.opendaylight.yangtools.concepts.Delegator;
23
24 /**
25  * YANG text schema source representation. Exposes an RFC6020 text representation
26  * as an {@link InputStream}.
27  */
28 @Beta
29 public abstract class YangTextSchemaSource extends ByteSource implements SchemaSourceRepresentation {
30     private final SourceIdentifier identifier;
31
32     protected YangTextSchemaSource(final SourceIdentifier identifier) {
33         this.identifier = Preconditions.checkNotNull(identifier);
34     }
35
36     public static SourceIdentifier identifierFromFilename(final String name) {
37         checkArgument(name.endsWith(".yang"), "Filename %s does not have a .yang extension", name);
38         // FIXME: add revision-awareness
39         return SourceIdentifier.create(name.substring(0, name.length() - 5), Optional.<String>absent());
40     }
41
42     /**
43      * {@inheritDoc}
44      */
45     @Override
46     public final SourceIdentifier getIdentifier() {
47         return identifier;
48     }
49
50     /**
51      * {@inheritDoc}
52      */
53     @Override
54     public Class<? extends YangTextSchemaSource> getType() {
55         return YangTextSchemaSource.class;
56     }
57
58     @Override
59     public final String toString() {
60         return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
61     }
62
63     /**
64      * Add subclass-specific attributes to the output {@link #toString()} output. Since
65      * subclasses are prevented from overriding {@link #toString()} for consistency
66      * reasons, they can add their specific attributes to the resulting string by attaching
67      * attributes to the supplied {@link ToStringHelper}.
68      *
69      * @param toStringHelper ToStringHelper onto the attributes can be added
70      * @return ToStringHelper supplied as input argument.
71      */
72     protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
73
74     /**
75      * Create a new YangTextSchemaSource with a specific source identifier and backed
76      * by ByteSource, which provides the actual InputStreams.
77      *
78      * @param identifier SourceIdentifier of the resulting schema source
79      * @param delegate Backing ByteSource instance
80      * @return A new YangTextSchemaSource
81      */
82     public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
83         return new DelegatedYangTextSchemaSource(identifier, delegate);
84     }
85
86     private static final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<ByteSource> {
87         private final ByteSource delegate;
88
89         private DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
90             super(identifier);
91             this.delegate = Preconditions.checkNotNull(delegate);
92         }
93
94         @Override
95         public final ByteSource getDelegate() {
96             return delegate;
97         }
98
99         @Override
100         public InputStream openStream() throws IOException {
101             return delegate.openStream();
102         }
103
104         @Override
105         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
106             return toStringHelper.add("delegate", delegate);
107         }
108     }
109 }