Deprecate simple DataTreeFactory.create()
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / meta / StatementDeclaration.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.api.meta;
9
10 import org.eclipse.jdt.annotation.NonNull;
11 import org.eclipse.jdt.annotation.NonNullByDefault;
12 import org.eclipse.jdt.annotation.Nullable;
13
14 /**
15  * A {@link StatementSourceReference} which acts as its own {@link DeclarationReference}, i.e. referring to a statement
16  * source present in textual source format.
17  */
18 @NonNullByDefault
19 public abstract class StatementDeclaration extends StatementSourceReference implements DeclarationReference {
20     /**
21      * A {@link StatementDeclaration} which acts as its own {@link DeclarationInText}.
22      */
23     public abstract static class InText extends StatementDeclaration implements DeclarationInText {
24         @Override
25         protected final int line() {
26             return startLine();
27         }
28
29         @Override
30         protected final int column() {
31             return startColumn();
32         }
33
34         @Override
35         protected @Nullable String file() {
36             return null;
37         }
38     }
39
40     /**
41      * A {@link StatementDeclaration.InText} which acts as its own {@link DeclarationInFile}.
42      */
43     public abstract static class InTextFile extends InText implements DeclarationInFile {
44         @Override
45         protected final @NonNull String file() {
46             return fileName();
47         }
48     }
49
50     @Override
51     public final StatementOrigin statementOrigin() {
52         return StatementOrigin.DECLARATION;
53     }
54
55     @Override
56     public final @NonNull DeclarationReference declarationReference() {
57         return this;
58     }
59
60     @Override
61     public final String toHumanReadable() {
62         final var sb = new StringBuilder();
63         final var file = file();
64         sb.append(file != null ? file : "<UNKNOWN>");
65         final var line = line();
66         if (line > 0) {
67             sb.append(':').append(line);
68         }
69         final var column = column();
70         if (column > 0) {
71             sb.append(':').append(column);
72         }
73         return sb.toString();
74     }
75
76     @Override
77     public final String toString() {
78         return toHumanReadable();
79     }
80
81     protected abstract @Nullable String file();
82
83     protected abstract int line();
84
85     protected abstract int column();
86 }