Skip to content

Commit

Permalink
tests and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Dharmesh Gohil committed Jul 29, 2017
1 parent 209a0dc commit 2e40996
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 11 deletions.
58 changes: 56 additions & 2 deletions README.md
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 .... :)
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.21</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
8 changes: 8 additions & 0 deletions src/main/java/com/vistrav/flow/Flow.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ public T firstMatch(Predicate<? super T> predicate) {
return null;
}

public <R> Flow<R> map(final Function<? super T, ? extends R> mapper) {
List<R> anotherResult = new ArrayList<>();
while (iterator.hasNext()) {
anotherResult.add(mapper.apply(iterator.next()));
}
return Flow.<R>of(anotherResult);
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/vistrav/flow/Function.java
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);
}
66 changes: 57 additions & 9 deletions src/test/java/com/vistrav/flow/FlowTest.java
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");
}

}
}

0 comments on commit 2e40996

Please sign in to comment.