/*
 * 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.ground.util;

import com.google.common.collect.ImmutableList;
import java.util.Collection;
import java8.util.function.Function;
import java8.util.function.Predicate;
import java8.util.stream.Collector;
import java8.util.stream.Collectors;
import java8.util.stream.StreamSupport;

/**
 * Custom collector for compatibility between {@link Collector} compat class and Guava {@link
 * ImmutableList}.
 */
public abstract class ImmutableListCollector {
  private static final Collector<Object, ?, ImmutableList<Object>> TO_IMMUTABLE_LIST =
      Collectors.of(
          ImmutableList::builder,
          ImmutableList.Builder::add,
          (a, b) -> {
            throw new UnsupportedOperationException();
          },
          ImmutableList.Builder::build);

  /** Do not instantiate. */
  private ImmutableListCollector() {}

  /**
   * Returns a {@link Collector} that accumulates the input elements into a new ImmutableList, in
   * encounter order.
   */
  public static <E> Collector<E, ?, ImmutableList<E>> toImmutableList() {
    return (Collector) TO_IMMUTABLE_LIST;
  }

  /**
   * Returns a function for use with RxJava Observables that maps a function over the contents of an
   * immutable list and recollects the results back into an immutable list.
   *
   * <p>This eliminates list handling boilerplate when lists are used as rx stream values.
   */
  public static <E, T>
      io.reactivex.functions.Function<Collection<T>, ImmutableList<E>> mapAndRecollect(
          Function<T, E> func) {
    return x -> StreamSupport.stream(x).map(func).collect(toImmutableList());
  }

  /**
   * Returns a function for use with RxJava Observables that filters a list using a provided
   * function and recollects the results back into an immutable list.
   *
   * <p>This eliminates list handling boilerplate when lists are used as rx stream values.
   */
  public static <T>
      io.reactivex.functions.Function<Collection<T>, ImmutableList<T>> filterAndRecollect(
          Predicate<T> func) {
    return x -> StreamSupport.stream(x).filter(func).collect(toImmutableList());
  }
}

@@ -682,112 +682,8 @@
 st;%0A
-import java.util.Collection;%0Aimport java8.util.function.Function;%0Aimport java8.util.function.Predicate;%0A
 impo
@@ -754,48 +754,8 @@
 ors;
-%0Aimport java8.util.stream.StreamSupport;
 %0A%0A/*
@@ -1555,1018 +1555,7 @@
 %0A  %7D
-%0A%0A  /**%0A   * Returns a function for use with RxJava Observables that maps a function over the contents of an%0A   * immutable list and recollects the results back into an immutable list.%0A   *%0A   * %3Cp%3EThis eliminates list handling boilerplate when lists are used as rx stream values.%0A   */%0A  public static %3CE, T%3E%0A      io.reactivex.functions.Function%3CCollection%3CT%3E, ImmutableList%3CE%3E%3E mapAndRecollect(%0A          Function%3CT, E%3E func) %7B%0A    return x -%3E StreamSupport.stream(x).map(func).collect(toImmutableList());%0A  %7D%0A%0A  /**%0A   * Returns a function for use with RxJava Observables that filters a list using a provided%0A   * function and recollects the results back into an immutable list.%0A   *%0A   * %3Cp%3EThis eliminates list handling boilerplate when lists are used as rx stream values.%0A   */%0A  public static %3CT%3E%0A      io.reactivex.functions.Function%3CCollection%3CT%3E, ImmutableList%3CT%3E%3E filterAndRecollect(%0A          Predicate%3CT%3E func) %7B%0A    return x -%3E StreamSupport.stream(x).filter(func).collect(toImmutableList());%0A  %7D
 %0A%7D%0A

Remove unused helper methods