From 70680ea79a8bc0ed063a922b5e3d804efb116dc4 Mon Sep 17 00:00:00 2001 From: lor6 Date: Thu, 18 May 2017 21:44:35 +0300 Subject: [PATCH] vavr refactor (#1871) * vavr refactor * small fix --- javaslang/README.md | 5 -- .../exception/handling/JavaslangTry.java | 18 ---- vavr/README.md | 5 ++ {javaslang => vavr}/pom.xml | 24 +++--- .../main/java/com/baeldung/vavr}/Person.java | 2 +- .../com/baeldung/vavr}/PersonValidator.java | 8 +- .../exception/handling/JavaTryCatch.java | 8 +- .../vavr/exception/handling/VavrTry.java | 18 ++++ .../handling/client/ClientException.java | 2 +- .../exception/handling/client/HttpClient.java | 2 +- .../exception/handling/client/Response.java | 2 +- .../vavr}/PatternMatchingUnitTest.java | 36 ++++---- .../PropertyBasedLongRunningUnitTest.java | 14 +-- .../java/com/baeldung/vavr/VavrUnitTest.java | 86 ++++++++++--------- .../exception/handling/VavrTryUnitTest.java | 40 ++++----- 15 files changed, 137 insertions(+), 133 deletions(-) delete mode 100644 javaslang/README.md delete mode 100644 javaslang/src/main/java/com/baeldung/javaslang/exception/handling/JavaslangTry.java create mode 100644 vavr/README.md rename {javaslang => vavr}/pom.xml (59%) rename {javaslang/src/main/java/com/baeldung/javaslang => vavr/src/main/java/com/baeldung/vavr}/Person.java (94%) rename {javaslang/src/main/java/com/baeldung/javaslang => vavr/src/main/java/com/baeldung/vavr}/PersonValidator.java (76%) rename {javaslang/src/main/java/com/baeldung/javaslang => vavr/src/main/java/com/baeldung/vavr}/exception/handling/JavaTryCatch.java (56%) create mode 100644 vavr/src/main/java/com/baeldung/vavr/exception/handling/VavrTry.java rename {javaslang/src/main/java/com/baeldung/javaslang => vavr/src/main/java/com/baeldung/vavr}/exception/handling/client/ClientException.java (68%) rename {javaslang/src/main/java/com/baeldung/javaslang => vavr/src/main/java/com/baeldung/vavr}/exception/handling/client/HttpClient.java (57%) rename {javaslang/src/main/java/com/baeldung/javaslang => vavr/src/main/java/com/baeldung/vavr}/exception/handling/client/Response.java (66%) rename {javaslang/src/test/java/com/baeldung/javaslang => vavr/src/test/java/com/baeldung/vavr}/PatternMatchingUnitTest.java (61%) rename {javaslang/src/test/java/com/baeldung/javaslang => vavr/src/test/java/com/baeldung/vavr}/PropertyBasedLongRunningUnitTest.java (89%) rename javaslang/src/test/java/com/baeldung/javaslang/JavaSlangUnitTest.java => vavr/src/test/java/com/baeldung/vavr/VavrUnitTest.java (81%) rename javaslang/src/test/java/com/baeldung/javaslang/exception/handling/JavaslangTryUnitTest.java => vavr/src/test/java/com/baeldung/vavr/exception/handling/VavrTryUnitTest.java (71%) diff --git a/javaslang/README.md b/javaslang/README.md deleted file mode 100644 index fe413fc275..0000000000 --- a/javaslang/README.md +++ /dev/null @@ -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) diff --git a/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/JavaslangTry.java b/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/JavaslangTry.java deleted file mode 100644 index 6945f3bc0b..0000000000 --- a/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/JavaslangTry.java +++ /dev/null @@ -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 getResponse() { - return Try.of(httpClient::call); - } -} diff --git a/vavr/README.md b/vavr/README.md new file mode 100644 index 0000000000..e79e3269d5 --- /dev/null +++ b/vavr/README.md @@ -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) diff --git a/javaslang/pom.xml b/vavr/pom.xml similarity index 59% rename from javaslang/pom.xml rename to vavr/pom.xml index 68969ef6c9..5356417b3e 100644 --- a/javaslang/pom.xml +++ b/vavr/pom.xml @@ -2,11 +2,11 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.baeldung - javaslang + vavr 1.0 - javaslang - - + vavr + + com.baeldung parent-modules 1.0.0-SNAPSHOT @@ -14,19 +14,21 @@ - io.javaslang - javaslang - 2.1.0-alpha + io.vavr + vavr-test + ${vavr.version} + - io.javaslang - javaslang-test - ${javaslang.test.version} + junit + junit + ${junit.version} - 2.0.5 + 0.9.0 + 4.12 \ No newline at end of file diff --git a/javaslang/src/main/java/com/baeldung/javaslang/Person.java b/vavr/src/main/java/com/baeldung/vavr/Person.java similarity index 94% rename from javaslang/src/main/java/com/baeldung/javaslang/Person.java rename to vavr/src/main/java/com/baeldung/vavr/Person.java index 38a6603f98..0c6c427c73 100644 --- a/javaslang/src/main/java/com/baeldung/javaslang/Person.java +++ b/vavr/src/main/java/com/baeldung/vavr/Person.java @@ -1,4 +1,4 @@ -package com.baeldung.javaslang; +package com.baeldung.vavr; public class Person { private String name; diff --git a/javaslang/src/main/java/com/baeldung/javaslang/PersonValidator.java b/vavr/src/main/java/com/baeldung/vavr/PersonValidator.java similarity index 76% rename from javaslang/src/main/java/com/baeldung/javaslang/PersonValidator.java rename to vavr/src/main/java/com/baeldung/vavr/PersonValidator.java index fb30d6072c..efaa74c710 100644 --- a/javaslang/src/main/java/com/baeldung/javaslang/PersonValidator.java +++ b/vavr/src/main/java/com/baeldung/vavr/PersonValidator.java @@ -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, Person> validatePerson(String name, int age) { + public Validation, Person> validatePerson(String name, int age) { return Validation.combine(validateName(name), validateAge(age)).ap(Person::new); } diff --git a/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/JavaTryCatch.java b/vavr/src/main/java/com/baeldung/vavr/exception/handling/JavaTryCatch.java similarity index 56% rename from javaslang/src/main/java/com/baeldung/javaslang/exception/handling/JavaTryCatch.java rename to vavr/src/main/java/com/baeldung/vavr/exception/handling/JavaTryCatch.java index 2849d3424f..25fa206f9f 100644 --- a/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/JavaTryCatch.java +++ b/vavr/src/main/java/com/baeldung/vavr/exception/handling/JavaTryCatch.java @@ -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; diff --git a/vavr/src/main/java/com/baeldung/vavr/exception/handling/VavrTry.java b/vavr/src/main/java/com/baeldung/vavr/exception/handling/VavrTry.java new file mode 100644 index 0000000000..3a9399c4f0 --- /dev/null +++ b/vavr/src/main/java/com/baeldung/vavr/exception/handling/VavrTry.java @@ -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 getResponse() { + return Try.of(httpClient::call); + } +} diff --git a/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/client/ClientException.java b/vavr/src/main/java/com/baeldung/vavr/exception/handling/client/ClientException.java similarity index 68% rename from javaslang/src/main/java/com/baeldung/javaslang/exception/handling/client/ClientException.java rename to vavr/src/main/java/com/baeldung/vavr/exception/handling/client/ClientException.java index 7993e10117..e05a05bd2d 100644 --- a/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/client/ClientException.java +++ b/vavr/src/main/java/com/baeldung/vavr/exception/handling/client/ClientException.java @@ -1,4 +1,4 @@ -package com.baeldung.javaslang.exception.handling.client; +package com.baeldung.vavr.exception.handling.client; public class ClientException extends Exception { diff --git a/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/client/HttpClient.java b/vavr/src/main/java/com/baeldung/vavr/exception/handling/client/HttpClient.java similarity index 57% rename from javaslang/src/main/java/com/baeldung/javaslang/exception/handling/client/HttpClient.java rename to vavr/src/main/java/com/baeldung/vavr/exception/handling/client/HttpClient.java index f14e6c718f..5d0b98a4ca 100644 --- a/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/client/HttpClient.java +++ b/vavr/src/main/java/com/baeldung/vavr/exception/handling/client/HttpClient.java @@ -1,4 +1,4 @@ -package com.baeldung.javaslang.exception.handling.client; +package com.baeldung.vavr.exception.handling.client; public interface HttpClient { diff --git a/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/client/Response.java b/vavr/src/main/java/com/baeldung/vavr/exception/handling/client/Response.java similarity index 66% rename from javaslang/src/main/java/com/baeldung/javaslang/exception/handling/client/Response.java rename to vavr/src/main/java/com/baeldung/vavr/exception/handling/client/Response.java index fded319245..e9bdc1f80e 100644 --- a/javaslang/src/main/java/com/baeldung/javaslang/exception/handling/client/Response.java +++ b/vavr/src/main/java/com/baeldung/vavr/exception/handling/client/Response.java @@ -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; diff --git a/javaslang/src/test/java/com/baeldung/javaslang/PatternMatchingUnitTest.java b/vavr/src/test/java/com/baeldung/vavr/PatternMatchingUnitTest.java similarity index 61% rename from javaslang/src/test/java/com/baeldung/javaslang/PatternMatchingUnitTest.java rename to vavr/src/test/java/com/baeldung/vavr/PatternMatchingUnitTest.java index b9317bcb16..ef5afe0e29 100644 --- a/javaslang/src/test/java/com/baeldung/javaslang/PatternMatchingUnitTest.java +++ b/vavr/src/test/java/com/baeldung/vavr/PatternMatchingUnitTest.java @@ -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)); }))); } diff --git a/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedLongRunningUnitTest.java b/vavr/src/test/java/com/baeldung/vavr/PropertyBasedLongRunningUnitTest.java similarity index 89% rename from javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedLongRunningUnitTest.java rename to vavr/src/test/java/com/baeldung/vavr/PropertyBasedLongRunningUnitTest.java index 2bbc92563e..8409dbfa60 100644 --- a/javaslang/src/test/java/com/baeldung/javaslang/PropertyBasedLongRunningUnitTest.java +++ b/vavr/src/test/java/com/baeldung/vavr/PropertyBasedLongRunningUnitTest.java @@ -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 { diff --git a/javaslang/src/test/java/com/baeldung/javaslang/JavaSlangUnitTest.java b/vavr/src/test/java/com/baeldung/vavr/VavrUnitTest.java similarity index 81% rename from javaslang/src/test/java/com/baeldung/javaslang/JavaSlangUnitTest.java rename to vavr/src/test/java/com/baeldung/vavr/VavrUnitTest.java index 8263dc111c..5ddd344c5c 100644 --- a/javaslang/src/test/java/com/baeldung/javaslang/JavaSlangUnitTest.java +++ b/vavr/src/test/java/com/baeldung/vavr/VavrUnitTest.java @@ -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 sortedList = List.of(3, 2, 1).sorted(); + List sortedList = List.of(3, 2, 1) + .sorted(); } /* @@ -57,9 +62,9 @@ public class JavaSlangUnitTest { @Test public void givenTuple_whenMapsComponentWise_thenCorrect() { Tuple2 java8 = Tuple.of("Java", 8); - Tuple2 mapOfJava8 = java8.map(s -> s + "slang", i -> i / 2); + Tuple2 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 java8 = Tuple.of("Java", 8); - Tuple2 mapOfJava8 = java8.map((s, i) -> Tuple.of(s + "slang", i / 2)); + Tuple2 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 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 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 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 getClazzName = () -> this.getClass().getName(); + Function0 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 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 result = Try.of(() -> 1 / 0); -// result.getOrElseThrow(ArithmeticException::new); -// } + // @Test(expected = ArithmeticException.class) + // public void givenBadCode_whenTryHandles_thenCorrect3() { + // Try 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, Person> valid = personValidator.validatePerson("John Doe", 30); - Validation, Person> invalid = personValidator.validatePerson("John? Doe!4", -1); + Validation, Person> valid = personValidator.validatePerson("John Doe", 30); + Validation, 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 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); } - - } diff --git a/javaslang/src/test/java/com/baeldung/javaslang/exception/handling/JavaslangTryUnitTest.java b/vavr/src/test/java/com/baeldung/vavr/exception/handling/VavrTryUnitTest.java similarity index 71% rename from javaslang/src/test/java/com/baeldung/javaslang/exception/handling/JavaslangTryUnitTest.java rename to vavr/src/test/java/com/baeldung/vavr/exception/handling/VavrTryUnitTest.java index e4ff91ea9e..9c3e5a9cae 100644 --- a/javaslang/src/test/java/com/baeldung/javaslang/exception/handling/JavaslangTryUnitTest.java +++ b/vavr/src/test/java/com/baeldung/vavr/exception/handling/VavrTryUnitTest.java @@ -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 = new JavaslangTry(httpClient).getResponse(); + Try response = new VavrTry(httpClient).getResponse(); Integer chainedResult = response .map(this::actionThatTakesResponse) .getOrElse(defaultChainedResult); @@ -49,7 +47,7 @@ public class JavaslangTryUnitTest { }; //when - Try response = new JavaslangTry(httpClient).getResponse(); + Try response = new VavrTry(httpClient).getResponse(); Integer chainedResult = response .map(this::actionThatTakesResponse) .getOrElse(defaultChainedResult); @@ -71,9 +69,9 @@ public class JavaslangTryUnitTest { }; //when - Try recovered = new JavaslangTry(httpClient).getResponse() + Try 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 recovered = new JavaslangTry(httpClient).getResponse() + Try 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