Convert parser-{reactor,spi} classes to JDT annotations
[yangtools.git] / yang / yang-parser-spi / 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 static java.util.Objects.requireNonNull;
11
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.util.Optional;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16
17 /**
18  * Thrown to indicate error in YANG model source.
19  */
20 public class SourceException extends RuntimeException {
21     private static final long serialVersionUID = 1L;
22
23     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Interface-specified member")
24     private final StatementSourceReference sourceRef;
25
26     /**
27      * Create a new instance with the specified message and source. The message will be appended with
28      * the source reference.
29      *
30      * @param message Context message
31      * @param source Statement source
32      */
33     public SourceException(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
34         super(createMessage(message, source));
35         sourceRef = source;
36     }
37
38     /**
39      * Create a new instance with the specified message and source. The message will be appended with
40      * the source reference.
41      *
42      * @param message Context message
43      * @param source Statement source
44      * @param cause Underlying cause of this exception
45      */
46     public SourceException(@Nonnull final String message, @Nonnull final StatementSourceReference source,
47             final Throwable cause) {
48         super(createMessage(message, source), cause);
49         sourceRef = source;
50     }
51
52     /**
53      * Create a new instance with the specified source and a formatted message. The message will be appended with
54      * the source reference.
55      *
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...)}
59      */
60     public SourceException(@Nonnull final StatementSourceReference source, @Nonnull final String format,
61             final Object... args) {
62         this(String.format(format, args), source);
63     }
64
65     /**
66      * Create a new instance with the specified source and a formatted message. The message will be appended with
67      * the source reference.
68      *
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...)}
73      */
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);
77     }
78
79     /**
80      * Return the reference to the source which caused this exception.
81      *
82      * @return Source reference
83      */
84     public @Nonnull StatementSourceReference getSourceReference() {
85         return sourceRef;
86     }
87
88     /**
89      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
90      * this method does nothing.
91      *
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.
97      */
98     public static void throwIf(final boolean expression, @Nonnull final StatementSourceReference source,
99             @Nonnull final String format, final Object... args) {
100         if (expression) {
101             throw new SourceException(source, format, args);
102         }
103     }
104
105     /**
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.
108      *
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
115      */
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);
119         return obj;
120     }
121
122     /**
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.
125      *
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
132      */
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);
136         return opt.get();
137     }
138
139     private static String createMessage(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
140         requireNonNull(message);
141         requireNonNull(source);
142
143         return message + " [at " + source + ']';
144     }
145 }