Expose FileYangTextSource
[yangtools.git] / model / yang-model-spi / src / main / java / org / opendaylight / yangtools / yang / model / spi / source / YangTextSource.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.spi.source;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.io.CharSource;
15 import java.io.InputStream;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
19
20 /**
21  * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}.
22  */
23 public abstract class YangTextSource extends CharSource implements YangSourceRepresentation {
24     private final @NonNull SourceIdentifier sourceId;
25
26     protected YangTextSource(final SourceIdentifier sourceId) {
27         this.sourceId = requireNonNull(sourceId);
28     }
29
30     @Override
31     public final Class<YangTextSource> getType() {
32         return YangTextSource.class;
33     }
34
35     @Override
36     public final SourceIdentifier sourceId() {
37         return sourceId;
38     }
39
40     @Override
41     public final String toString() {
42         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
43     }
44
45     /**
46      * Add subclass-specific attributes to the output {@link #toString()} output. Since
47      * subclasses are prevented from overriding {@link #toString()} for consistency
48      * reasons, they can add their specific attributes to the resulting string by attaching
49      * attributes to the supplied {@link ToStringHelper}.
50      *
51      * @param toStringHelper ToStringHelper onto the attributes can be added
52      * @return ToStringHelper supplied as input argument.
53      */
54     protected ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
55         return toStringHelper.add("identifier", sourceId);
56     }
57 }