Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/util/ExceptionHelper.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.util;
15
16 import java.util.*;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.atomic.AtomicReference;
19
20 import io.reactivex.rxjava3.exceptions.CompositeException;
21
22 /**
23 * Terminal atomics for Throwable containers.
24 */
25 public final class ExceptionHelper {
26
27 /** Utility class. */
28 private ExceptionHelper() {
29 throw new IllegalStateException("No instances!");
30 }
31
32 /**
33 * If the provided Throwable is an Error this method
34 * throws it, otherwise returns a RuntimeException wrapping the error
35 * if that error is a checked exception.
36 * @param error the error to wrap or throw
37 * @return the (wrapped) error
38 */
39 public static RuntimeException wrapOrThrow(Throwable error) {
40 if (error instanceof Error) {
41 throw (Error)error;
42 }
43 if (error instanceof RuntimeException) {
44 return (RuntimeException)error;
45 }
46 return new RuntimeException(error);
47 }
48
49 /**
50 * A singleton instance of a Throwable indicating a terminal state for exceptions,
51 * don't leak this.
52 */
53 public static final Throwable TERMINATED = new Termination();
54
55 public static boolean addThrowable(AtomicReference<Throwable> field, Throwable exception) {
56 for (;;) {
57 Throwable current = field.get();
58
59 if (current == TERMINATED) {
60 return false;
61 }
62
63 Throwable update;
64 if (current == null) {
65 update = exception;
66 } else {
67 update = new CompositeException(current, exception);
68 }
69
70 if (field.compareAndSet(current, update)) {
71 return true;
72 }
73 }
74 }
75
76 public static Throwable terminate(AtomicReference<Throwable> field) {
77 Throwable current = field.get();
78 if (current != TERMINATED) {
79 current = field.getAndSet(TERMINATED);
80 }
81 return current;
82 }
83
84 /**
85 * Returns a flattened list of Throwables from tree-like CompositeException chain.
86 * @param t the starting throwable
87 * @return the list of Throwables flattened in a depth-first manner
88 */
89 public static List<Throwable> flatten(Throwable t) {
90 List<Throwable> list = new ArrayList<>();
91 ArrayDeque<Throwable> deque = new ArrayDeque<>();
92 deque.offer(t);
93
94 while (!deque.isEmpty()) {
95 Throwable e = deque.removeFirst();
96 if (e instanceof CompositeException) {
97 CompositeException ce = (CompositeException) e;
98 List<Throwable> exceptions = ce.getExceptions();
99 for (int i = exceptions.size() - 1; i >= 0; i--) {
100 deque.offerFirst(exceptions.get(i));
101 }
102 } else {
103 list.add(e);
104 }
105 }
106
107 return list;
108 }
109
110 /**
111 * Workaround for Java 6 not supporting throwing a final Throwable from a catch block.
112 * @param <E> the generic exception type
113 * @param e the Throwable error to return or throw
114 * @return the Throwable e if it is a subclass of Exception
115 * @throws E the generic exception thrown
116 */
117 @SuppressWarnings("unchecked")
118 public static <E extends Throwable> Exception throwIfThrowable(Throwable e) throws E {
119 if (e instanceof Exception) {
120 return (Exception)e;
121 }
122 throw (E)e;
123 }
124
125 public static String timeoutMessage(long timeout, TimeUnit unit) {
126 return "The source did not signal an event for "
127 + timeout
128 + " "
129 + unit.toString().toLowerCase()
130 + " and has been terminated.";
131 }
132
133 static final class Termination extends Throwable {
134
135 private static final long serialVersionUID = -4649703670690200604L;
136
137 Termination() {
138 super("No further exceptions");
139 }
140
141 @Override
142 public Throwable fillInStackTrace() {
143 return this;
144 }
145 }
146
147 /**
148 * Composes a String with a null warning message.
149 * @param prefix the prefix to add to the message.
150 * @return the composed String
151 * @since 3.0.0
152 */
153 public static String nullWarning(String prefix) {
154 return prefix + " Null values are generally not allowed in 3.x operators and sources.";
155 }
156
157 /**
158 * Creates a NullPointerException with a composed message via {@link #nullWarning(String)}.
159 * @param prefix the prefix to add to the message.
160 * @return the composed String
161 * @since 3.0.0
162 */
163 public static NullPointerException createNullPointerException(String prefix) {
164 return new NullPointerException(nullWarning(prefix));
165 }
166
167 /**
168 * Similar to Objects.requireNonNull but composes the error message via
169 * {@link #nullWarning(String)}.
170 * @param <T> the value type
171 * @param value the value to check
172 * @param prefix the prefix to the error message
173 * @return the value
174 * @throws NullPointerException if value is null
175 * @since 3.0.0
176 */
177 public static <T> T nullCheck(T value, String prefix) {
178 if (value == null) {
179 throw createNullPointerException(prefix);
180 }
181 return value;
182 }
183 }