/*
 * Copyright 2018 Google LLC
 *
 * 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
 *
 *     https://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 com.google.android.gnd.rx;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.reactivex.Completable;
import io.reactivex.Single;
import java.util.concurrent.Callable;
import java8.util.function.Consumer;
import java8.util.function.Supplier;

/** Helpers for working with RxJava Completable classes. */
public abstract class RxCompletable {
  /** Do not instantiate. */
  private RxCompletable() {}

  public static Completable completeIf(Callable<Boolean> conditionFunction) {
    return Completable.create(
        em -> {
          if (conditionFunction.call()) {
            em.onComplete();
          }
        });
  }

  public static Completable completeOrError(
      Supplier<Boolean> supplier, Class<? extends Throwable> errorClass) {
    return Completable.create(
        em -> {
          if (supplier.get()) {
            em.onComplete();
          } else {
            em.onError(errorClass.newInstance());
          }
        });
  }

  /**
   * Receives a {@link Completable} and returns a {@link Single <Boolean>} that emits <code>true
   * </code> if the <code>Completable</code> completes successful, <code>false</code> otherwise. An
   * action to be performed in case or error can also be passed through the argument <code>
   * exceptionConsumer</code>.
   *
   * @param completable The input {@link Completable}
   * @param exceptionConsumer A consumer to process the exception (eg log it) in case the {@link
   *     Completable} terminates with an error.
   * @return The {@link Single<Boolean>}, as described above.
   */
  public static Single<Boolean> toSingle(
      @NonNull Completable completable, @Nullable Consumer<? super Throwable> exceptionConsumer) {
    return completable
        .doOnError(
            throwable -> {
              if (exceptionConsumer != null) {
                exceptionConsumer.accept(throwable);
              }
            })
        .toSingleDefault(true)
        .onErrorReturnItem(false);
  }
}

@@ -1584,606 +1584,8 @@
  %7D%0A%0A
-  /**%0A   * Receives a %7B@link Completable%7D and returns a %7B@link Single %3CBoolean%3E%7D that emits %3Ccode%3Etrue%0A   * %3C/code%3E if the %3Ccode%3ECompletable%3C/code%3E completes successful, %3Ccode%3Efalse%3C/code%3E otherwise. An%0A   * action to be performed in case or error can also be passed through the argument %3Ccode%3E%0A   * exceptionConsumer%3C/code%3E.%0A   *%0A   * @param completable The input %7B@link Completable%7D%0A   * @param exceptionConsumer A consumer to process the exception (eg log it) in case the %7B@link%0A   *     Completable%7D terminates with an error.%0A   * @return The %7B@link Single%3CBoolean%3E%7D, as described above.%0A   */%0A
   pu

Remove javadoc (causes checkstyle error)