Merge branch 'master' of ../controller
[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 org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.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 @NonNull 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(final @NonNull String message, final @NonNull 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(final @NonNull String message, final @NonNull 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(final @NonNull StatementSourceReference source, final @NonNull 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(final @NonNull StatementSourceReference source, final Throwable cause,
75             final @NonNull 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, final @NonNull StatementSourceReference source,
99             final @NonNull 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     public static <T> @NonNull T throwIfNull(final @Nullable T obj, final @NonNull StatementSourceReference source,
117             final @NonNull String format, final Object... args) {
118         if (obj == null) {
119             throw new SourceException(source, format, args);
120         }
121         return obj;
122     }
123
124     /**
125      * Throw an instance of this exception if an optional is not present. If it is present, this method will return
126      * the unwrapped value.
127      *
128      * @param opt Optional to be checked
129      * @param source Statement source reference
130      * @param format Format string, according to {@link String#format(String, Object...)}.
131      * @param args Format string arguments, according to {@link String#format(String, Object...)}
132      * @return Object unwrapped from the opt optional
133      * @throws SourceException if the optional is not present
134      */
135     public static <T> @NonNull T unwrap(final Optional<T> opt, final @NonNull StatementSourceReference source,
136             final @NonNull String format, final Object... args) {
137         throwIf(opt.isEmpty(), source, format, args);
138         return opt.get();
139     }
140
141     private static String createMessage(final @NonNull String message, final @NonNull StatementSourceReference source) {
142         return requireNonNull(message) + " [at " + requireNonNull(source) + ']';
143     }
144 }