Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vadyalex/rill

Fluent Java streams
https://github.com/vadyalex/rill

java stream

Last synced: about 1 month ago
JSON representation

Fluent Java streams

Awesome Lists containing this project

README

        

## rill

[![Build Status](https://travis-ci.org/vadyalex/rill.svg)](https://travis-ci.org/vadyalex/rill)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/me.vadyalex/rill/badge.svg)](https://maven-badges.herokuapp.com/maven-central/me.vadyalex/rill)
[![Javadocs](http://www.javadoc.io/badge/me.vadyalex/rill.svg)](http://www.javadoc.io/doc/me.vadyalex/rill)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

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

### Motivation

- Join streams

Standard approach to join two streams:

```java
final Stream stream1 = Stream.of(1);
final Stream stream2 = Stream.of(2);

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

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

Standard approach:

```java
final Stream stream1 = Stream.of(1);
final Stream stream2 = Stream.of(2);
final Stream stream3 = Stream.of(3);
final Stream stream4 = Stream.of(4);

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

Rill way:

```java
final Stream stream1 = Stream.of(1);
final Stream stream2 = Stream.of(2);
final Stream stream3 = Stream.of(3);
final Stream stream4 = Stream.of(4);

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

Filter out something in between joining?

Standard way:
```java
final Stream stream1 = Stream.of(-1, 1);
final Stream stream2 = Stream.of(2, 5);
final Stream stream3 = Stream.of(3, 0);
final Stream stream4 = Stream.of(4, 5);

final Stream 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:

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

final Stream 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

```java
final 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)
);
```
```java
> [#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:

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

Or using FluentCollector:

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

Make an ImmutableMap:

```java
final ImmutableMap result = Rill
.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:

```xml

me.vadyalex
rill
1.0.1

```

Add a dependency using Gradle:

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