2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3 * Copyright (c) 2021 PANTHEON.tech, s.r.o.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.parser.spi.source;
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static com.google.common.base.Verify.verifyNotNull;
14 import com.google.common.annotations.Beta;
15 import java.util.Objects;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationInFile;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationInText;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementDeclaration;
21 import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceReference;
24 * Reference of statement source present in textual source format. Utility implementation
25 * of {@link StatementSourceReference} for textual sources, this is preferred {@link StatementSourceReference}
26 * for implementations of YANG / YIN statement stream sources.
29 * To create source reference use one of this static factories:
31 * <li>{@link #atPosition(String, int, int)} - provides most specific reference of statement location, this is most
32 * preferred since it provides most context to debug YANG model.</li>
33 * <li>{@link #atPosition(int, int)}- provides location in text without knowing the name of the text file.</li>
34 * <li>{@link #inFile(String)}- provides a source name.</li>
38 public abstract class ExplicitStatement extends StatementDeclaration {
39 private static final class InFile extends ExplicitStatement implements DeclarationInFile {
40 InFile(final String fileName) {
41 super(fileName, -1, -1);
42 checkArgument(!fileName.isEmpty(), "Invalid empty file name");
46 public String fileName() {
47 return verifyNotNull(file());
51 private static class InText extends ExplicitStatement implements DeclarationInText {
52 InText(final String file, final int line, final int column) {
53 super(file, line, column);
54 checkArgument(line > 0, "Invalid start line %s", line);
55 checkArgument(column > 0, "Invalid start column %s", column);
59 public int startLine() {
64 public int startColumn() {
69 private static final class InTextFile extends InText implements DeclarationInFile {
70 InTextFile(final String fileName, final int line, final int column) {
71 super(fileName, line, column);
72 checkArgument(!fileName.isEmpty(), "Invalid empty file name");
76 public String fileName() {
77 return verifyNotNull(file());
81 private final String file;
82 private final int line;
83 private final int column;
85 ExplicitStatement(final String file, final int line, final int column) {
91 public static @NonNull ExplicitStatement atPosition(final int line, final int column) {
92 return new InText(null, line, column);
95 public static @NonNull ExplicitStatement atPosition(final @Nullable String fileName, final int line,
97 return fileName == null ? atPosition(line, column) : new InTextFile(fileName, line, column);
100 public static @NonNull ExplicitStatement inFile(final @NonNull String fileName) {
101 return new InFile(fileName);
105 public final int hashCode() {
106 return Objects.hash(file(), line(), column());
110 public final boolean equals(final Object obj) {
114 if (obj == null || !getClass().equals(obj.getClass())) {
117 final ExplicitStatement other = (ExplicitStatement) obj;
118 return line == other.line && column == other.column && Objects.equals(file, other.file);
122 protected final String file() {
127 protected final int line() {
132 protected final int column() {