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