2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.spi.source;
10 import static java.util.Objects.requireNonNull;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
19 * Thrown to indicate error in YANG model source.
21 public class SourceException extends RuntimeException {
22 private static final long serialVersionUID = 1L;
24 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Interface-specified member")
25 private final @NonNull StatementSourceReference sourceRef;
28 * Create a new instance with the specified message and source. The message will be appended with
29 * the source reference.
31 * @param message Context message
32 * @param source Statement source
34 public SourceException(final @NonNull String message, final @NonNull StatementSourceReference source) {
35 super(createMessage(message, source));
40 * Create a new instance with the specified message and source. The message will be appended with
41 * the source reference.
43 * @param message Context message
44 * @param source Statement source
45 * @param cause Underlying cause of this exception
47 public SourceException(final @NonNull String message, final @NonNull StatementSourceReference source,
48 final Throwable cause) {
49 super(createMessage(message, source), cause);
54 * Create a new instance with the specified source and a formatted message. The message will be appended with
55 * the source reference.
57 * @param source Statement source
58 * @param format Format string, according to {@link String#format(String, Object...)}.
59 * @param args Format string arguments, according to {@link String#format(String, Object...)}
61 public SourceException(final @NonNull StatementSourceReference source, final @NonNull String format,
62 final Object... args) {
63 this(String.format(format, args), source);
67 * Create a new instance with the specified source and a formatted message. The message will be appended with
68 * the source reference.
70 * @param source Statement source
71 * @param cause Underlying cause of this exception
72 * @param format Format string, according to {@link String#format(String, Object...)}.
73 * @param args Format string arguments, according to {@link String#format(String, Object...)}
75 public SourceException(final @NonNull StatementSourceReference source, final Throwable cause,
76 final @NonNull String format, final Object... args) {
77 this(String.format(format, args), source, cause);
81 * Create a new instance with the specified message and source. The message will be appended with the source
84 * @param message Context message
85 * @param stmt Statement context, not retained
87 public SourceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt) {
88 this(message, stmt.sourceReference());
92 * Create a new instance with the specified message and source. The message will be appended with
93 * the source reference.
95 * @param message Context message
96 * @param stmt Statement context, not retained
97 * @param cause Underlying cause of this exception
99 public SourceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt, final Throwable cause) {
100 this(message, stmt.sourceReference(), cause);
104 * Create a new instance with the specified source and a formatted message. The message will be appended with
105 * the source reference.
107 * @param stmt Statement context, not retained
108 * @param format Format string, according to {@link String#format(String, Object...)}.
109 * @param args Format string arguments, according to {@link String#format(String, Object...)}
111 public SourceException(final @NonNull CommonStmtCtx stmt, final @NonNull String format, final Object... args) {
112 this(stmt.sourceReference(), format, args);
116 * Create a new instance with the specified source and a formatted message. The message will be appended with
117 * the source reference.
119 * @param stmt Statement context, not retained
120 * @param cause Underlying cause of this exception
121 * @param format Format string, according to {@link String#format(String, Object...)}.
122 * @param args Format string arguments, according to {@link String#format(String, Object...)}
124 public SourceException(final @NonNull CommonStmtCtx stmt, final Throwable cause,
125 final @NonNull String format, final Object... args) {
126 this(stmt.sourceReference(), cause, format, args);
130 * Return the reference to the source which caused this exception.
132 * @return Source reference
134 public @NonNull StatementSourceReference getSourceReference() {
139 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
140 * this method does nothing.
142 * @param expression Expression to be evaluated
143 * @param stmt Statement context, not retained
144 * @param format Format string, according to {@link String#format(String, Object...)}.
145 * @param args Format string arguments, according to {@link String#format(String, Object...)}
146 * @throws SourceException if the expression evaluates to true.
148 public static void throwIf(final boolean expression, final @NonNull CommonStmtCtx stmt,
149 final @NonNull String format, final Object... args) {
151 throw new SourceException(stmt, format, args);
156 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
157 * this method does nothing.
159 * @param expression Expression to be evaluated
160 * @param source Statement source reference
161 * @param format Format string, according to {@link String#format(String, Object...)}.
162 * @param args Format string arguments, according to {@link String#format(String, Object...)}
163 * @throws SourceException if the expression evaluates to true.
165 public static void throwIf(final boolean expression, final @NonNull StatementSourceReference source,
166 final @NonNull String format, final Object... args) {
168 throw new SourceException(source, format, args);
173 * Throw an instance of this exception if an object is null. If the object is non-null, it will
174 * be returned as the result of this method.
176 * @param obj Object reference to be checked
177 * @param source Statement source reference
178 * @param format Format string, according to {@link String#format(String, Object...)}.
179 * @param args Format string arguments, according to {@link String#format(String, Object...)}
180 * @return Object if it is not null
181 * @throws SourceException if object is null
183 public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull StatementSourceReference source,
184 final @NonNull String format, final Object... args) {
186 throw new SourceException(source, format, args);
192 * Throw an instance of this exception if an object is null. If the object is non-null, it will
193 * be returned as the result of this method.
195 * @param obj Object reference to be checked
196 * @param stmt Statement context, not retained
197 * @param format Format string, according to {@link String#format(String, Object...)}.
198 * @param args Format string arguments, according to {@link String#format(String, Object...)}
199 * @return Object if it is not null
200 * @throws SourceException if object is null
202 public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull CommonStmtCtx stmt,
203 final @NonNull String format, final Object... args) {
205 throw new SourceException(stmt.sourceReference(), format, args);
211 * Throw an instance of this exception if an optional is not present. If it is present, this method will return
212 * the unwrapped value.
214 * @param opt Optional to be checked
215 * @param source Statement source reference
216 * @param format Format string, according to {@link String#format(String, Object...)}.
217 * @param args Format string arguments, according to {@link String#format(String, Object...)}
218 * @return Object unwrapped from the opt optional
219 * @throws SourceException if the optional is not present
221 public static <T> @NonNull T unwrap(final Optional<T> opt, final @NonNull StatementSourceReference source,
222 final @NonNull String format, final Object... args) {
223 throwIf(opt.isEmpty(), source, format, args);
228 * Throw an instance of this exception if an optional is not present. If it is present, this method will return
229 * the unwrapped value.
231 * @param opt Optional to be checked
232 * @param stmt Statement context, not retained
233 * @param format Format string, according to {@link String#format(String, Object...)}.
234 * @param args Format string arguments, according to {@link String#format(String, Object...)}
235 * @return Object unwrapped from the opt optional
236 * @throws SourceException if the optional is not present
238 public static <T> @NonNull T unwrap(final Optional<T> opt, final @NonNull CommonStmtCtx stmt,
239 final @NonNull String format, final Object... args) {
240 throwIf(opt.isEmpty(), stmt, format, args);
244 private static String createMessage(final @NonNull String message, final @NonNull StatementSourceReference source) {
245 return requireNonNull(message) + " [at " + requireNonNull(source) + ']';