-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dharmesh Gohil
committed
Jul 29, 2017
1 parent
209a0dc
commit 2e40996
Showing
5 changed files
with
136 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,63 @@ | ||
# Flow | ||
The Very light weight java 8 like stream for Android | ||
The Very light weight java 8 like stream for Android. | ||
|
||
|
||
# Usage | ||
|
||
### Filter | ||
|
||
```java | ||
Flow.of(1, 2, 3, 4, 5, 6, 7, 8, 9) | ||
.filter(n -> n % 2 == 0) | ||
.forEach(System.out::println); | ||
``` | ||
|
||
### Reduce | ||
|
||
```java | ||
Integer sum = Flow.of(1, 2, 3, 4, 5, 6, 7, 8, 9) | ||
.reduce(0, (n1, n2) -> n1 + n2); | ||
System.out.println(sum); //output: 45 | ||
|
||
``` | ||
|
||
### First Match | ||
```java | ||
List<Integer> evens = Flow.of(1,2,3,4,5,6,7).filter(i -> i%2).toList(); | ||
String firstMatch = Flow.of("USA", "Canada", "India", "Russia", "China") | ||
.firstMatch(c -> c.endsWith("ia")); | ||
System.out.println(firstMatch); //India | ||
|
||
``` | ||
|
||
### Map | ||
|
||
```java | ||
Flow.of("USA", "Canada", "India", "Russia", "China") | ||
.map(String::length).forEach(System.out::println); | ||
|
||
``` | ||
|
||
### How to include in your Android project | ||
|
||
#### 1. Add this to your project `build.gradle` | ||
|
||
```javascript | ||
allprojects { | ||
repositories { | ||
... | ||
maven { url 'https://jitpack.io' } | ||
} | ||
} | ||
``` | ||
|
||
#### 2. Add this to your app module's build.gradle file: | ||
|
||
```javascript | ||
dependencies { | ||
... | ||
compile 'com.github.00ec454:Flow:0.5' | ||
} | ||
``` | ||
check the of lib [latest version](https://jitpack.io/#00ec454/Flow) | ||
|
||
#### 3. And start using .... :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.vistrav.flow; | ||
|
||
@FunctionalInterface | ||
public interface Function<T, R> { | ||
|
||
R apply(T value1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,64 @@ | ||
package com.vistrav.flow; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class FlowTest { | ||
|
||
public static void main(String[] args) { | ||
String[] tests = {"groupon", "google", "microsoft", "linkedin"}; | ||
boolean result = Flow.of(tests).anyMatch(test -> test.startsWith("grou")); | ||
System.out.println("AnyMatch :: " + result); | ||
Flow<String> countries; | ||
|
||
@BeforeMethod | ||
public void setUp() throws Exception { | ||
countries = Flow.of("USA", "Canada", "United Kingdom", "India", "Russia", "China"); | ||
Flow.of(1, 2, 3, 4, 5, 6, 7, 8, 9) | ||
.filter(n -> n % 2 == 0) | ||
.forEach(System.out::println); | ||
|
||
Integer sum = Flow.of(1, 2, 3, 4, 5, 6, 7, 8, 9) | ||
.reduce(0, (n1, n2) -> n1 + n2); | ||
System.out.println(sum); | ||
|
||
String firstMatch = Flow.of("USA", "Canada", "United Kingdom", "India", "Russia", "China") | ||
.firstMatch(country -> country.endsWith("ia")); | ||
System.out.println(firstMatch); | ||
|
||
Flow.of("USA", "Canada", "India", "Russia", "China") | ||
.map(String::length).forEach(System.out::println); | ||
|
||
} | ||
|
||
@Test | ||
public void testFilter() throws Exception { | ||
List<String> countriesStartingWithC = countries.filter(c -> c.startsWith("C")).toList(); | ||
Assert.assertEquals(countriesStartingWithC, Arrays.asList("Canada", "China")); | ||
} | ||
|
||
@Test | ||
public void testForEach() throws Exception { | ||
countries.forEach(System.out::println); | ||
} | ||
|
||
@Test | ||
public void testReduce() throws Exception { | ||
String concateWithSpace = countries.filter(c -> c.contains("n")).reduce("", (value1, value2) -> value1 + " " + value2); | ||
Assert.assertEquals(" Canada United Kingdom India China", concateWithSpace); | ||
} | ||
|
||
|
||
@Test | ||
public void testAnyMatch() throws Exception { | ||
boolean noAus = countries.anyMatch(c -> c.equals("Australia")); | ||
Assert.assertFalse(noAus); | ||
} | ||
|
||
Integer result1 = Flow.of(tests).reduce(0, (Integer val, String test) -> val+1); | ||
System.out.println("=====>" + result1); | ||
String result2 = Flow.of(tests).firstMatch(t -> t.endsWith("soft1")); | ||
System.out.println(result2); | ||
@Test | ||
public void testFirstMatch() throws Exception { | ||
String canada = countries.firstMatch(c -> c.startsWith("C")); | ||
Assert.assertEquals(canada, "Canada"); | ||
} | ||
|
||
} | ||
} |