// Path: mug/src/main/java/com/google/mu/util/MoreCollections.java
// public static <T, R> Optional<R> findFirstElements(
//     Collection<T> collection, BiFunction<? super T, ? super T, ? extends R> found) {
//   requireNonNull(found);
//   if (collection.size() < 2) return Optional.empty();
//   if (collection instanceof List && collection instanceof RandomAccess) {
//     List<T> list = (List<T>) collection;
//     return Optional.of(found.apply(list.get(0), list.get(1)));
//   }
//   Iterator<T> it = collection.iterator();
//   return Optional.of(found.apply(it.next(), it.next()));
// }
// 
// Path: mug/src/main/java/com/google/mu/util/MoreCollections.java
// public static <T, R> Optional<R> findOnlyElements(
//     Collection<T> collection, BiFunction<? super T, ? super T, ? extends R> found) {
//   requireNonNull(found);
//   if (collection.size() != 2) return Optional.empty();
//   if (collection instanceof List && collection instanceof RandomAccess) {
//     List<T> list = (List<T>) collection;
//     return Optional.of(found.apply(list.get(0), list.get(1)));
//   }
//   Iterator<T> it = collection.iterator();
//   return Optional.of(found.apply(it.next(), it.next()));
// }


import static com.google.common.truth.Truth8.assertThat;
import static com.google.mu.util.MoreCollections.findFirstElements;
import static com.google.mu.util.MoreCollections.findOnlyElements;
import static java.util.Arrays.asList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.google.common.collect.ImmutableSet;
import com.google.common.testing.NullPointerTester;
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
/*****************************************************************************
 * ------------------------------------------------------------------------- *
 * 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                                   *
 *                                                                           *
 * http://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.mu.util;





@RunWith(TestParameterInjector.class)
public class MoreCollectionsTest {
  @TestParameter private CollectionType makeCollection;

  @Test public void testFindOnlyTwoElements() {
    assertThat(findOnlyElements(makeCollection.of(1, 10), Integer::sum)).hasValue(11);
    assertThat(findOnlyElements(makeCollection.of(1), Integer::sum)).isEmpty();
    assertThat(findOnlyElements(makeCollection.of(1, 2, 3), Integer::sum)).isEmpty();
  }

  @Test public void testFindOnlyThreeElements() {
    assertThat(findOnlyElements(makeCollection.of(1, 3, 5), (a, b, c) -> a + b + c)).hasValue(9);
    assertThat(findOnlyElements(makeCollection.of(1, 2), (a, b, c) -> a + b + c)).isEmpty();
    assertThat(findOnlyElements(makeCollection.of(1, 2, 3, 4), (a, b, c) -> a + b + c)).isEmpty();
  }

  @Test public void testFindOnlyFourElements() {
    assertThat(findOnlyElements(makeCollection.of(1, 3, 5, 7), (a, b, c, d) -> a + b + c + d)).hasValue(16);
    assertThat(findOnlyElements(makeCollection.of(1, 2, 3), (a, b, c, d) -> a + b + c + d)).isEmpty();
// Path: mug/src/main/java/com/google/mu/util/MoreCollections.java
// public static <T, R> Optional<R> findFirstElements(
//     Collection<T> collection, BiFunction<? super T, ? super T, ? extends R> found) {
//   requireNonNull(found);
//   if (collection.size() < 2) return Optional.empty();
//   if (collection instanceof List && collection instanceof RandomAccess) {
//     List<T> list = (List<T>) collection;
//     return Optional.of(found.apply(list.get(0), list.get(1)));
//   }
//   Iterator<T> it = collection.iterator();
//   return Optional.of(found.apply(it.next(), it.next()));
// }
// 
// Path: mug/src/main/java/com/google/mu/util/MoreCollections.java
// public static <T, R> Optional<R> findOnlyElements(
//     Collection<T> collection, BiFunction<? super T, ? super T, ? extends R> found) {
//   requireNonNull(found);
//   if (collection.size() != 2) return Optional.empty();
//   if (collection instanceof List && collection instanceof RandomAccess) {
//     List<T> list = (List<T>) collection;
//     return Optional.of(found.apply(list.get(0), list.get(1)));
//   }
//   Iterator<T> it = collection.iterator();
//   return Optional.of(found.apply(it.next(), it.next()));
// }


// Path: mug/src/test/java/com/google/mu/util/MoreCollectionsTest.java
import static com.google.common.truth.Truth8.assertThat;
import static com.google.mu.util.MoreCollections.findFirstElements;
import static com.google.mu.util.MoreCollections.findOnlyElements;
import static java.util.Arrays.asList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.google.common.collect.ImmutableSet;
import com.google.common.testing.NullPointerTester;
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;

/*****************************************************************************
 * ------------------------------------------------------------------------- *
 * 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                                   *
 *                                                                           *
 * http://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.mu.util;





@RunWith(TestParameterInjector.class)
public class MoreCollectionsTest {
  @TestParameter private CollectionType makeCollection;

  @Test public void testFindOnlyTwoElements() {
    assertThat(findOnlyElements(makeCollection.of(1, 10), Integer::sum)).hasValue(11);
    assertThat(findOnlyElements(makeCollection.of(1), Integer::sum)).isEmpty();
    assertThat(findOnlyElements(makeCollection.of(1, 2, 3), Integer::sum)).isEmpty();
  }

  @Test public void testFindOnlyThreeElements() {
    assertThat(findOnlyElements(makeCollection.of(1, 3, 5), (a, b, c) -> a + b + c)).hasValue(9);
    assertThat(findOnlyElements(makeCollection.of(1, 2), (a, b, c) -> a + b + c)).isEmpty();
    assertThat(findOnlyElements(makeCollection.of(1, 2, 3, 4), (a, b, c) -> a + b + c)).isEmpty();
  }

  @Test public void testFindOnlyFourElements() {
    assertThat(findOnlyElements(makeCollection.of(1, 3, 5, 7), (a, b, c, d) -> a + b + c + d)).hasValue(16);
    assertThat(findOnlyElements(makeCollection.of(1, 2, 3), (a, b, c, d) -> a + b + c + d)).isEmpty();
    assertThat(findOnlyElements(makeCollection.of(1, 2, 3, 4, 5), (a, b, c, d) -> a + b + c)).isEmpty();