Use Collections.singletonList() instead of Arrays.asList()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / source / DeclarationInTextSource.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.parser.spi.source;
9
10 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
11
12 /**
13  *
14  * Reference of statement source present in textual source format
15  *
16  * Utility implementation of {@link StatementSourceReference} for textual sources,
17  * this is prefered {@link StatementSourceReference} for implementations
18  * of YANG / YIN statement stream sources.
19  *
20  *
21  *  To create source reference use one of this static factories:
22  *  <ul>
23  *  <li>{@link #atPosition(String, int, int)} - provides most specific reference of statement location,
24  *  this is most prefered since it provides most context to debug YANG model.
25  *  </li>
26  *  <li>{@link #atLine(String, int)}- provides source and line of statement location.
27  *  </li>
28  *  <li>{@link #inSource(String)} - least specific reference, should be used only if any of previous
29  *  references are unable to create / derive from source.
30  *  </li>
31  *  </ul>
32  *
33  */
34 public abstract class DeclarationInTextSource implements StatementSourceReference {
35
36     private final String sourceName;
37
38     DeclarationInTextSource(String sourceName) {
39         this.sourceName = sourceName;
40     }
41
42     public String getSourceName() {
43         return sourceName;
44     }
45
46     @Override
47     public StatementSource getStatementSource() {
48         return StatementSource.DECLARATION;
49     }
50
51     @Override
52     public abstract String toString();
53
54     public static final DeclarationInTextSource inSource(String sourceName) {
55         return new InSource(sourceName);
56     }
57
58     public static final DeclarationInTextSource atLine(String sourceName, int line) {
59         return new AtLine(sourceName, line);
60     }
61
62     public static final DeclarationInTextSource atPosition(String sourceName, int line, int position) {
63         return new AtPosition(sourceName, line,position);
64     }
65
66     private static class InSource extends DeclarationInTextSource {
67
68         InSource(String sourceName) {
69             super(sourceName);
70         }
71
72         @Override
73         public String toString() {
74             return getSourceName();
75         }
76
77     }
78
79     private static class AtLine extends InSource {
80
81         private final int line;
82
83         AtLine(String sourceName, int line) {
84             super(sourceName);
85             this.line = line;
86         }
87
88         @Override
89         public String toString() {
90             return String.format("%s:%d", getSourceName(),line);
91         }
92         
93         public int getLine() {
94             return line;
95         }
96
97     }
98
99     private static class AtPosition extends AtLine {
100
101         private final int character;
102
103         AtPosition(String sourceName, int line, int character) {
104             super(sourceName, line);
105             this.character = character;
106         }
107
108         @Override
109         public String toString() {
110             return String.format("%s:%d:%d", getSourceName(),getLine(),character);
111         }
112
113     }
114
115 }