package com.atlassian.bitbucket.util;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import org.junit.Test;

import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class MoreCollectorsTest {

    @Test
    public void testToImmutableSet() {
        Set<Character> set = ImmutableSet.of('a', 'B', 'c').stream()
                .filter(Character::isLowerCase)
                .collect(MoreCollectors.toImmutableSet());
        assertEquals(ImmutableSet.of('a', 'c'), set);
        assertEquals(Character.valueOf('a'), Iterables.get(set, 0)); //Verify the order is stable
        assertTrue(set instanceof ImmutableSet);
    }

    @Test
    public void testToImmutableSetParallel() {
        Set<Character> set = ImmutableSet.of('a', 'B', 'c').stream()
                .parallel()
                .filter(Character::isLowerCase)
                .collect(MoreCollectors.toImmutableSet());
        assertEquals(ImmutableSet.of('a', 'c'), set);
        assertEquals(Character.valueOf('a'), Iterables.get(set, 0)); //Verify the order is stable
        assertTrue(set instanceof ImmutableSet);
    }

    @Test
    public void testToImmutableList() throws Exception {
        List<String> list = ImmutableList.of('a', 'b', 'c').stream()
                .map(String::valueOf)
                .collect(MoreCollectors.toImmutableList());
        assertEquals(ImmutableList.of("a", "b", "c"), list);
        assertTrue(list instanceof ImmutableList);
    }

    @Test
    public void testToImmutableListParallel() throws Exception {
        List<String> list = ImmutableList.of('a', 'b', 'c').stream()
                .parallel()
                .map(String::valueOf)
                .collect(MoreCollectors.toImmutableList());
        assertEquals(ImmutableList.of("a", "b", "c"), list);
        assertTrue(list instanceof ImmutableList);
    }
}