From 2e40996be648f6ed867aa02824a7ec6634542996 Mon Sep 17 00:00:00 2001 From: Dharmesh Gohil Date: Fri, 28 Jul 2017 17:25:20 -0700 Subject: [PATCH] tests and readme --- README.md | 58 ++++++++++++++++- pom.xml | 8 +++ src/main/java/com/vistrav/flow/Flow.java | 8 +++ src/main/java/com/vistrav/flow/Function.java | 7 +++ src/test/java/com/vistrav/flow/FlowTest.java | 66 +++++++++++++++++--- 5 files changed, 136 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/vistrav/flow/Function.java diff --git a/README.md b/README.md index b76ef4b..978d58a 100644 --- a/README.md +++ b/README.md @@ -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 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 .... :) \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8af2d78..15d5192 100644 --- a/pom.xml +++ b/pom.xml @@ -20,5 +20,13 @@ + + + org.testng + testng + 6.8.21 + test + + \ No newline at end of file diff --git a/src/main/java/com/vistrav/flow/Flow.java b/src/main/java/com/vistrav/flow/Flow.java index 4afefe0..97001ee 100644 --- a/src/main/java/com/vistrav/flow/Flow.java +++ b/src/main/java/com/vistrav/flow/Flow.java @@ -95,4 +95,12 @@ public T firstMatch(Predicate predicate) { return null; } + public Flow map(final Function mapper) { + List anotherResult = new ArrayList<>(); + while (iterator.hasNext()) { + anotherResult.add(mapper.apply(iterator.next())); + } + return Flow.of(anotherResult); + } + } diff --git a/src/main/java/com/vistrav/flow/Function.java b/src/main/java/com/vistrav/flow/Function.java new file mode 100644 index 0000000..fb6d86d --- /dev/null +++ b/src/main/java/com/vistrav/flow/Function.java @@ -0,0 +1,7 @@ +package com.vistrav.flow; + +@FunctionalInterface +public interface Function { + + R apply(T value1); +} \ No newline at end of file diff --git a/src/test/java/com/vistrav/flow/FlowTest.java b/src/test/java/com/vistrav/flow/FlowTest.java index 0a752d4..138ad11 100644 --- a/src/test/java/com/vistrav/flow/FlowTest.java +++ b/src/test/java/com/vistrav/flow/FlowTest.java @@ -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 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 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"); } -} +} \ No newline at end of file