Introduce formatting methods for SourceException
[yangtools.git] / yang / yang-parser-impl / 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 com.google.common.base.Preconditions;
11 import javax.annotation.Nonnull;
12
13 /**
14  * Thrown to indicate error in YANG model source.
15  */
16 public class SourceException extends RuntimeException {
17     private static final long serialVersionUID = 1L;
18
19     private final StatementSourceReference sourceRef;
20
21     /**
22      * Create a new instance with the specified message and source. The message will be appended with
23      * the source reference.
24      *
25      * @param message Context message
26      * @param source Statement source
27      */
28     public SourceException(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
29         super(createMessage(message, source));
30         sourceRef = source;
31     }
32
33     /**
34      * Create a new instance with the specified message and source. The message will be appended with
35      * the source reference.
36      *
37      * @param message Context message
38      * @param source Statement source
39      * @param cause Underlying cause of this exception
40      */
41     public SourceException(@Nonnull final String message, @Nonnull final StatementSourceReference source,
42             final Throwable cause) {
43         super(createMessage(message, source), cause);
44         sourceRef = source;
45     }
46
47     /**
48      * Create a new instance with the specified source and a formatted message. The message will be appended with
49      * the source reference.
50      *
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...)}
54      */
55     public SourceException(@Nonnull final StatementSourceReference source, @Nonnull final String format,
56             final Object... args) {
57         this(String.format(format, args), source);
58     }
59
60     /**
61      * Create a new instance with the specified source and a formatted message. The message will be appended with
62      * the source reference.
63      *
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...)}
68      */
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);
72     }
73
74     /**
75      * Return the reference to the source which caused this exception.
76      *
77      * @return Source reference
78      */
79     public @Nonnull StatementSourceReference getSourceReference() {
80         return sourceRef;
81     }
82
83     /**
84      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
85      * this method does nothing.
86      *
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.
92      */
93     public static void throwIf(final boolean expression, @Nonnull final StatementSourceReference source,
94             @Nonnull final String format, final Object... args) {
95         if (expression) {
96             throw new SourceException(source, format, args);
97         }
98     }
99
100     /**
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.
103      *
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
110      */
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);
114         return obj;
115     }
116
117     private static String createMessage(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
118         Preconditions.checkNotNull(message);
119         Preconditions.checkNotNull(source);
120
121         return message + " [at " + source + ']';
122     }
123 }