Move message formatting to StatementSourceException
[yangtools.git] / parser / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / source / SourceException.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.Optional;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceException;
14 import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceReference;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
16
17 /**
18  * Thrown to indicate error in YANG model source.
19  */
20 public class SourceException extends StatementSourceException {
21     @java.io.Serial
22     private static final long serialVersionUID = 1L;
23
24     /**
25      * Create a new instance with the specified message and source. The message will be appended with
26      * the source reference.
27      *
28      * @param message Context message
29      * @param sourceRef Statement source
30      */
31     public SourceException(final @NonNull String message, final @NonNull StatementSourceReference sourceRef) {
32         super(sourceRef, message);
33     }
34
35     /**
36      * Create a new instance with the specified message and source. The message will be appended with
37      * the source reference.
38      *
39      * @param message Context message
40      * @param sourceRef Statement source
41      * @param cause Underlying cause of this exception
42      */
43     public SourceException(final @NonNull String message, final @NonNull StatementSourceReference sourceRef,
44             final Throwable cause) {
45         super(sourceRef, message, cause);
46     }
47
48     /**
49      * Create a new instance with the specified source and a formatted message. The message will be appended with
50      * the source reference.
51      *
52      * @param source Statement source
53      * @param format Format string, according to {@link String#format(String, Object...)}.
54      * @param args Format string arguments, according to {@link String#format(String, Object...)}
55      */
56     public SourceException(final @NonNull StatementSourceReference source, final @NonNull String format,
57             final Object... args) {
58         this(String.format(format, args), source);
59     }
60
61     /**
62      * Create a new instance with the specified source and a formatted message. The message will be appended with
63      * the source reference.
64      *
65      * @param source Statement source
66      * @param cause Underlying cause of this exception
67      * @param format Format string, according to {@link String#format(String, Object...)}.
68      * @param args Format string arguments, according to {@link String#format(String, Object...)}
69      */
70     public SourceException(final @NonNull StatementSourceReference source, final Throwable cause,
71             final @NonNull String format, final Object... args) {
72         this(String.format(format, args), source, cause);
73     }
74
75     /**
76      * Create a new instance with the specified message and source. The message will be appended with the source
77      * reference.
78      *
79      * @param message Context message
80      * @param stmt Statement context, not retained
81      */
82     public SourceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt) {
83         this(message, stmt.sourceReference());
84     }
85
86     /**
87      * Create a new instance with the specified message and source. The message will be appended with
88      * the source reference.
89      *
90      * @param message Context message
91      * @param stmt Statement context, not retained
92      * @param cause Underlying cause of this exception
93      */
94     public SourceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt, final Throwable cause) {
95         this(message, stmt.sourceReference(), cause);
96     }
97
98     /**
99      * Create a new instance with the specified source and a formatted message. The message will be appended with
100      * the source reference.
101      *
102      * @param stmt Statement context, not retained
103      * @param format Format string, according to {@link String#format(String, Object...)}.
104      * @param args Format string arguments, according to {@link String#format(String, Object...)}
105      */
106     public SourceException(final @NonNull CommonStmtCtx stmt, final @NonNull String format, final Object... args) {
107         this(stmt.sourceReference(), format, args);
108     }
109
110     /**
111      * Create a new instance with the specified source and a formatted message. The message will be appended with
112      * the source reference.
113      *
114      * @param stmt Statement context, not retained
115      * @param cause Underlying cause of this exception
116      * @param format Format string, according to {@link String#format(String, Object...)}.
117      * @param args Format string arguments, according to {@link String#format(String, Object...)}
118      */
119     public SourceException(final @NonNull CommonStmtCtx stmt, final Throwable cause,
120             final @NonNull String format, final Object... args) {
121         this(stmt.sourceReference(), cause, format, args);
122     }
123
124     /**
125      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
126      * this method does nothing.
127      *
128      * @param expression Expression to be evaluated
129      * @param stmt Statement context, not retained
130      * @param format Format string, according to {@link String#format(String, Object...)}.
131      * @param args Format string arguments, according to {@link String#format(String, Object...)}
132      * @throws SourceException if the expression evaluates to true.
133      */
134     public static void throwIf(final boolean expression, final @NonNull CommonStmtCtx stmt,
135             final @NonNull String format, final Object... args) {
136         if (expression) {
137             throw new SourceException(stmt, format, args);
138         }
139     }
140
141     /**
142      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
143      * this method does nothing.
144      *
145      * @param expression Expression to be evaluated
146      * @param source Statement source reference
147      * @param format Format string, according to {@link String#format(String, Object...)}.
148      * @param args Format string arguments, according to {@link String#format(String, Object...)}
149      * @throws SourceException if the expression evaluates to true.
150      */
151     public static void throwIf(final boolean expression, final @NonNull StatementSourceReference source,
152             final @NonNull String format, final Object... args) {
153         if (expression) {
154             throw new SourceException(source, format, args);
155         }
156     }
157
158     /**
159      * Throw an instance of this exception if an object is null. If the object is non-null, it will
160      * be returned as the result of this method.
161      *
162      * @param obj Object reference to be checked
163      * @param source Statement source reference
164      * @param format Format string, according to {@link String#format(String, Object...)}.
165      * @param args Format string arguments, according to {@link String#format(String, Object...)}
166      * @return Object if it is not null
167      * @throws SourceException if object is null
168      */
169     public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull StatementSourceReference source,
170             final @NonNull String format, final Object... args) {
171         if (obj == null) {
172             throw new SourceException(source, format, args);
173         }
174         return obj;
175     }
176
177     /**
178      * Throw an instance of this exception if an object is null. If the object is non-null, it will
179      * be returned as the result of this method.
180      *
181      * @param obj Object reference to be checked
182      * @param stmt Statement context, not retained
183      * @param format Format string, according to {@link String#format(String, Object...)}.
184      * @param args Format string arguments, according to {@link String#format(String, Object...)}
185      * @return Object if it is not null
186      * @throws SourceException if object is null
187      */
188     public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull CommonStmtCtx stmt,
189             final @NonNull String format, final Object... args) {
190         if (obj == null) {
191             throw new SourceException(stmt.sourceReference(), format, args);
192         }
193         return obj;
194     }
195
196     /**
197      * Throw an instance of this exception if an optional is not present. If it is present, this method will return
198      * the unwrapped value.
199      *
200      * @param opt Optional to be checked
201      * @param source Statement source reference
202      * @param format Format string, according to {@link String#format(String, Object...)}.
203      * @param args Format string arguments, according to {@link String#format(String, Object...)}
204      * @return Object unwrapped from the opt optional
205      * @throws SourceException if the optional is not present
206      */
207     public static <T> @NonNull T unwrap(final Optional<T> opt, final @NonNull StatementSourceReference source,
208             final @NonNull String format, final Object... args) {
209         throwIf(opt.isEmpty(), source, format, args);
210         return opt.orElseThrow();
211     }
212
213     /**
214      * Throw an instance of this exception if an optional is not present. If it is present, this method will return
215      * the unwrapped value.
216      *
217      * @param opt Optional to be checked
218      * @param stmt Statement context, not retained
219      * @param format Format string, according to {@link String#format(String, Object...)}.
220      * @param args Format string arguments, according to {@link String#format(String, Object...)}
221      * @return Object unwrapped from the opt optional
222      * @throws SourceException if the optional is not present
223      */
224     public static <T> @NonNull T unwrap(final Optional<T> opt, final @NonNull CommonStmtCtx stmt,
225             final @NonNull String format, final Object... args) {
226         throwIf(opt.isEmpty(), stmt, format, args);
227         return opt.orElseThrow();
228     }
229 }