package com.github.arteam.jgcompare;

import com.google.common.collect.*;
import org.assertj.core.api.Assertions;
import org.junit.Test;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Date: 5/15/14
 * Time: 12:06 AM
 *
 * @author Artem Prigoda
 */
public class MapsTest {

    private final List<Team> teams = ImmutableList.of(
            new Team(12, "Chicago Blackhawks"),
            new Team(42, "St. Louis Blues"),
            new Team(88, "Colorado Avalanche"));

    @Test
    public void testUniqueIndex() {
        Map<Long, Team> expected = ImmutableMap.of(
                12L, new Team(12, "Chicago Blackhawks"),
                42L, new Team(42, "St. Louis Blues"),
                88L, new Team(88, "Colorado Avalanche"));
        assertThat(Maps.uniqueIndex(teams, Team::getId)).isEqualTo(expected);

        assertThat(teams.stream().collect(Collectors.toMap(Team::getId, Function.identity())))
                .isEqualTo(expected);
    }

    @Test
    public void testAsNavigableMap() {
        Map<Integer, String> teams = ImmutableMap.<Integer, String>builder()
                .put(21, "Boston Bruins")
                .put(24, "Los Angeles Kings")
                .put(12, "Chicago Blackhawks")
                .put(42, "St. Louis Blues")
                .put(29, "Arizona Coyotes")
                .put(92, "Winnipeg Jets")
                .put(88, "Colorado Avalanche")
                .build();
        Map<Integer, String> expected = ImmutableSortedMap.of(12, "Chicago Blackhawks", 42, "St. Louis Blues", 88, "Colorado Avalanche");

        Set<Integer> source = ImmutableSortedSet.of(42, 88, 12);
        assertThat(Maps.asMap(source, teams::get)).isEqualTo(expected);

        Map<Integer, String> result = source.stream()
                .collect(Collectors.toMap(Function.identity(), teams::get,
                        (s1, s2) -> {
                            throw new IllegalArgumentException("Two values for the same key");
                        },
                        TreeMap::new));
        assertThat(result).isEqualTo(expected);
    }
}

@@ -1716,16 +1716,25 @@
         
+Navigable
 Set%3CInte
@@ -2237,11 +2237,857 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testSetAsMap() %7B%0A        Map%3CInteger, String%3E teams = ImmutableMap.%3CInteger, String%3Ebuilder()%0A                .put(21, %22Boston Bruins%22)%0A                .put(24, %22Los Angeles Kings%22)%0A                .put(12, %22Chicago Blackhawks%22)%0A                .put(42, %22St. Louis Blues%22)%0A                .put(29, %22Arizona Coyotes%22)%0A                .put(92, %22Winnipeg Jets%22)%0A                .put(88, %22Colorado Avalanche%22)%0A                .build();%0A%0A        Map%3CInteger, String%3E expected = ImmutableMap.of(12, %22Chicago Blackhawks%22, 42, %22St. Louis Blues%22, 88, %22Colorado Avalanche%22);%0A        Set%3CInteger%3E source = ImmutableSet.of(42, 88, 12);%0A%0A        assertThat(Maps.asMap(source, teams::get)).isEqualTo(expected);%0A        assertThat(source.stream().collect(Collectors.toMap(Function.identity(), teams::get))).isEqualTo(expected);%0A    %7D
 %0A%7D%0A

Add testSetAsMap