All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hotrodorm.hotrod.utils.XUtil Maven / Gradle / Ivy

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;
        }
      };
    }

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy