
org.ardulink.util.Throwables Maven / Gradle / Ivy
The newest version!
/**
Copyright 2013 project Ardulink http://www.ardulink.org/
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.ardulink.util;
import static java.util.stream.Stream.concat;
import static java.util.stream.Stream.empty;
import static org.ardulink.util.Preconditions.checkNotNull;
import static org.ardulink.util.Streams.getLast;
import java.util.stream.Stream;
import org.ardulink.util.anno.LapsedWith;
/**
* [ardulinktitle] [ardulinkversion]
*
* project Ardulink http://www.ardulink.org/
*
* [adsense]
*
*/
public final class Throwables {
private Throwables() {
super();
}
public static Throwable getRootCause(Throwable throwable) {
return getLast(getCauses(throwable)).orElse(throwable);
}
public static Stream getCauses(Throwable throwable) {
return _getCauses(throwable);
}
@LapsedWith(module = LapsedWith.JDK9, value = "Stream#iterate(throwable, Objects::nonNull, Throwable::getCause)")
private static Stream _getCauses(Throwable throwable) {
return concat(Stream.of(throwable), throwable.getCause() == null ? empty() : _getCauses(throwable.getCause()));
}
public static RuntimeException propagate(Throwable throwable) {
propagateIfPossible(checkNotNull(throwable, "throwable must not be null"));
throw new RuntimeException(throwable);
}
public static void propagateIfPossible(Throwable throwable) {
propagateIfInstanceOf(throwable, Error.class);
propagateIfInstanceOf(throwable, RuntimeException.class);
}
public static void propagateIfInstanceOf(Throwable throwable, Class type) throws T {
if (type.isInstance(throwable)) {
throw type.cast(throwable);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy