vavr refactor (#1871)

* vavr refactor

* small fix
This commit is contained in:
lor6 2017-05-18 21:44:35 +03:00 committed by Grzegorz Piwowarek
parent 7728d58e5b
commit 70680ea79a
15 changed files with 137 additions and 133 deletions

View File

@ -1,5 +0,0 @@
### Relevant Articles:
- [Introduction to Javaslang](http://www.baeldung.com/javaslang)
- [Guide to Try in Javaslang](http://www.baeldung.com/javaslang-try)
- [Guide to Pattern Matching in Javaslang](http://www.baeldung.com/javaslang-pattern-matching)
- [Property Testing Example With Javaslang](http://www.baeldung.com/javaslang-property-testing)

View File

@ -1,18 +0,0 @@
package com.baeldung.javaslang.exception.handling;
import com.baeldung.javaslang.exception.handling.client.HttpClient;
import com.baeldung.javaslang.exception.handling.client.Response;
import javaslang.control.Try;
public class JavaslangTry {
private final HttpClient httpClient;
public JavaslangTry(HttpClient httpClient) {
this.httpClient = httpClient;
}
public Try<Response> getResponse() {
return Try.of(httpClient::call);
}
}

5
vavr/README.md Normal file
View File

@ -0,0 +1,5 @@
### Relevant Articles:
- [Introduction to Vavr](http://www.baeldung.com/javaslang)
- [Guide to Try in Vavr](http://www.baeldung.com/javaslang-try)
- [Guide to Pattern Matching in Vavr](http://www.baeldung.com/javaslang-pattern-matching)
- [Property Testing Example With Vavr](http://www.baeldung.com/javaslang-property-testing)

View File

@ -2,9 +2,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>javaslang</artifactId>
<artifactId>vavr</artifactId>
<version>1.0</version>
<name>javaslang</name>
<name>vavr</name>
<parent>
<groupId>com.baeldung</groupId>
@ -14,19 +14,21 @@
<dependencies>
<dependency>
<groupId>io.javaslang</groupId>
<artifactId>javaslang</artifactId>
<version>2.1.0-alpha</version>
<groupId>io.vavr</groupId>
<artifactId>vavr-test</artifactId>
<version>${vavr.version}</version>
</dependency>
<dependency>
<groupId>io.javaslang</groupId>
<artifactId>javaslang-test</artifactId>
<version>${javaslang.test.version}</version>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
<properties>
<javaslang.test.version>2.0.5</javaslang.test.version>
<vavr.version>0.9.0</vavr.version>
<junit.version>4.12</junit.version>
</properties>
</project>

View File

@ -1,4 +1,4 @@
package com.baeldung.javaslang;
package com.baeldung.vavr;
public class Person {
private String name;

View File

@ -1,13 +1,13 @@
package com.baeldung.javaslang;
package com.baeldung.vavr;
import javaslang.collection.List;
import javaslang.control.Validation;
import io.vavr.collection.Seq;
import io.vavr.control.Validation;
class PersonValidator {
String NAME_ERR = "Invalid characters in name: ";
String AGE_ERR = "Age must be at least 0";
public Validation<List<String>, Person> validatePerson(String name, int age) {
public Validation<Seq<String>, Person> validatePerson(String name, int age) {
return Validation.combine(validateName(name), validateAge(age)).ap(Person::new);
}

View File

@ -1,8 +1,8 @@
package com.baeldung.javaslang.exception.handling;
package com.baeldung.vavr.exception.handling;
import com.baeldung.javaslang.exception.handling.client.ClientException;
import com.baeldung.javaslang.exception.handling.client.HttpClient;
import com.baeldung.javaslang.exception.handling.client.Response;
import com.baeldung.vavr.exception.handling.client.ClientException;
import com.baeldung.vavr.exception.handling.client.HttpClient;
import com.baeldung.vavr.exception.handling.client.Response;
public class JavaTryCatch {
private HttpClient httpClient;

View File

@ -0,0 +1,18 @@
package com.baeldung.vavr.exception.handling;
import com.baeldung.vavr.exception.handling.client.HttpClient;
import com.baeldung.vavr.exception.handling.client.Response;
import io.vavr.control.Try;
public class VavrTry {
private final HttpClient httpClient;
public VavrTry(HttpClient httpClient) {
this.httpClient = httpClient;
}
public Try<Response> getResponse() {
return Try.of(httpClient::call);
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.javaslang.exception.handling.client;
package com.baeldung.vavr.exception.handling.client;
public class ClientException extends Exception {

View File

@ -1,4 +1,4 @@
package com.baeldung.javaslang.exception.handling.client;
package com.baeldung.vavr.exception.handling.client;
public interface HttpClient {

View File

@ -1,4 +1,4 @@
package com.baeldung.javaslang.exception.handling.client;
package com.baeldung.vavr.exception.handling.client;
public class Response {
public final String id;

View File

@ -1,19 +1,17 @@
package com.baeldung.javaslang;
package com.baeldung.vavr;
import static javaslang.API.$;
import static javaslang.API.Case;
import static javaslang.API.Match;
import static javaslang.API.run;
import static javaslang.Predicates.*;
import static io.vavr.API.$;
import static io.vavr.API.Case;
import static io.vavr.API.Match;
import static io.vavr.API.run;
import static io.vavr.Predicates.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Optional;
import org.junit.Test;
import javaslang.MatchError;
import javaslang.control.Option;
import io.vavr.MatchError;
import io.vavr.control.Option;
public class PatternMatchingUnitTest {
@Test
@ -41,63 +39,63 @@ public class PatternMatchingUnitTest {
@Test
public void whenMatchWorksWithPredicate_thenCorrect() {
int i = 3;
String s = Match(i).of(Case(is(1), "one"), Case(is(2), "two"), Case(is(3), "three"), Case($(), "?"));
String s = Match(i).of(Case($(is(1)), "one"), Case($(is(2)), "two"), Case($(is(3)), "three"), Case($(), "?"));
assertEquals("three", s);
}
@Test
public void givenInput_whenMatchesClass_thenCorrect() {
Object obj = 5;
String s = Match(obj).of(Case(instanceOf(String.class), "string matched"), Case($(), "not string"));
String s = Match(obj).of(Case($(instanceOf(String.class)), "string matched"), Case($(), "not string"));
assertEquals("not string", s);
}
@Test
public void givenInput_whenMatchesNull_thenCorrect() {
Object obj = 5;
String s = Match(obj).of(Case(isNull(), "no value"), Case(isNotNull(), "value found"));
String s = Match(obj).of(Case($(isNull()), "no value"), Case($(isNotNull()), "value found"));
assertEquals("value found", s);
}
@Test
public void givenInput_whenContainsWorks_thenCorrect() {
int i = 5;
String s = Match(i).of(Case(isIn(2, 4, 6, 8), "Even Single Digit"), Case(isIn(1, 3, 5, 7, 9), "Odd Single Digit"), Case($(), "Out of range"));
String s = Match(i).of(Case($(isIn(2, 4, 6, 8)), "Even Single Digit"), Case($(isIn(1, 3, 5, 7, 9)), "Odd Single Digit"), Case($(), "Out of range"));
assertEquals("Odd Single Digit", s);
}
@Test
public void givenInput_whenMatchAllWorks_thenCorrect() {
Integer i = null;
String s = Match(i).of(Case(allOf(isNotNull(), isIn(1, 2, 3, null)), "Number found"), Case($(), "Not found"));
String s = Match(i).of(Case($(allOf(isNotNull(), isIn(1, 2, 3, null))), "Number found"), Case($(), "Not found"));
assertEquals("Not found", s);
}
@Test
public void givenInput_whenMatchesAnyOfWorks_thenCorrect() {
Integer year = 1990;
String s = Match(year).of(Case(anyOf(isIn(1990, 1991, 1992), is(1986)), "Age match"), Case($(), "No age match"));
String s = Match(year).of(Case($(anyOf(isIn(1990, 1991, 1992), is(1986))), "Age match"), Case($(), "No age match"));
assertEquals("Age match", s);
}
@Test
public void givenInput_whenMatchesNoneOfWorks_thenCorrect() {
Integer year = 1990;
String s = Match(year).of(Case(noneOf(isIn(1990, 1991, 1992), is(1986)), "Age match"), Case($(), "No age match"));
String s = Match(year).of(Case($(noneOf(isIn(1990, 1991, 1992), is(1986))), "Age match"), Case($(), "No age match"));
assertEquals("No age match", s);
}
@Test
public void whenMatchWorksWithPredicate_thenCorrect2() {
int i = 5;
String s = Match(i).of(Case(isIn(2, 4, 6, 8), "Even Single Digit"), Case(isIn(1, 3, 5, 7, 9), "Odd Single Digit"), Case($(), "Out of range"));
String s = Match(i).of(Case($(isIn(2, 4, 6, 8)), "Even Single Digit"), Case($(isIn(1, 3, 5, 7, 9)), "Odd Single Digit"), Case($(), "Out of range"));
assertEquals("Odd Single Digit", s);
}
@Test
public void whenMatchCreatesSideEffects_thenCorrect() {
int i = 4;
Match(i).of(Case(isIn(2, 4, 6, 8), o -> run(this::displayEven)), Case(isIn(1, 3, 5, 7, 9), o -> run(this::displayOdd)), Case($(), o -> run(() -> {
Match(i).of(Case($(isIn(2, 4, 6, 8)), o -> run(this::displayEven)), Case($(isIn(1, 3, 5, 7, 9)), o -> run(this::displayOdd)), Case($(), o -> run(() -> {
throw new IllegalArgumentException(String.valueOf(i));
})));
}

View File

@ -1,15 +1,15 @@
package com.baeldung.javaslang;
package com.baeldung.vavr;
import javaslang.CheckedFunction1;
import javaslang.collection.Stream;
import javaslang.test.Arbitrary;
import javaslang.test.CheckResult;
import javaslang.test.Property;
import io.vavr.CheckedFunction1;
import io.vavr.collection.Stream;
import io.vavr.test.Arbitrary;
import io.vavr.test.CheckResult;
import io.vavr.test.Property;
import org.junit.Test;
import java.util.function.Predicate;
import static javaslang.API.*;
import static io.vavr.API.*;
public class PropertyBasedLongRunningUnitTest {

View File

@ -1,30 +1,35 @@
package com.baeldung.javaslang;
package com.baeldung.vavr;
import javaslang.Function0;
import javaslang.Function1;
import javaslang.Function2;
import javaslang.Function5;
import javaslang.Lazy;
import javaslang.*;
import javaslang.collection.List;
import javaslang.control.Option;
import javaslang.control.Try;
import javaslang.control.Validation;
import io.vavr.Function0;
import io.vavr.Function1;
import io.vavr.Function2;
import io.vavr.Function5;
import io.vavr.Lazy;
import io.vavr.*;
import io.vavr.collection.List;
import io.vavr.collection.Seq;
import io.vavr.control.Option;
import io.vavr.control.Try;
import io.vavr.control.Validation;
import org.junit.Test;
import com.baeldung.vavr.Person;
import com.baeldung.vavr.PersonValidator;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.IntStream;
import static javaslang.API.*;
import static io.vavr.API.*;
import static org.junit.Assert.*;
public class JavaSlangUnitTest {
public class VavrUnitTest {
@Test
public void givenList_whenSorts_thenCorrect() {
List<Integer> sortedList = List.of(3, 2, 1).sorted();
List<Integer> sortedList = List.of(3, 2, 1)
.sorted();
}
/*
@ -57,9 +62,9 @@ public class JavaSlangUnitTest {
@Test
public void givenTuple_whenMapsComponentWise_thenCorrect() {
Tuple2<String, Integer> java8 = Tuple.of("Java", 8);
Tuple2<String, Integer> mapOfJava8 = java8.map(s -> s + "slang", i -> i / 2);
Tuple2<String, Integer> mapOfJava8 = java8.map(s -> s + "Vavr", i -> i / 2);
int num = mapOfJava8._2();
assertEquals("Javaslang", mapOfJava8._1);
assertEquals("JavaVavr", mapOfJava8._1);
assertEquals(4, num);
@ -69,9 +74,9 @@ public class JavaSlangUnitTest {
@Test
public void givenTuple_whenMapsWithOneMapper_thenCorrect() {
Tuple2<String, Integer> java8 = Tuple.of("Java", 8);
Tuple2<String, Integer> mapOfJava8 = java8.map((s, i) -> Tuple.of(s + "slang", i / 2));
Tuple2<String, Integer> mapOfJava8 = java8.map((s, i) -> Tuple.of(s + "Vavr", i / 2));
int num = mapOfJava8._2();
assertEquals("Javaslang", mapOfJava8._1);
assertEquals("JavaVavr", mapOfJava8._1);
assertEquals(4, num);
}
@ -80,8 +85,8 @@ public class JavaSlangUnitTest {
@Test
public void givenTuple_whenTransforms_thenCorrect() {
Tuple2<String, Integer> java8 = Tuple.of("Java", 8);
String transformed = java8.apply((s, i) -> s + "slang " + i / 2);
assertEquals("Javaslang 4", transformed);
String transformed = java8.apply((s, i) -> s + "Vavr " + i / 2);
assertEquals("JavaVavr 4", transformed);
}
/*
@ -102,14 +107,14 @@ public class JavaSlangUnitTest {
}
@Test
public void givenJavaslangFunction_whenWorks_thenCorrect() {
public void givenVavrFunction_whenWorks_thenCorrect() {
Function1<Integer, Integer> square = (num) -> num * num;
Integer result = square.apply(2);
assertEquals(Integer.valueOf(4), result);
}
@Test
public void givenJavaslangBiFunction_whenWorks_thenCorrect() {
public void givenVavrBiFunction_whenWorks_thenCorrect() {
Function2<Integer, Integer, Integer> sum = (num1, num2) -> num1 + num2;
Integer result = sum.apply(5, 7);
assertEquals(Integer.valueOf(12), result);
@ -117,9 +122,10 @@ public class JavaSlangUnitTest {
@Test
public void whenCreatesFunction_thenCorrect0() {
Function0<String> getClazzName = () -> this.getClass().getName();
Function0<String> getClazzName = () -> this.getClass()
.getName();
String clazzName = getClazzName.apply();
assertEquals("com.baeldung.javaslang.JavaSlangUnitTest", clazzName);
assertEquals("com.baeldung.vavr.VavrUnitTest", clazzName);
}
@Test
@ -132,8 +138,8 @@ public class JavaSlangUnitTest {
@Test
public void whenCreatesFunction_thenCorrect5() {
Function5<String, String, String, String, String, String> concat = (a, b, c, d, e) -> a + b + c + d + e;
String finalString = concat.apply("Hello ", "world", "! ", "Learn ", "Javaslang");
assertEquals("Hello world! Learn Javaslang", finalString);
String finalString = concat.apply("Hello ", "world", "! ", "Learn ", "Vavr");
assertEquals("Hello world! Learn Vavr", finalString);
}
@Test
@ -147,7 +153,6 @@ public class JavaSlangUnitTest {
return a + b;
}
/*
* Values
*/
@ -207,11 +212,11 @@ public class JavaSlangUnitTest {
assertEquals(-1, errorSentinel);
}
// @Test(expected = ArithmeticException.class)
// public void givenBadCode_whenTryHandles_thenCorrect3() {
// Try<Integer> result = Try.of(() -> 1 / 0);
// result.getOrElseThrow(ArithmeticException::new);
// }
// @Test(expected = ArithmeticException.class)
// public void givenBadCode_whenTryHandles_thenCorrect3() {
// Try<Integer> result = Try.of(() -> 1 / 0);
// result.getOrElseThrow(ArithmeticException::new);
// }
// lazy
@Test
@ -230,8 +235,8 @@ public class JavaSlangUnitTest {
@Test
public void whenValidationWorks_thenCorrect() {
PersonValidator personValidator = new PersonValidator();
Validation<List<String>, Person> valid = personValidator.validatePerson("John Doe", 30);
Validation<List<String>, Person> invalid = personValidator.validatePerson("John? Doe!4", -1);
Validation<Seq<String>, Person> valid = personValidator.validatePerson("John Doe", 30);
Validation<Seq<String>, Person> invalid = personValidator.validatePerson("John? Doe!4", -1);
assertEquals("Valid(Person [name=John Doe, age=30])", valid.toString());
assertEquals("Invalid(List(Invalid characters in name: ?!4, Age must be at least 0))", invalid.toString());
@ -251,12 +256,13 @@ public class JavaSlangUnitTest {
@Test
public void whenSumsJava8List_thenCorrect() {
// Arrays.asList(1, 2, 3).stream().reduce((i, j) -> i + j);
int sum = IntStream.of(1, 2, 3).sum();
int sum = IntStream.of(1, 2, 3)
.sum();
assertEquals(6, sum);
}
@Test
public void whenCreatesJavaslangList_thenCorrect() {
public void whenCreatesVavrList_thenCorrect() {
List<Integer> intList = List.of(1, 2, 3);
assertEquals(3, intList.length());
assertEquals(new Integer(1), intList.get(0));
@ -265,8 +271,10 @@ public class JavaSlangUnitTest {
}
@Test
public void whenSumsJavaslangList_thenCorrect() {
int sum = List.of(1, 2, 3).sum().intValue();
public void whenSumsVavrList_thenCorrect() {
int sum = List.of(1, 2, 3)
.sum()
.intValue();
assertEquals(6, sum);
}
@ -325,6 +333,4 @@ public class JavaSlangUnitTest {
assertEquals("two", output);
}
}

View File

@ -1,22 +1,20 @@
package com.baeldung.javaslang.exception.handling;
package com.baeldung.vavr.exception.handling;
import com.baeldung.javaslang.exception.handling.client.ClientException;
import com.baeldung.javaslang.exception.handling.client.HttpClient;
import com.baeldung.javaslang.exception.handling.client.Response;
import javaslang.collection.Stream;
import javaslang.control.Option;
import javaslang.control.Try;
import com.baeldung.vavr.exception.handling.client.ClientException;
import com.baeldung.vavr.exception.handling.client.HttpClient;
import com.baeldung.vavr.exception.handling.client.Response;
import com.baeldung.vavr.exception.handling.VavrTry;
import io.vavr.collection.Stream;
import io.vavr.control.Option;
import io.vavr.control.Try;
import org.junit.Test;
import java.util.function.Function;
import java.util.stream.Collectors;
import static javaslang.API.Case;
import static javaslang.API.Match;
import static javaslang.Predicates.instanceOf;
import static io.vavr.API.*;
import static io.vavr.Predicates.instanceOf;
import static org.junit.Assert.*;
public class JavaslangTryUnitTest {
public class VavrTryUnitTest {
@Test
public void givenHttpClient_whenMakeACall_shouldReturnSuccess() {
@ -26,7 +24,7 @@ public class JavaslangTryUnitTest {
HttpClient httpClient = () -> new Response(id);
//when
Try<Response> response = new JavaslangTry(httpClient).getResponse();
Try<Response> response = new VavrTry(httpClient).getResponse();
Integer chainedResult = response
.map(this::actionThatTakesResponse)
.getOrElse(defaultChainedResult);
@ -49,7 +47,7 @@ public class JavaslangTryUnitTest {
};
//when
Try<Response> response = new JavaslangTry(httpClient).getResponse();
Try<Response> response = new VavrTry(httpClient).getResponse();
Integer chainedResult = response
.map(this::actionThatTakesResponse)
.getOrElse(defaultChainedResult);
@ -71,9 +69,9 @@ public class JavaslangTryUnitTest {
};
//when
Try<Response> recovered = new JavaslangTry(httpClient).getResponse()
Try<Response> recovered = new VavrTry(httpClient).getResponse()
.recover(r -> Match(r).of(
Case(instanceOf(ClientException.class), defaultResponse)
Case($(instanceOf(ClientException.class)), defaultResponse)
));
//then
@ -93,10 +91,10 @@ public class JavaslangTryUnitTest {
};
//when
Try<Response> recovered = new JavaslangTry(httpClient).getResponse()
Try<Response> recovered = new VavrTry(httpClient).getResponse()
.recover(r -> Match(r).of(
Case(instanceOf(ClientException.class), defaultResponse),
Case(instanceOf(IllegalArgumentException.class), defaultResponse)
Case($(instanceOf(ClientException.class)), defaultResponse),
Case($(instanceOf(IllegalArgumentException.class)), defaultResponse)
));
//then