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 javax.annotation.Nonnull;
15 import javax.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 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(@Nonnull final String message, @Nonnull final 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(@Nonnull final String message, @Nonnull final 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(@Nonnull final StatementSourceReference source, @Nonnull final 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(@Nonnull final StatementSourceReference source, final Throwable cause,
75 @Nonnull final 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, @Nonnull final StatementSourceReference source,
99 @Nonnull final 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 @Nonnull public static <T> T throwIfNull(@Nullable final T obj, @Nonnull final StatementSourceReference source,
117 @Nonnull final String format, final Object... args) {
118 throwIf(obj == null, source, format, args);
123 * Throw an instance of this exception if an optional is not present. If it is present, this method will return
124 * the unwrapped value.
126 * @param opt Optional to be checked
127 * @param source Statement source reference
128 * @param format Format string, according to {@link String#format(String, Object...)}.
129 * @param args Format string arguments, according to {@link String#format(String, Object...)}
130 * @return Object unwrapped from the opt optional
131 * @throws SourceException if the optional is not present
133 @Nonnull public static <T> T unwrap(final Optional<T> opt, @Nonnull final StatementSourceReference source,
134 @Nonnull final String format, final Object... args) {
135 throwIf(!opt.isPresent(), source, format, args);
139 private static String createMessage(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
140 requireNonNull(message);
141 requireNonNull(source);
143 return message + " [at " + source + ']';