Skip to content

vadyalex/rill

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rill

Build Status Maven Central Javadocs License: MIT

This tiny library is fluent facade for Java 8 streams as well as some additional stream operation goodies, like stream zipping and stream concatenation.

Motivation

  • Join streams

Standard approach to join two streams:

        final Stream<Integer> stream1 = Stream.of(1);
        final Stream<Integer> stream2 = Stream.of(2);

        final Stream<Integer> result = Stream.concat(
            stream1,
            stream2
        );
> [1, 2]

Easy, right? But if you want to join four streams?

Standard approach:

       final Stream<Integer> stream1 = Stream.of(1);
       final Stream<Integer> stream2 = Stream.of(2);
       final Stream<Integer> stream3 = Stream.of(3);
       final Stream<Integer> stream4 = Stream.of(4);

       final Stream<Integer> result = Stream.concat(
               stream1,
               Stream.concat(
                       stream2,
                       Stream.concat(
                               stream3,
                               stream4
                       )
               )
       );
> [1, 2, 3, 4]

Rill way:

        final Stream<Integer> stream1 = Stream.of(1);
        final Stream<Integer> stream2 = Stream.of(2);
        final Stream<Integer> stream3 = Stream.of(3);
        final Stream<Integer> stream4 = Stream.of(4);

        final Stream<Integer> result = Rill
                .from(stream1)
                .join(stream2, stream3, stream4);
> [1, 2, 3, 4]

Filter out something in between joining?

Standard way:

        final Stream<Integer> stream1 = Stream.of(-1, 1);
        final Stream<Integer> stream2 = Stream.of(2, 5);
        final Stream<Integer> stream3 = Stream.of(3, 0);
        final Stream<Integer> stream4 = Stream.of(4, 5);

        final Stream<Integer> result = Stream.concat(
                Stream
                        .concat(
                                Stream
                                        .concat(
                                                stream1,
                                                stream2
                                        )
                                        .filter(
                                                i -> i != 5
                                        ),
                                stream3
                        )
                        .filter(
                                i -> i != 0
                        ),
                stream4
        );
> [-1, 1, 2, 3, 4, 5]

Rill way:

        final Stream<Integer> stream1 = Stream.of(-1, 1);
        final Stream<Integer> stream2 = Stream.of(2, 5);
        final Stream<Integer> stream3 = Stream.of(3, 0);
        final Stream<Integer> stream4 = Stream.of(4, 5);

        final Stream<Integer> result = Rill
                .from(stream1)
                .join(stream2)
                .filter(
                        i -> i != 5
                )
                .join(stream3)
                .filter(
                        i -> i != 0
                )
                .join(stream4);
> [-1, 1, 2, 3, 4, 5]
  • Zip streams
        final String<String> result = Rill
                .zip(
                        stream0,
                        stream1,
                        stream2,
                        stream3
                )
                .map(
                        quadruple -> quadruple._0.orElse("#UNKNOWN") + ": " + quadruple._1.map(value -> value + " ").orElse("") + quadruple._2.orElse("") + " -> " + quadruple._3.orElse(-1)
                );
> [#001: Mr Joe -> 30, #002: Ms Ann -> 27, #003: Ms Olivia -> -1, #UNKNOWN: Jonas -> -1]
  • Immutable collectors using Google Guava

Drop Google Guava to classpath and collect content of the stream into immutable collections of your choise:

        ImmutableList<String> list = Rill
                .from("A", "B", "C")
                .collect(
                        ImmutableCollectors.toImmutableList()
                );
> ["A", "B", "C"]
        ImmutableSet<String> set = Rill
                .from("A", "B", "C")
                .collect(
                        ImmutableCollectors.toImmutableSet()
                );
> ["A", "B", "C"]

Or using FluentCollector:

        ImmutableList<String> list = Rill
                .from("A", "B", "C")
                .collect()
                .toImmutableList();
> ["A", "B", "C"]
        ImmutableSet<String> set = Rill
                .from("A", "B", "C")
                .collect()
                .toImmutableSet();
> ["A", "B", "C"]

Make an ImmutableMap:

        final ImmutableMap<String, Integer> result = Rill
                .<String>from()
                .join(
                        Rill.from("A")
                )
                .join("B")
                .join("X", "X", "X")
                .join(
                        ImmutableList.of("X", "X")
                )
                .join(
                        Iterators.forArray("X", "X")
                )
                .filter(
                        s -> !s.equals("X")
                )
                .join("C")
                .zip(
                        0, 1, 2
                )
                .map(
                        couple -> Maps.immutableEntry(
                                couple.first().orElse(""),
                                couple.second().orElse(-1)
                        )
                )
                .collect(
                        ImmutableCollectors.toImmutableMap()
                );
> {A=0, B=1, C=2}

Dependency

Add a dependency to project using Maven:

<dependency>
  <groupId>me.vadyalex</groupId>
  <artifactId>rill</artifactId>
  <version>1.0.1</version>
</dependency>

Add a dependency using Gradle:

dependencies {
  compile 'me.vadyalex:rill:1.0.1'
}

About

Fluent Java 8 streams

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages