a8e7f0198d6249a2a1f31d05b87e02d0f910328d
[yangtools.git] / yang / yang-parser-spi / 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 java.util.Objects;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
13
14 /**
15  * Reference of statement source present in textual source format. Utility implementation
16  * of {@link StatementSourceReference} for textual sources, this is preferred {@link StatementSourceReference}
17  * for implementations of YANG / YIN statement stream sources.
18  *
19  * <p>
20  * To create source reference use one of this static factories:
21  * <ul>
22  *   <li>{@link #atPosition(String, int, int)} - provides most specific reference of statement location, this is most
23  *       preferred since it provides most context to debug YANG model.</li>
24  *   <li>{@link #atLine(String, int)}- provides source and line of statement location.</li>
25  *   <li>{@link #inSource(String)} - least specific reference, should be used only if any of previous references are
26  *       unable to create / derive from source.</li>
27  * </ul>
28  */
29 public abstract class DeclarationInTextSource implements StatementSourceReference {
30     private static class InSource extends DeclarationInTextSource {
31         InSource(final String sourceName) {
32             super(sourceName);
33         }
34     }
35
36     private static class AtLine extends InSource {
37         private final int line;
38
39         AtLine(final String sourceName, final int line) {
40             super(sourceName);
41             this.line = line;
42         }
43
44         @Override
45         int hashCodeImpl() {
46             return super.hashCodeImpl() * 31 + line;
47         }
48
49         @Override
50         boolean equalsImpl(final DeclarationInTextSource obj) {
51             return line == ((AtLine) obj).line && super.equalsImpl(obj);
52         }
53
54         @Override
55         public String toString() {
56             return super.toString() + ':' + line;
57         }
58     }
59
60     private static final class AtPosition extends AtLine {
61         private final int character;
62
63         AtPosition(final String sourceName, final int line, final int character) {
64             super(sourceName, line);
65             this.character = character;
66         }
67
68         @Override
69         int hashCodeImpl() {
70             return super.hashCodeImpl() * 31 + character;
71         }
72
73         @Override
74         boolean equalsImpl(final DeclarationInTextSource obj) {
75             return character == ((AtPosition) obj).character && super.equalsImpl(obj);
76         }
77
78         @Override
79         public String toString() {
80             return super.toString() + ':' + character;
81         }
82     }
83
84     private final String sourceName;
85
86     DeclarationInTextSource(final String sourceName) {
87         this.sourceName = sourceName;
88     }
89
90     public static @NonNull DeclarationInTextSource inSource(final String sourceName) {
91         return new InSource(sourceName);
92     }
93
94     public static @NonNull DeclarationInTextSource atLine(final String sourceName, final int line) {
95         return new AtLine(sourceName, line);
96     }
97
98     public static @NonNull DeclarationInTextSource atPosition(final String sourceName, final int line,
99             final int position) {
100         return new AtPosition(sourceName, line, position);
101     }
102
103     public final String getSourceName() {
104         return sourceName;
105     }
106
107     @Override
108     public final StatementSource getStatementSource() {
109         return StatementSource.DECLARATION;
110     }
111
112     @Override
113     public final int hashCode() {
114         return hashCodeImpl();
115     }
116
117     @Override
118     public final boolean equals(final Object obj) {
119         return this == obj
120                 || obj != null && getClass().equals(obj.getClass()) && equalsImpl((DeclarationInTextSource) obj);
121     }
122
123     @Override
124     public String toString() {
125         return sourceName == null ? "null" : sourceName;
126     }
127
128     int hashCodeImpl() {
129         return Objects.hashCode(sourceName);
130     }
131
132     boolean equalsImpl(final DeclarationInTextSource obj) {
133         return Objects.equals(sourceName, obj.sourceName);
134     }
135 }