Cleanup use of Guava library
[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 java.util.Optional;
13 import javax.annotation.Nonnull;
14
15 /**
16  * Thrown to indicate error in YANG model source.
17  */
18 public class SourceException extends RuntimeException {
19     private static final long serialVersionUID = 1L;
20
21     private final StatementSourceReference sourceRef;
22
23     /**
24      * Create a new instance with the specified message and source. The message will be appended with
25      * the source reference.
26      *
27      * @param message Context message
28      * @param source Statement source
29      */
30     public SourceException(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
31         super(createMessage(message, source));
32         sourceRef = source;
33     }
34
35     /**
36      * Create a new instance with the specified message and source. The message will be appended with
37      * the source reference.
38      *
39      * @param message Context message
40      * @param source Statement source
41      * @param cause Underlying cause of this exception
42      */
43     public SourceException(@Nonnull final String message, @Nonnull final StatementSourceReference source,
44             final Throwable cause) {
45         super(createMessage(message, source), cause);
46         sourceRef = source;
47     }
48
49     /**
50      * Create a new instance with the specified source and a formatted message. The message will be appended with
51      * the source reference.
52      *
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...)}
56      */
57     public SourceException(@Nonnull final StatementSourceReference source, @Nonnull final String format,
58             final Object... args) {
59         this(String.format(format, args), source);
60     }
61
62     /**
63      * Create a new instance with the specified source and a formatted message. The message will be appended with
64      * the source reference.
65      *
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...)}
70      */
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);
74     }
75
76     /**
77      * Return the reference to the source which caused this exception.
78      *
79      * @return Source reference
80      */
81     public @Nonnull StatementSourceReference getSourceReference() {
82         return sourceRef;
83     }
84
85     /**
86      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
87      * this method does nothing.
88      *
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.
94      */
95     public static void throwIf(final boolean expression, @Nonnull final StatementSourceReference source,
96             @Nonnull final String format, final Object... args) {
97         if (expression) {
98             throw new SourceException(source, format, args);
99         }
100     }
101
102     /**
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.
105      *
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
112      */
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);
116         return obj;
117     }
118
119     /**
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.
122      *
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
129      */
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);
133         return opt.get();
134     }
135
136     private static String createMessage(@Nonnull final String message, @Nonnull final StatementSourceReference source) {
137         requireNonNull(message);
138         requireNonNull(source);
139
140         return message + " [at " + source + ']';
141     }
142 }