Populate xpath/ hierarchy
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / InferenceException.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.meta;
9
10 import org.eclipse.jdt.annotation.NonNull;
11 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
12 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
13
14 /**
15  * Thrown when there is an inference error.
16  */
17 public class InferenceException extends SourceException {
18     private static final long serialVersionUID = 1L;
19
20     public InferenceException(final @NonNull String message, final @NonNull StatementSourceReference source) {
21         super(message, source);
22     }
23
24     public InferenceException(final @NonNull String message, final @NonNull StatementSourceReference source,
25             final Throwable cause) {
26         super(message, source, cause);
27     }
28
29     public InferenceException(final @NonNull StatementSourceReference source, final @NonNull String format,
30             final Object... args) {
31         this(String.format(format, args), source);
32     }
33
34     public InferenceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt) {
35         super(message, stmt);
36     }
37
38     public InferenceException(final @NonNull String message, final @NonNull CommonStmtCtx stmt, final Throwable cause) {
39         super(message, stmt, cause);
40     }
41
42     public InferenceException(final @NonNull CommonStmtCtx stmt, final @NonNull String format,
43             final Object... args) {
44         this(stmt.sourceReference(), format, args);
45     }
46
47     /**
48      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
49      * this method does nothing.
50      *
51      * @param expression Expression to be evaluated
52      * @param source Statement source reference
53      * @param format Format string, according to {@link String#format(String, Object...)}.
54      * @param args Format string arguments, according to {@link String#format(String, Object...)}
55      * @throws InferenceException if the expression evaluates to true.
56      */
57     public static void throwIf(final boolean expression, final @NonNull StatementSourceReference source,
58             final @NonNull String format, final Object... args) {
59         if (expression) {
60             throw new InferenceException(source, format, args);
61         }
62     }
63
64     /**
65      * Throw an instance of this exception if an expression evaluates to true. If the expression evaluates to false,
66      * this method does nothing.
67      *
68      * @param expression Expression to be evaluated
69      * @param stmt Statement context
70      * @param format Format string, according to {@link String#format(String, Object...)}.
71      * @param args Format string arguments, according to {@link String#format(String, Object...)}
72      * @throws InferenceException if the expression evaluates to true.
73      */
74     public static void throwIf(final boolean expression, final @NonNull CommonStmtCtx stmt,
75             final @NonNull String format, final Object... args) {
76         if (expression) {
77             throw new InferenceException(stmt.sourceReference(), format, args);
78         }
79     }
80 }