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;
18 * Thrown to indicate error in YANG model source.
20 public class SourceException extends RuntimeException {
21 private static final long serialVersionUID = 1L;
23 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Interface-specified member")
24 private final @NonNull StatementSourceReference sourceRef;
27 * Create a new instance with the specified message and source. The message will be appended with
28 * the source reference.
30 * @param message Context message
31 * @param source Statement source
33 public SourceException(final @NonNull String message, final @NonNull StatementSourceReference source) {
34 super(createMessage(message, source));
39 * Create a new instance with the specified message and source. The message will be appended with
40 * the source reference.
42 * @param message Context message
43 * @param source Statement source
44 * @param cause Underlying cause of this exception
46 public SourceException(final @NonNull String message, final @NonNull StatementSourceReference source,
47 final Throwable cause) {
48 super(createMessage(message, source), cause);
53 * Create a new instance with the specified source and a formatted message. The message will be appended with
54 * the source reference.
56 * @param source Statement source
57 * @param format Format string, according to {@link String#format(String, Object...)}.
58 * @param args Format string arguments, according to {@link String#format(String, Object...)}
60 public SourceException(final @NonNull StatementSourceReference source, final @NonNull String format,
61 final Object... args) {
62 this(String.format(format, args), source);
66 * Create a new instance with the specified source and a formatted message. The message will be appended with
67 * the source reference.
69 * @param source Statement source
70 * @param cause Underlying cause of this exception
71 * @param format Format string, according to {@link String#format(String, Object...)}.
72 * @param args Format string arguments, according to {@link String#format(String, Object...)}
74 public SourceException(final @NonNull StatementSourceReference source, final Throwable cause,
75 final @NonNull String format, final Object... args) {
76 this(String.format(format, args), source, cause);
80 * Return the reference to the source which caused this exception.
82 * @return Source reference
84 public @NonNull StatementSourceReference getSourceReference() {
89 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
90 * this method does nothing.
92 * @param expression Expression to be evaluated
93 * @param source Statement source reference
94 * @param format Format string, according to {@link String#format(String, Object...)}.
95 * @param args Format string arguments, according to {@link String#format(String, Object...)}
96 * @throws SourceException if the expression evaluates to true.
98 public static void throwIf(final boolean expression, final @NonNull StatementSourceReference source,
99 final @NonNull String format, final Object... args) {
101 throw new SourceException(source, format, args);
106 * Throw an instance of this exception if an object is null. If the object is non-null, it will
107 * be returned as the result of this method.
109 * @param obj Object reference to be checked
110 * @param source Statement source reference
111 * @param format Format string, according to {@link String#format(String, Object...)}.
112 * @param args Format string arguments, according to {@link String#format(String, Object...)}
113 * @return Object if it is not null
114 * @throws SourceException if object is null
116 public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull StatementSourceReference source,
117 final @NonNull String format, final Object... args) {
119 throw new SourceException(source, format, args);
125 * Throw an instance of this exception if an optional is not present. If it is present, this method will return
126 * the unwrapped value.
128 * @param opt Optional to be checked
129 * @param source Statement source reference
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 * @return Object unwrapped from the opt optional
133 * @throws SourceException if the optional is not present
135 public static <T> @NonNull T unwrap(final Optional<T> opt, final @NonNull StatementSourceReference source,
136 final @NonNull String format, final Object... args) {
137 throwIf(opt.isEmpty(), source, format, args);
141 private static String createMessage(final @NonNull String message, final @NonNull StatementSourceReference source) {
142 return requireNonNull(message) + " [at " + requireNonNull(source) + ']';