Populate parser/ hierarchy
[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     /**
117      * Create a new instance with the specified source and a formatted message. The message will be appended with
118      * the source reference.
119      *
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...)}
124      */
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);
128     }
129
130     /**
131      * Return the reference to the source which caused this exception.
132      *
133      * @return Source reference
134      */
135     public @NonNull StatementSourceReference getSourceReference() {
136         return sourceRef;
137     }
138
139     /**
140      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
141      * this method does nothing.
142      *
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.
148      */
149     public static void throwIf(final boolean expression, final @NonNull CommonStmtCtx stmt,
150             final @NonNull String format, final Object... args) {
151         if (expression) {
152             throw new SourceException(stmt, format, args);
153         }
154     }
155
156     /**
157      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
158      * this method does nothing.
159      *
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.
165      */
166     public static void throwIf(final boolean expression, final @NonNull StatementSourceReference source,
167             final @NonNull String format, final Object... args) {
168         if (expression) {
169             throw new SourceException(source, format, args);
170         }
171     }
172
173     /**
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.
176      *
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
183      */
184     public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull StatementSourceReference source,
185             final @NonNull String format, final Object... args) {
186         if (obj == null) {
187             throw new SourceException(source, format, args);
188         }
189         return obj;
190     }
191
192     /**
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.
195      *
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
202      */
203     public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull CommonStmtCtx stmt,
204             final @NonNull String format, final Object... args) {
205         if (obj == null) {
206             throw new SourceException(stmt.sourceReference(), format, args);
207         }
208         return obj;
209     }
210
211     /**
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.
214      *
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
221      */
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);
225         return opt.get();
226     }
227
228     /**
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.
231      *
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
238      */
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);
242         return opt.get();
243     }
244
245     private static String createMessage(final @NonNull String message, final @NonNull StatementSourceReference source) {
246         return requireNonNull(message) + " [at " + requireNonNull(source) + ']';
247     }
248 }