Switch Import/Include/BelongsTo statements to use Unqualified
[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 static java.util.Objects.requireNonNull;
11
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;
17
18 /**
19  * Thrown to indicate error in YANG model source.
20  */
21 public class SourceException extends RuntimeException {
22     private static final long serialVersionUID = 1L;
23
24     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Interface-specified member")
25     private final @NonNull StatementSourceReference sourceRef;
26
27     /**
28      * Create a new instance with the specified message and source. The message will be appended with
29      * the source reference.
30      *
31      * @param message Context message
32      * @param source Statement source
33      */
34     public SourceException(final @NonNull String message, final @NonNull StatementSourceReference source) {
35         super(createMessage(message, source));
36         sourceRef = source;
37     }
38
39     /**
40      * Create a new instance with the specified message and source. The message will be appended with
41      * the source reference.
42      *
43      * @param message Context message
44      * @param source Statement source
45      * @param cause Underlying cause of this exception
46      */
47     public SourceException(final @NonNull String message, final @NonNull StatementSourceReference source,
48             final Throwable cause) {
49         super(createMessage(message, source), cause);
50         sourceRef = source;
51     }
52
53     /**
54      * Create a new instance with the specified source and a formatted message. The message will be appended with
55      * the source reference.
56      *
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...)}
60      */
61     public SourceException(final @NonNull StatementSourceReference source, final @NonNull String format,
62             final Object... args) {
63         this(String.format(format, args), source);
64     }
65
66     /**
67      * Create a new instance with the specified source and a formatted message. The message will be appended with
68      * the source reference.
69      *
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...)}
74      */
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);
78     }
79
80     /**
81      * Create a new instance with the specified message and source. The message will be appended with the source
82      * reference.
83      *
84      * @param message Context message
85      * @param stmt Statement context, not retained
86      */
87     public SourceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt) {
88         this(message, stmt.sourceReference());
89     }
90
91     /**
92      * Create a new instance with the specified message and source. The message will be appended with
93      * the source reference.
94      *
95      * @param message Context message
96      * @param stmt Statement context, not retained
97      * @param cause Underlying cause of this exception
98      */
99     public SourceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt, final Throwable cause) {
100         this(message, stmt.sourceReference(), cause);
101     }
102
103     /**
104      * Create a new instance with the specified source and a formatted message. The message will be appended with
105      * the source reference.
106      *
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...)}
110      */
111     public SourceException(final @NonNull CommonStmtCtx stmt, final @NonNull String format, final Object... args) {
112         this(stmt.sourceReference(), format, args);
113     }
114
115     /**
116      * Create a new instance with the specified source and a formatted message. The message will be appended with
117      * the source reference.
118      *
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...)}
123      */
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);
127     }
128
129     /**
130      * Return the reference to the source which caused this exception.
131      *
132      * @return Source reference
133      */
134     public @NonNull StatementSourceReference getSourceReference() {
135         return sourceRef;
136     }
137
138     /**
139      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
140      * this method does nothing.
141      *
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.
147      */
148     public static void throwIf(final boolean expression, final @NonNull CommonStmtCtx stmt,
149             final @NonNull String format, final Object... args) {
150         if (expression) {
151             throw new SourceException(stmt, format, args);
152         }
153     }
154
155     /**
156      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
157      * this method does nothing.
158      *
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.
164      */
165     public static void throwIf(final boolean expression, final @NonNull StatementSourceReference source,
166             final @NonNull String format, final Object... args) {
167         if (expression) {
168             throw new SourceException(source, format, args);
169         }
170     }
171
172     /**
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.
175      *
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
182      */
183     public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull StatementSourceReference source,
184             final @NonNull String format, final Object... args) {
185         if (obj == null) {
186             throw new SourceException(source, format, args);
187         }
188         return obj;
189     }
190
191     /**
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.
194      *
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
201      */
202     public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull CommonStmtCtx stmt,
203             final @NonNull String format, final Object... args) {
204         if (obj == null) {
205             throw new SourceException(stmt.sourceReference(), format, args);
206         }
207         return obj;
208     }
209
210     /**
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.
213      *
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
220      */
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);
224         return opt.get();
225     }
226
227     /**
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.
230      *
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
237      */
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);
241         return opt.get();
242     }
243
244     private static String createMessage(final @NonNull String message, final @NonNull StatementSourceReference source) {
245         return requireNonNull(message) + " [at " + requireNonNull(source) + ']';
246     }
247 }