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 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;
18 * Thrown to indicate error in YANG model source.
20 public class SourceException extends StatementSourceException {
22 private static final long serialVersionUID = 1L;
25 * Create a new instance with the specified message and source. The message will be appended with
26 * the source reference.
28 * @param message Context message
29 * @param sourceRef Statement source
31 public SourceException(final @NonNull String message, final @NonNull StatementSourceReference sourceRef) {
32 super(sourceRef, message);
36 * Create a new instance with the specified message and source. The message will be appended with
37 * the source reference.
39 * @param message Context message
40 * @param sourceRef Statement source
41 * @param cause Underlying cause of this exception
43 public SourceException(final @NonNull String message, final @NonNull StatementSourceReference sourceRef,
44 final Throwable cause) {
45 super(sourceRef, message, cause);
49 * Create a new instance with the specified source and a formatted message. The message will be appended with
50 * the source reference.
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...)}
56 public SourceException(final @NonNull StatementSourceReference source, final @NonNull String format,
57 final Object... args) {
58 this(String.format(format, args), source);
62 * Create a new instance with the specified source and a formatted message. The message will be appended with
63 * the source reference.
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...)}
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);
76 * Create a new instance with the specified message and source. The message will be appended with the source
79 * @param message Context message
80 * @param stmt Statement context, not retained
82 public SourceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt) {
83 this(message, stmt.sourceReference());
87 * Create a new instance with the specified message and source. The message will be appended with
88 * the source reference.
90 * @param message Context message
91 * @param stmt Statement context, not retained
92 * @param cause Underlying cause of this exception
94 public SourceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt, final Throwable cause) {
95 this(message, stmt.sourceReference(), cause);
99 * Create a new instance with the specified source and a formatted message. The message will be appended with
100 * the source reference.
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...)}
106 public SourceException(final @NonNull CommonStmtCtx stmt, final @NonNull String format, final Object... args) {
107 this(stmt.sourceReference(), format, args);
111 * Create a new instance with the specified source and a formatted message. The message will be appended with
112 * the source reference.
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...)}
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);
125 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
126 * this method does nothing.
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.
134 public static void throwIf(final boolean expression, final @NonNull CommonStmtCtx stmt,
135 final @NonNull String format, final Object... args) {
137 throw new SourceException(stmt, format, args);
142 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
143 * this method does nothing.
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.
151 public static void throwIf(final boolean expression, final @NonNull StatementSourceReference source,
152 final @NonNull String format, final Object... args) {
154 throw new SourceException(source, format, args);
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.
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
169 public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull StatementSourceReference source,
170 final @NonNull String format, final Object... args) {
172 throw new SourceException(source, format, args);
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.
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
188 public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull CommonStmtCtx stmt,
189 final @NonNull String format, final Object... args) {
191 throw new SourceException(stmt.sourceReference(), format, args);
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.
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
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();
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.
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
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();