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 com.google.common.base.Preconditions;
11 import javax.annotation.Nonnull;
14 * Thrown to indicate error in YANG model source.
16 public class SourceException extends RuntimeException {
17 private static final long serialVersionUID = 1L;
19 private final StatementSourceReference sourceRef;
22 * Create a new instance with the specified message and source. The message will be appended with
23 * the source reference.
25 * @param message Context message
26 * @param source Statement source
28 public SourceException(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
29 super(createMessage(message, source));
34 * Create a new instance with the specified message and source. The message will be appended with
35 * the source reference.
37 * @param message Context message
38 * @param source Statement source
39 * @param cause Underlying cause of this exception
41 public SourceException(@Nonnull final String message, @Nonnull final StatementSourceReference source,
42 final Throwable cause) {
43 super(createMessage(message, source), cause);
48 * Create a new instance with the specified source and a formatted message. The message will be appended with
49 * the source reference.
51 * @param source Statement source
52 * @param format Format string, according to {@link String#format(String, Object...)}.
53 * @param args Format string arguments, according to {@link String#format(String, Object...)}
55 public SourceException(@Nonnull final StatementSourceReference source, @Nonnull final String format,
56 final Object... args) {
57 this(String.format(format, args), source);
61 * Create a new instance with the specified source and a formatted message. The message will be appended with
62 * the source reference.
64 * @param source Statement source
65 * @param cause Underlying cause of this exception
66 * @param format Format string, according to {@link String#format(String, Object...)}.
67 * @param args Format string arguments, according to {@link String#format(String, Object...)}
69 public SourceException(@Nonnull final StatementSourceReference source, final Throwable cause,
70 @Nonnull final String format, final Object... args) {
71 this(String.format(format, args), source, cause);
75 * Return the reference to the source which caused this exception.
77 * @return Source reference
79 public @Nonnull StatementSourceReference getSourceReference() {
84 * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
85 * this method does nothing.
87 * @param expression Expression to be evaluated
88 * @param source Statement source reference
89 * @param format Format string, according to {@link String#format(String, Object...)}.
90 * @param args Format string arguments, according to {@link String#format(String, Object...)}
91 * @throws SourceException if the expression evaluates to true.
93 public static void throwIf(final boolean expression, @Nonnull final StatementSourceReference source,
94 @Nonnull final String format, final Object... args) {
96 throw new SourceException(source, format, args);
101 * Throw an instance of this exception if an object is null. If the object is non-null, it will
102 * be returned as the result of this method.
104 * @param obj Object reference to be checked
105 * @param source Statement source reference
106 * @param format Format string, according to {@link String#format(String, Object...)}.
107 * @param args Format string arguments, according to {@link String#format(String, Object...)}
108 * @return Object if it is not null
109 * @throws SourceException if object is null
111 @Nonnull public static <T> T throwIfNull(final T obj, @Nonnull final StatementSourceReference source,
112 @Nonnull final String format, final Object... args) {
113 throwIf(obj == null, source, format, args);
117 private static String createMessage(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
118 Preconditions.checkNotNull(message);
119 Preconditions.checkNotNull(source);
121 return message + " [at " + source + ']';