/*
 * Copyright 2019 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 io.reactivex.Single;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import javax.annotation.Nullable;

public class Result<T> {
  public enum State {
    SUCCESS,
    ERROR
  }

  private final State state;

  @Nullable private final T value;
  @Nullable private final Throwable error;

  private Result(State state, @Nullable T value, @Nullable Throwable error) {
    this.state = state;
    this.value = value;
    this.error = error;
  }

  public static <T> Result<T> success(@NonNull T result) {
    return new Result<>(State.SUCCESS, result, null);
  }

  public static <T> Result<T> error(@NonNull Throwable t) {
    return new Result<>(State.ERROR, null, t);
  }

  @Nullable
  public State getState() {
    return state;
  }

  @Nullable
  public T get() {
    return value;
  }

  @Nullable
  public Throwable getError() {
    return error;
  }

  public static <T, R> Function<T, Single<Result<R>>> wrapErrors(
      @NonNull Function<T, Single<R>> fn) {
    return (T value) -> fn.apply(value).map(Result::success).onErrorReturn(Result::error);
  }

  public static <T> Consumer<? super Result<T>> unwrapErrors(
      Consumer<T> onSuccess, Consumer<Throwable> onError) {
    return result -> {
      switch (result.getState()) {
        case SUCCESS:
          onSuccess.accept(result.get());
          break;
        case ERROR:
          onError.accept(result.getError());
          break;
      }
    };
  }
}

@@ -625,16 +625,48 @@
 nd.rx;%0A%0A
+import io.reactivex.Observable;%0A
 import i
@@ -681,24 +681,24 @@
 vex.Single;%0A
-
 import io.re
@@ -1648,30 +1648,30 @@
 lt%3CR%3E%3E%3E wrap
-Errors
+Single
 (%0A      @Non
@@ -1792,32 +1792,905 @@
 t::error);%0A  %7D%0A%0A
+  public static %3CT, R%3E Function%3CT, Observable%3CResult%3CR%3E%3E%3E wrapObservable(%0A      @NonNull Function%3CT, Observable%3CR%3E%3E fn) %7B%0A    return (T value) -%3E fn.apply(value).map(Result::success).onErrorReturn(Result::error);%0A  %7D%0A%0A  /**%0A   * Wraps observables in a Result without applying any operation. Equivalent to mapping %22id%22 on the%0A   * observable or %22casting%22 or %22lifting%22 a given observable into a Result.%0A   *%0A   * %3Cp%3EThis is mostly needed for situations in which we need to use a different operator aside from%0A   * map, such as zipWith, but want to eventually catch any errors that might propagate downstream.%0A   *%0A   * @param observable%0A   * @param %3CT%3E The type of the observable's emissions.%0A   * @return%0A   */%0A  public static %3CT%3E Observable%3CResult%3CT%3E%3E wrapObservable(Observable%3CT%3E observable) %7B%0A    return observable.map(Result::success).onErrorReturn(Result::error);%0A  %7D%0A%0A
   public static 
@@ -2731,14 +2731,8 @@
 wrap
-Errors
 (%0A  

Add additional utilities to Result.