package io.reactivex.exceptions; import com.ubtrobot.jimu.robotapi.PeripheralType; import java.io.PrintStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; /* loaded from: classes2.dex */ public final class CompositeException extends RuntimeException { private static final long serialVersionUID = 3026362227162912146L; private Throwable cause; private final List exceptions; private final String message; static final class CompositeExceptionCausalChain extends RuntimeException { CompositeExceptionCausalChain() { } @Override // java.lang.Throwable public String getMessage() { return "Chain of Causes for CompositeException In Order Received =>"; } } static abstract class PrintStreamOrWriter { PrintStreamOrWriter() { } abstract void a(Object obj); } static final class WrappedPrintStream extends PrintStreamOrWriter { private final PrintStream a; WrappedPrintStream(PrintStream printStream) { this.a = printStream; } @Override // io.reactivex.exceptions.CompositeException.PrintStreamOrWriter void a(Object obj) { this.a.println(obj); } } static final class WrappedPrintWriter extends PrintStreamOrWriter { private final PrintWriter a; WrappedPrintWriter(PrintWriter printWriter) { this.a = printWriter; } @Override // io.reactivex.exceptions.CompositeException.PrintStreamOrWriter void a(Object obj) { this.a.println(obj); } } public CompositeException(Throwable... thArr) { this(thArr == null ? Collections.singletonList(new NullPointerException("exceptions was null")) : Arrays.asList(thArr)); } private void appendStackTrace(StringBuilder sb, Throwable th, String str) { sb.append(str); sb.append(th); sb.append('\n'); for (StackTraceElement stackTraceElement : th.getStackTrace()) { sb.append("\t\tat "); sb.append(stackTraceElement); sb.append('\n'); } if (th.getCause() != null) { sb.append("\tCaused by: "); appendStackTrace(sb, th.getCause(), ""); } } private List getListOfCauses(Throwable th) { ArrayList arrayList = new ArrayList(); Throwable cause = th.getCause(); if (cause != null && cause != th) { while (true) { arrayList.add(cause); Throwable cause2 = cause.getCause(); if (cause2 == null || cause2 == cause) { break; } cause = cause2; } } return arrayList; } @Override // java.lang.Throwable public synchronized Throwable getCause() { if (this.cause == null) { CompositeExceptionCausalChain compositeExceptionCausalChain = new CompositeExceptionCausalChain(); HashSet hashSet = new HashSet(); Iterator it = this.exceptions.iterator(); Throwable th = compositeExceptionCausalChain; while (it.hasNext()) { Throwable next = it.next(); if (!hashSet.contains(next)) { hashSet.add(next); for (Throwable th2 : getListOfCauses(next)) { if (hashSet.contains(th2)) { next = new RuntimeException("Duplicate found in causal chain so cropping to prevent loop ..."); } else { hashSet.add(th2); } } try { th.initCause(next); } catch (Throwable unused) { } th = getRootCause(th); } } this.cause = compositeExceptionCausalChain; } return this.cause; } public List getExceptions() { return this.exceptions; } @Override // java.lang.Throwable public String getMessage() { return this.message; } Throwable getRootCause(Throwable th) { Throwable cause = th.getCause(); if (cause == null || this.cause == cause) { return th; } while (true) { Throwable cause2 = cause.getCause(); if (cause2 == null || cause2 == cause) { break; } cause = cause2; } return cause; } @Override // java.lang.Throwable public void printStackTrace() { printStackTrace(System.err); } public int size() { return this.exceptions.size(); } @Override // java.lang.Throwable public void printStackTrace(PrintStream printStream) { printStackTrace(new WrappedPrintStream(printStream)); } @Override // java.lang.Throwable public void printStackTrace(PrintWriter printWriter) { printStackTrace(new WrappedPrintWriter(printWriter)); } public CompositeException(Iterable iterable) { LinkedHashSet linkedHashSet = new LinkedHashSet(); ArrayList arrayList = new ArrayList(); if (iterable != null) { for (Throwable th : iterable) { if (th instanceof CompositeException) { linkedHashSet.addAll(((CompositeException) th).getExceptions()); } else if (th != null) { linkedHashSet.add(th); } else { linkedHashSet.add(new NullPointerException("Throwable was null!")); } } } else { linkedHashSet.add(new NullPointerException("errors was null")); } if (!linkedHashSet.isEmpty()) { arrayList.addAll(linkedHashSet); this.exceptions = Collections.unmodifiableList(arrayList); this.message = this.exceptions.size() + " exceptions occurred. "; return; } throw new IllegalArgumentException("errors is empty"); } private void printStackTrace(PrintStreamOrWriter printStreamOrWriter) { StringBuilder sb = new StringBuilder(PeripheralType.SERVO); sb.append(this); sb.append('\n'); for (StackTraceElement stackTraceElement : getStackTrace()) { sb.append("\tat "); sb.append(stackTraceElement); sb.append('\n'); } int i = 1; for (Throwable th : this.exceptions) { sb.append(" ComposedException "); sb.append(i); sb.append(" :\n"); appendStackTrace(sb, th, "\t"); i++; } printStreamOrWriter.a(sb.toString()); } }