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