/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.util;

import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * Utility functions for {@link Throwable}.
 */
public class ThrowableUtils {
    private ThrowableUtils() {}

    /**
     * Returns the Throwable at the bottom of the {@link Throwable#getCause()} chain. If the first {@code getCause}
     * returns null, returns the parameter throwable.
     *
     * @param throwableToGetRootCauseOf The Throwable to find the root cause of.
     * @return The Throwable at the bottom of the {@code getCause} chain, or the parameter if the first getCause is null.
     */
    @CheckResult
    public static Throwable getRootCause(@NonNull final Throwable throwableToGetRootCauseOf) {
        Throwable bottommostCause = throwableToGetRootCauseOf.getCause();
        if (bottommostCause == null) { return throwableToGetRootCauseOf; }

        Throwable nonNullCause;
        do {
            nonNullCause = bottommostCause;
            bottommostCause = nonNullCause.getCause();
        } while (bottommostCause != null);
        return nonNullCause;
    }
}

@@ -361,16 +361,69 @@
 lable;%0A%0A
+import java.util.LinkedList;%0Aimport java.util.List;%0A%0A
 /**%0A * U
@@ -925,16 +925,29 @@
      */%0A
+    @NonNull%0A
     @Che
@@ -1387,16 +1387,16 @@
  null);%0A
-
         
@@ -1421,11 +1421,693 @@
 e;%0A    %7D
+%0A%0A    /**%0A     * Returns a sequence from the bottom-most Throwable from calling %7B@link java.lang.Throwable#getCause()%7D%0A     * repeatedly, then the Throwable one level above, etc., to the given Throwable, which is included in the list.%0A     */%0A    @NonNull%0A    @CheckResult%0A    public static List%3CThrowable%3E getRootCauseToTopThrowable(final Throwable topThrowable) %7B%0A        final LinkedList%3CThrowable%3E throwables = new LinkedList%3C%3E();%0A        Throwable currentThrowable = topThrowable;%0A        while (currentThrowable != null) %7B%0A            throwables.addFirst(currentThrowable);%0A            currentThrowable = currentThrowable.getCause();%0A        %7D%0A        return throwables;%0A    %7D
 %0A%7D%0A

Add ThrowableUtils.getRootCauseToTopThrowable.