Deprecate simple DataTreeFactory.create()
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / meta / StatementSourceException.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.model.api.meta;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.IOException;
13 import java.io.NotSerializableException;
14 import java.io.ObjectInputStream;
15 import java.io.ObjectOutputStream;
16 import java.io.ObjectStreamException;
17 import org.eclipse.jdt.annotation.NonNull;
18
19 /**
20  * An exception identifying a problem detected at a particular YANG statement. Exposes {@link #sourceRef()} to that
21  * statement.
22  */
23 public class StatementSourceException extends RuntimeException {
24     @java.io.Serial
25     private static final long serialVersionUID = 2L;
26
27     private final @NonNull StatementSourceReference sourceRef;
28
29     public StatementSourceException(final StatementSourceReference sourceRef, final String message) {
30         super(createMessage(sourceRef, message));
31         this.sourceRef = requireNonNull(sourceRef);
32     }
33
34     public StatementSourceException(final StatementSourceReference sourceRef, final String message,
35             final Throwable cause) {
36         super(createMessage(sourceRef, message), cause);
37         this.sourceRef = requireNonNull(sourceRef);
38     }
39
40     public StatementSourceException(final StatementSourceReference sourceRef, final String format,
41             final Object... args) {
42         this(sourceRef, format.formatted(args));
43     }
44
45     public StatementSourceException(final StatementSourceReference sourceRef, final Throwable cause,
46             final String format, final Object... args) {
47         this(sourceRef, format.formatted(args), cause);
48     }
49
50     private static String createMessage(final StatementSourceReference sourceRef, final String message) {
51         return requireNonNull(message) + " [at " + requireNonNull(sourceRef) + ']';
52     }
53
54     /**
55      * Return the reference to the source which caused this exception.
56      *
57      * @return the reference to the source which caused this exception
58      */
59     public final @NonNull StatementSourceReference sourceRef() {
60         return sourceRef;
61     }
62
63     @java.io.Serial
64     private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
65         throwNSE();
66     }
67
68     @java.io.Serial
69     private void readObjectNoData() throws ObjectStreamException {
70         throwNSE();
71     }
72
73     @java.io.Serial
74     private void writeObject(final ObjectOutputStream stream) throws IOException {
75         throwNSE();
76     }
77
78     protected final void throwNSE() throws NotSerializableException {
79         throw new NotSerializableException(getClass().getName());
80     }
81 }