Guava之Fluentlterable

public class FluentIterableExampleTest
{

    private FluentIterable build()
    {
        ArrayList list = Lists.newArrayList("Alex", "Wang", "Guava", "Scala");
        return FluentIterable.from(list);
    }

    @Test
    public void testFilter()
    {
        FluentIterable fit = build();
        assertThat(fit.size(), equalTo(4));

        FluentIterable result = fit.filter(e -> e != null && e.length() > 4);
        assertThat(result.size(), equalTo(2));
    }

    @Test
    public void testAppend()
    {
        FluentIterable fit = build();
        ArrayList append = Lists.newArrayList("APPEND");
        assertThat(fit.size(), equalTo(4));
        FluentIterable appendFI = fit.append(append);
        assertThat(appendFI.size(), equalTo(5));
        assertThat(appendFI.contains("APPEND"), is(true));

        appendFI = appendFI.append("APPEND2");
        assertThat(appendFI.size(), equalTo(6));
        assertThat(appendFI.contains("APPEND2"), is(true));
        assertThat(appendFI.contains("Alex"), is(true));
    }

    @Test
    public void testMatch()
    {
        FluentIterable fit = build();
        boolean result = fit.allMatch(e -> e != null && e.length() >= 4);
        assertThat(result, is(true));

        result = fit.anyMatch(e -> e != null && e.length() == 5);
        assertThat(result, is(true));

        Optional optional = fit.firstMatch(e -> e != null && e.length() == 5);
        assertThat(optional.isPresent(), is(true));
        assertThat(optional.get(), equalTo("Guava"));
    }

    @Test
    public void testFirst$Last()
    {
        FluentIterable fit = build();
        Optional optional = fit.first();
        assertThat(optional.isPresent(), is(true));
        assertThat(optional.get(), equalTo("Alex"));

        optional = fit.last();
        assertThat(optional.isPresent(), is(true));
        assertThat(optional.get(), equalTo("Scala"));
    }

    @Test
    public void testLimit()
    {
        FluentIterable fit = build();
        FluentIterable limit = fit.limit(3);
        System.out.println(limit);
        assertThat(limit.contains("Scala"), is(false));

        limit = fit.limit(300);
        System.out.println(limit);
        assertThat(limit.contains("Scala"), is(true));

    }

    /**
     * DSL
     */
    @Test
    public void testCopyIn()
    {
        FluentIterable fit = build();
        ArrayList list = Lists.newArrayList("Java");
        ArrayList result = fit.copyInto(list);

        assertThat(result.size(), equalTo(5));
        assertThat(result.contains("Scala"), is(true));
    }

    @Test
    public void testCycle()
    {
        FluentIterable fit = build();
        FluentIterable cycle = fit.cycle().limit(20);
        cycle.forEach(System.out::println);
    }

    @Test
    public void testTransform()
    {
        FluentIterable fit = build();
        fit.transform(e -> e.length()).forEach(System.out::println);
    }

    @Test
    public void testTransformAndConcat()
    {
        FluentIterable fit = build();
        List list = Lists.newArrayList(1);
        FluentIterable result = fit.transformAndConcat(e -> list);
        result.forEach(System.out::println);
    }

    /**
     * A----->API---->B(Server)
     * 1,2
     */
    @Test
    public void testTransformAndConcatInAction()
    {
        ArrayList cTypes = Lists.newArrayList(1, 2);
        FluentIterable.from(cTypes).transformAndConcat(this::search)
                .forEach(System.out::println);
    }

    @Test
    public void testJoin()
    {
        FluentIterable fit = build();
        String result = fit.join(Joiner.on(','));
        assertThat(result, equalTo("Alex,Wang,Guava,Scala"));
    }


    private List search(int type)
    {
        if (type == 1)
        {
            return Lists.newArrayList(new Customer(type, "Alex"), new Customer(type, "Tina"));
        } else
        {
            return Lists.newArrayList(new Customer(type, "Wang"), new Customer(type, "Wen"), new Customer(type, "Jun"));
        }
    }

    class Customer
    {
        final int type;
        final String name;

        Customer(int type, String name)
        {
            this.type = type;
            this.name = name;
        }

        @Override
        public String toString()
        {
            return "Customer{" +
                    "type=" + type +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}

你可能感兴趣的:(Guava之Fluentlterable)