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 java.util.Optional;
13 import javax.annotation.Nonnull;
16 * Thrown to indicate error in YANG model source.
18 public class SourceException extends RuntimeException {
19 private static final long serialVersionUID = 1L;
21 private final StatementSourceReference sourceRef;
24 * Create a new instance with the specified message and source. The message will be appended with
25 * the source reference.
27 * @param message Context message
28 * @param source Statement source
30 public SourceException(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
31 super(createMessage(message, source));
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 source Statement source
41 * @param cause Underlying cause of this exception
43 public SourceException(@Nonnull final String message, @Nonnull final StatementSourceReference source,
44 final Throwable cause) {
45 super(createMessage(message, source), cause);
50 * Create a new instance with the specified source and a formatted message. The message will be appended with
51 * the source reference.
53 * @param source Statement source
54 * @param format Format string, according to {@link String#format(String, Object...)}.
55 * @param args Format string arguments, according to {@link String#format(String, Object...)}
57 public SourceException(@Nonnull final StatementSourceReference source, @Nonnull final String format,
58 final Object... args) {
59 this(String.format(format, args), source);
63 * Create a new instance with the specified source and a formatted message. The message will be appended with
64 * the source reference.
66 * @param source Statement source
67 * @param cause Underlying cause of this exception
68 * @param format Format string, according to {@link String#format(String, Object...)}.
69 * @param args Format string arguments, according to {@link String#format(String, Object...)}
71 public SourceException(@Nonnull final StatementSourceReference source, final Throwable cause,
72 @Nonnull final String format, final Object... args) {
73 this(String.format(format, args), source, cause);
77 * Return the reference to the source which caused this exception.
79 * @return Source reference
81 public @Nonnull StatementSourceReference getSourceReference() {
86 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
87 * this method does nothing.
89 * @param expression Expression to be evaluated
90 * @param source Statement source reference
91 * @param format Format string, according to {@link String#format(String, Object...)}.
92 * @param args Format string arguments, according to {@link String#format(String, Object...)}
93 * @throws SourceException if the expression evaluates to true.
95 public static void throwIf(final boolean expression, @Nonnull final StatementSourceReference source,
96 @Nonnull final String format, final Object... args) {
98 throw new SourceException(source, format, args);
103 * Throw an instance of this exception if an object is null. If the object is non-null, it will
104 * be returned as the result of this method.
106 * @param obj Object reference to be checked
107 * @param source Statement source reference
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 * @return Object if it is not null
111 * @throws SourceException if object is null
113 @Nonnull public static <T> T throwIfNull(final T obj, @Nonnull final StatementSourceReference source,
114 @Nonnull final String format, final Object... args) {
115 throwIf(obj == null, source, format, args);
120 * Throw an instance of this exception if an optional is not present. If it is present, this method will return
121 * the unwrapped value.
123 * @param opt Optional to be checked
124 * @param source Statement source reference
125 * @param format Format string, according to {@link String#format(String, Object...)}.
126 * @param args Format string arguments, according to {@link String#format(String, Object...)}
127 * @return Object unwrapped from the opt optional
128 * @throws SourceException if the optional is not present
130 @Nonnull public static <T> T unwrap(final Optional<T> opt, @Nonnull final StatementSourceReference source,
131 @Nonnull final String format, final Object... args) {
132 throwIf(!opt.isPresent(), source, format, args);
136 private static String createMessage(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
137 requireNonNull(message);
138 requireNonNull(source);
140 return message + " [at " + source + ']';