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);
117 * Create a new instance with the specified source and a formatted message. The message will be appended with
118 * the source reference.
120 * @param stmt Statement context, not retained
121 * @param cause Underlying cause of this exception
122 * @param format Format string, according to {@link String#format(String, Object...)}.
123 * @param args Format string arguments, according to {@link String#format(String, Object...)}
125 public SourceException(final @NonNull CommonStmtCtx stmt, final Throwable cause,
126 final @NonNull String format, final Object... args) {
127 this(stmt.sourceReference(), cause, format, args);
131 * Return the reference to the source which caused this exception.
133 * @return Source reference
135 public @NonNull StatementSourceReference getSourceReference() {
140 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
141 * this method does nothing.
143 * @param expression Expression to be evaluated
144 * @param stmt Statement context, not retained
145 * @param format Format string, according to {@link String#format(String, Object...)}.
146 * @param args Format string arguments, according to {@link String#format(String, Object...)}
147 * @throws SourceException if the expression evaluates to true.
149 public static void throwIf(final boolean expression, final @NonNull CommonStmtCtx stmt,
150 final @NonNull String format, final Object... args) {
152 throw new SourceException(stmt, format, args);
157 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
158 * this method does nothing.
160 * @param expression Expression to be evaluated
161 * @param source Statement source reference
162 * @param format Format string, according to {@link String#format(String, Object...)}.
163 * @param args Format string arguments, according to {@link String#format(String, Object...)}
164 * @throws SourceException if the expression evaluates to true.
166 public static void throwIf(final boolean expression, final @NonNull StatementSourceReference source,
167 final @NonNull String format, final Object... args) {
169 throw new SourceException(source, format, args);
174 * Throw an instance of this exception if an object is null. If the object is non-null, it will
175 * be returned as the result of this method.
177 * @param obj Object reference to be checked
178 * @param source Statement source reference
179 * @param format Format string, according to {@link String#format(String, Object...)}.
180 * @param args Format string arguments, according to {@link String#format(String, Object...)}
181 * @return Object if it is not null
182 * @throws SourceException if object is null
184 public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull StatementSourceReference source,
185 final @NonNull String format, final Object... args) {
187 throw new SourceException(source, format, args);
193 * Throw an instance of this exception if an object is null. If the object is non-null, it will
194 * be returned as the result of this method.
196 * @param obj Object reference to be checked
197 * @param stmt Statement context, not retained
198 * @param format Format string, according to {@link String#format(String, Object...)}.
199 * @param args Format string arguments, according to {@link String#format(String, Object...)}
200 * @return Object if it is not null
201 * @throws SourceException if object is null
203 public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull CommonStmtCtx stmt,
204 final @NonNull String format, final Object... args) {
206 throw new SourceException(stmt.sourceReference(), format, args);
212 * Throw an instance of this exception if an optional is not present. If it is present, this method will return
213 * the unwrapped value.
215 * @param opt Optional to be checked
216 * @param source Statement source reference
217 * @param format Format string, according to {@link String#format(String, Object...)}.
218 * @param args Format string arguments, according to {@link String#format(String, Object...)}
219 * @return Object unwrapped from the opt optional
220 * @throws SourceException if the optional is not present
222 public static <T> @NonNull T unwrap(final Optional<T> opt, final @NonNull StatementSourceReference source,
223 final @NonNull String format, final Object... args) {
224 throwIf(opt.isEmpty(), source, format, args);
229 * Throw an instance of this exception if an optional is not present. If it is present, this method will return
230 * the unwrapped value.
232 * @param opt Optional to be checked
233 * @param stmt Statement context, not retained
234 * @param format Format string, according to {@link String#format(String, Object...)}.
235 * @param args Format string arguments, according to {@link String#format(String, Object...)}
236 * @return Object unwrapped from the opt optional
237 * @throws SourceException if the optional is not present
239 public static <T> @NonNull T unwrap(final Optional<T> opt, final @NonNull CommonStmtCtx stmt,
240 final @NonNull String format, final Object... args) {
241 throwIf(opt.isEmpty(), stmt, format, args);
245 private static String createMessage(final @NonNull String message, final @NonNull StatementSourceReference source) {
246 return requireNonNull(message) + " [at " + requireNonNull(source) + ']';