org.hotrodorm.hotrod.utils.XUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hotrod-utils Show documentation
Show all versions of hotrod-utils Show documentation
HotRod is an ORM for Java, Spring and SpringBoot.
The newest version!
package org.hotrodorm.hotrod.utils;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class XUtil {
public static String trim(final Throwable e) {
return stream(e).map(c -> renderMessage(c)).collect(Collectors.joining(": "));
}
private static String renderMessage(final Throwable e) {
return (e.getClass().equals(ClassNotFoundException.class) ? "Class not found: "
: (e.getClass().equals(FileNotFoundException.class) ? "File not found: " : "")) + e.getMessage();
}
public static Stream stream(final Throwable e) {
IterableThrowable it = new IterableThrowable(e);
return StreamSupport.stream(it.spliterator(), false);
}
private static class IterableThrowable implements Iterable {
private Throwable e;
public IterableThrowable(final Throwable e) {
this.e = e;
}
@Override
public Iterator iterator() {
return new Iterator() {
@Override
public boolean hasNext() {
return e != null;
}
@Override
public Throwable next() {
if (e == null) {
throw new NoSuchElementException();
}
Throwable aux = e;
e = e.getCause();
return aux;
}
};
}
}
}