parent
7728d58e5b
commit
70680ea79a
|
@ -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)
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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)
|
|
@ -2,9 +2,9 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>javaslang</artifactId>
|
<artifactId>vavr</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
<name>javaslang</name>
|
<name>vavr</name>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
@ -14,19 +14,21 @@
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.javaslang</groupId>
|
<groupId>io.vavr</groupId>
|
||||||
<artifactId>javaslang</artifactId>
|
<artifactId>vavr-test</artifactId>
|
||||||
<version>2.1.0-alpha</version>
|
<version>${vavr.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.javaslang</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>javaslang-test</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>${javaslang.test.version}</version>
|
<version>${junit.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<javaslang.test.version>2.0.5</javaslang.test.version>
|
<vavr.version>0.9.0</vavr.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.javaslang;
|
package com.baeldung.vavr;
|
||||||
|
|
||||||
public class Person {
|
public class Person {
|
||||||
private String name;
|
private String name;
|
|
@ -1,13 +1,13 @@
|
||||||
package com.baeldung.javaslang;
|
package com.baeldung.vavr;
|
||||||
|
|
||||||
import javaslang.collection.List;
|
import io.vavr.collection.Seq;
|
||||||
import javaslang.control.Validation;
|
import io.vavr.control.Validation;
|
||||||
|
|
||||||
class PersonValidator {
|
class PersonValidator {
|
||||||
String NAME_ERR = "Invalid characters in name: ";
|
String NAME_ERR = "Invalid characters in name: ";
|
||||||
String AGE_ERR = "Age must be at least 0";
|
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);
|
return Validation.combine(validateName(name), validateAge(age)).ap(Person::new);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.vavr.exception.handling.client.ClientException;
|
||||||
import com.baeldung.javaslang.exception.handling.client.HttpClient;
|
import com.baeldung.vavr.exception.handling.client.HttpClient;
|
||||||
import com.baeldung.javaslang.exception.handling.client.Response;
|
import com.baeldung.vavr.exception.handling.client.Response;
|
||||||
|
|
||||||
public class JavaTryCatch {
|
public class JavaTryCatch {
|
||||||
private HttpClient httpClient;
|
private HttpClient httpClient;
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.javaslang.exception.handling.client;
|
package com.baeldung.vavr.exception.handling.client;
|
||||||
|
|
||||||
|
|
||||||
public class ClientException extends Exception {
|
public class ClientException extends Exception {
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.javaslang.exception.handling.client;
|
package com.baeldung.vavr.exception.handling.client;
|
||||||
|
|
||||||
|
|
||||||
public interface HttpClient {
|
public interface HttpClient {
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.javaslang.exception.handling.client;
|
package com.baeldung.vavr.exception.handling.client;
|
||||||
|
|
||||||
public class Response {
|
public class Response {
|
||||||
public final String id;
|
public final String id;
|
|
@ -1,19 +1,17 @@
|
||||||
package com.baeldung.javaslang;
|
package com.baeldung.vavr;
|
||||||
|
|
||||||
import static javaslang.API.$;
|
import static io.vavr.API.$;
|
||||||
import static javaslang.API.Case;
|
import static io.vavr.API.Case;
|
||||||
import static javaslang.API.Match;
|
import static io.vavr.API.Match;
|
||||||
import static javaslang.API.run;
|
import static io.vavr.API.run;
|
||||||
import static javaslang.Predicates.*;
|
import static io.vavr.Predicates.*;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import javaslang.MatchError;
|
import io.vavr.MatchError;
|
||||||
import javaslang.control.Option;
|
import io.vavr.control.Option;
|
||||||
|
|
||||||
public class PatternMatchingUnitTest {
|
public class PatternMatchingUnitTest {
|
||||||
@Test
|
@Test
|
||||||
|
@ -41,63 +39,63 @@ public class PatternMatchingUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenMatchWorksWithPredicate_thenCorrect() {
|
public void whenMatchWorksWithPredicate_thenCorrect() {
|
||||||
int i = 3;
|
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);
|
assertEquals("three", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInput_whenMatchesClass_thenCorrect() {
|
public void givenInput_whenMatchesClass_thenCorrect() {
|
||||||
Object obj = 5;
|
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);
|
assertEquals("not string", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInput_whenMatchesNull_thenCorrect() {
|
public void givenInput_whenMatchesNull_thenCorrect() {
|
||||||
Object obj = 5;
|
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);
|
assertEquals("value found", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInput_whenContainsWorks_thenCorrect() {
|
public void givenInput_whenContainsWorks_thenCorrect() {
|
||||||
int i = 5;
|
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);
|
assertEquals("Odd Single Digit", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInput_whenMatchAllWorks_thenCorrect() {
|
public void givenInput_whenMatchAllWorks_thenCorrect() {
|
||||||
Integer i = null;
|
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);
|
assertEquals("Not found", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInput_whenMatchesAnyOfWorks_thenCorrect() {
|
public void givenInput_whenMatchesAnyOfWorks_thenCorrect() {
|
||||||
Integer year = 1990;
|
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);
|
assertEquals("Age match", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInput_whenMatchesNoneOfWorks_thenCorrect() {
|
public void givenInput_whenMatchesNoneOfWorks_thenCorrect() {
|
||||||
Integer year = 1990;
|
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);
|
assertEquals("No age match", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenMatchWorksWithPredicate_thenCorrect2() {
|
public void whenMatchWorksWithPredicate_thenCorrect2() {
|
||||||
int i = 5;
|
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);
|
assertEquals("Odd Single Digit", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenMatchCreatesSideEffects_thenCorrect() {
|
public void whenMatchCreatesSideEffects_thenCorrect() {
|
||||||
int i = 4;
|
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));
|
throw new IllegalArgumentException(String.valueOf(i));
|
||||||
})));
|
})));
|
||||||
}
|
}
|
|
@ -1,15 +1,15 @@
|
||||||
package com.baeldung.javaslang;
|
package com.baeldung.vavr;
|
||||||
|
|
||||||
import javaslang.CheckedFunction1;
|
import io.vavr.CheckedFunction1;
|
||||||
import javaslang.collection.Stream;
|
import io.vavr.collection.Stream;
|
||||||
import javaslang.test.Arbitrary;
|
import io.vavr.test.Arbitrary;
|
||||||
import javaslang.test.CheckResult;
|
import io.vavr.test.CheckResult;
|
||||||
import javaslang.test.Property;
|
import io.vavr.test.Property;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import static javaslang.API.*;
|
import static io.vavr.API.*;
|
||||||
|
|
||||||
public class PropertyBasedLongRunningUnitTest {
|
public class PropertyBasedLongRunningUnitTest {
|
||||||
|
|
|
@ -1,30 +1,35 @@
|
||||||
package com.baeldung.javaslang;
|
package com.baeldung.vavr;
|
||||||
|
|
||||||
import javaslang.Function0;
|
import io.vavr.Function0;
|
||||||
import javaslang.Function1;
|
import io.vavr.Function1;
|
||||||
import javaslang.Function2;
|
import io.vavr.Function2;
|
||||||
import javaslang.Function5;
|
import io.vavr.Function5;
|
||||||
import javaslang.Lazy;
|
import io.vavr.Lazy;
|
||||||
import javaslang.*;
|
import io.vavr.*;
|
||||||
import javaslang.collection.List;
|
import io.vavr.collection.List;
|
||||||
import javaslang.control.Option;
|
import io.vavr.collection.Seq;
|
||||||
import javaslang.control.Try;
|
import io.vavr.control.Option;
|
||||||
import javaslang.control.Validation;
|
import io.vavr.control.Try;
|
||||||
|
import io.vavr.control.Validation;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.vavr.Person;
|
||||||
|
import com.baeldung.vavr.PersonValidator;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
import static javaslang.API.*;
|
import static io.vavr.API.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class JavaSlangUnitTest {
|
public class VavrUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenList_whenSorts_thenCorrect() {
|
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
|
@Test
|
||||||
public void givenTuple_whenMapsComponentWise_thenCorrect() {
|
public void givenTuple_whenMapsComponentWise_thenCorrect() {
|
||||||
Tuple2<String, Integer> java8 = Tuple.of("Java", 8);
|
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();
|
int num = mapOfJava8._2();
|
||||||
assertEquals("Javaslang", mapOfJava8._1);
|
assertEquals("JavaVavr", mapOfJava8._1);
|
||||||
|
|
||||||
assertEquals(4, num);
|
assertEquals(4, num);
|
||||||
|
|
||||||
|
@ -69,9 +74,9 @@ public class JavaSlangUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenTuple_whenMapsWithOneMapper_thenCorrect() {
|
public void givenTuple_whenMapsWithOneMapper_thenCorrect() {
|
||||||
Tuple2<String, Integer> java8 = Tuple.of("Java", 8);
|
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();
|
int num = mapOfJava8._2();
|
||||||
assertEquals("Javaslang", mapOfJava8._1);
|
assertEquals("JavaVavr", mapOfJava8._1);
|
||||||
|
|
||||||
assertEquals(4, num);
|
assertEquals(4, num);
|
||||||
}
|
}
|
||||||
|
@ -80,8 +85,8 @@ public class JavaSlangUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenTuple_whenTransforms_thenCorrect() {
|
public void givenTuple_whenTransforms_thenCorrect() {
|
||||||
Tuple2<String, Integer> java8 = Tuple.of("Java", 8);
|
Tuple2<String, Integer> java8 = Tuple.of("Java", 8);
|
||||||
String transformed = java8.apply((s, i) -> s + "slang " + i / 2);
|
String transformed = java8.apply((s, i) -> s + "Vavr " + i / 2);
|
||||||
assertEquals("Javaslang 4", transformed);
|
assertEquals("JavaVavr 4", transformed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -102,14 +107,14 @@ public class JavaSlangUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJavaslangFunction_whenWorks_thenCorrect() {
|
public void givenVavrFunction_whenWorks_thenCorrect() {
|
||||||
Function1<Integer, Integer> square = (num) -> num * num;
|
Function1<Integer, Integer> square = (num) -> num * num;
|
||||||
Integer result = square.apply(2);
|
Integer result = square.apply(2);
|
||||||
assertEquals(Integer.valueOf(4), result);
|
assertEquals(Integer.valueOf(4), result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJavaslangBiFunction_whenWorks_thenCorrect() {
|
public void givenVavrBiFunction_whenWorks_thenCorrect() {
|
||||||
Function2<Integer, Integer, Integer> sum = (num1, num2) -> num1 + num2;
|
Function2<Integer, Integer, Integer> sum = (num1, num2) -> num1 + num2;
|
||||||
Integer result = sum.apply(5, 7);
|
Integer result = sum.apply(5, 7);
|
||||||
assertEquals(Integer.valueOf(12), result);
|
assertEquals(Integer.valueOf(12), result);
|
||||||
|
@ -117,9 +122,10 @@ public class JavaSlangUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCreatesFunction_thenCorrect0() {
|
public void whenCreatesFunction_thenCorrect0() {
|
||||||
Function0<String> getClazzName = () -> this.getClass().getName();
|
Function0<String> getClazzName = () -> this.getClass()
|
||||||
|
.getName();
|
||||||
String clazzName = getClazzName.apply();
|
String clazzName = getClazzName.apply();
|
||||||
assertEquals("com.baeldung.javaslang.JavaSlangUnitTest", clazzName);
|
assertEquals("com.baeldung.vavr.VavrUnitTest", clazzName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -132,8 +138,8 @@ public class JavaSlangUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenCreatesFunction_thenCorrect5() {
|
public void whenCreatesFunction_thenCorrect5() {
|
||||||
Function5<String, String, String, String, String, String> concat = (a, b, c, d, e) -> a + b + c + d + e;
|
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");
|
String finalString = concat.apply("Hello ", "world", "! ", "Learn ", "Vavr");
|
||||||
assertEquals("Hello world! Learn Javaslang", finalString);
|
assertEquals("Hello world! Learn Vavr", finalString);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -147,7 +153,6 @@ public class JavaSlangUnitTest {
|
||||||
return a + b;
|
return a + b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Values
|
* Values
|
||||||
*/
|
*/
|
||||||
|
@ -207,11 +212,11 @@ public class JavaSlangUnitTest {
|
||||||
assertEquals(-1, errorSentinel);
|
assertEquals(-1, errorSentinel);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Test(expected = ArithmeticException.class)
|
// @Test(expected = ArithmeticException.class)
|
||||||
// public void givenBadCode_whenTryHandles_thenCorrect3() {
|
// public void givenBadCode_whenTryHandles_thenCorrect3() {
|
||||||
// Try<Integer> result = Try.of(() -> 1 / 0);
|
// Try<Integer> result = Try.of(() -> 1 / 0);
|
||||||
// result.getOrElseThrow(ArithmeticException::new);
|
// result.getOrElseThrow(ArithmeticException::new);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// lazy
|
// lazy
|
||||||
@Test
|
@Test
|
||||||
|
@ -230,8 +235,8 @@ public class JavaSlangUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenValidationWorks_thenCorrect() {
|
public void whenValidationWorks_thenCorrect() {
|
||||||
PersonValidator personValidator = new PersonValidator();
|
PersonValidator personValidator = new PersonValidator();
|
||||||
Validation<List<String>, Person> valid = personValidator.validatePerson("John Doe", 30);
|
Validation<Seq<String>, Person> valid = personValidator.validatePerson("John Doe", 30);
|
||||||
Validation<List<String>, Person> invalid = personValidator.validatePerson("John? Doe!4", -1);
|
Validation<Seq<String>, Person> invalid = personValidator.validatePerson("John? Doe!4", -1);
|
||||||
|
|
||||||
assertEquals("Valid(Person [name=John Doe, age=30])", valid.toString());
|
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());
|
assertEquals("Invalid(List(Invalid characters in name: ?!4, Age must be at least 0))", invalid.toString());
|
||||||
|
@ -251,12 +256,13 @@ public class JavaSlangUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenSumsJava8List_thenCorrect() {
|
public void whenSumsJava8List_thenCorrect() {
|
||||||
// Arrays.asList(1, 2, 3).stream().reduce((i, j) -> i + j);
|
// 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);
|
assertEquals(6, sum);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCreatesJavaslangList_thenCorrect() {
|
public void whenCreatesVavrList_thenCorrect() {
|
||||||
List<Integer> intList = List.of(1, 2, 3);
|
List<Integer> intList = List.of(1, 2, 3);
|
||||||
assertEquals(3, intList.length());
|
assertEquals(3, intList.length());
|
||||||
assertEquals(new Integer(1), intList.get(0));
|
assertEquals(new Integer(1), intList.get(0));
|
||||||
|
@ -265,8 +271,10 @@ public class JavaSlangUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSumsJavaslangList_thenCorrect() {
|
public void whenSumsVavrList_thenCorrect() {
|
||||||
int sum = List.of(1, 2, 3).sum().intValue();
|
int sum = List.of(1, 2, 3)
|
||||||
|
.sum()
|
||||||
|
.intValue();
|
||||||
assertEquals(6, sum);
|
assertEquals(6, sum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -325,6 +333,4 @@ public class JavaSlangUnitTest {
|
||||||
assertEquals("two", output);
|
assertEquals("two", output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -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.vavr.exception.handling.client.ClientException;
|
||||||
import com.baeldung.javaslang.exception.handling.client.HttpClient;
|
import com.baeldung.vavr.exception.handling.client.HttpClient;
|
||||||
import com.baeldung.javaslang.exception.handling.client.Response;
|
import com.baeldung.vavr.exception.handling.client.Response;
|
||||||
import javaslang.collection.Stream;
|
import com.baeldung.vavr.exception.handling.VavrTry;
|
||||||
import javaslang.control.Option;
|
|
||||||
import javaslang.control.Try;
|
import io.vavr.collection.Stream;
|
||||||
|
import io.vavr.control.Option;
|
||||||
|
import io.vavr.control.Try;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.function.Function;
|
import static io.vavr.API.*;
|
||||||
import java.util.stream.Collectors;
|
import static io.vavr.Predicates.instanceOf;
|
||||||
|
|
||||||
import static javaslang.API.Case;
|
|
||||||
import static javaslang.API.Match;
|
|
||||||
import static javaslang.Predicates.instanceOf;
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class JavaslangTryUnitTest {
|
public class VavrTryUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenHttpClient_whenMakeACall_shouldReturnSuccess() {
|
public void givenHttpClient_whenMakeACall_shouldReturnSuccess() {
|
||||||
|
@ -26,7 +24,7 @@ public class JavaslangTryUnitTest {
|
||||||
HttpClient httpClient = () -> new Response(id);
|
HttpClient httpClient = () -> new Response(id);
|
||||||
|
|
||||||
//when
|
//when
|
||||||
Try<Response> response = new JavaslangTry(httpClient).getResponse();
|
Try<Response> response = new VavrTry(httpClient).getResponse();
|
||||||
Integer chainedResult = response
|
Integer chainedResult = response
|
||||||
.map(this::actionThatTakesResponse)
|
.map(this::actionThatTakesResponse)
|
||||||
.getOrElse(defaultChainedResult);
|
.getOrElse(defaultChainedResult);
|
||||||
|
@ -49,7 +47,7 @@ public class JavaslangTryUnitTest {
|
||||||
};
|
};
|
||||||
|
|
||||||
//when
|
//when
|
||||||
Try<Response> response = new JavaslangTry(httpClient).getResponse();
|
Try<Response> response = new VavrTry(httpClient).getResponse();
|
||||||
Integer chainedResult = response
|
Integer chainedResult = response
|
||||||
.map(this::actionThatTakesResponse)
|
.map(this::actionThatTakesResponse)
|
||||||
.getOrElse(defaultChainedResult);
|
.getOrElse(defaultChainedResult);
|
||||||
|
@ -71,9 +69,9 @@ public class JavaslangTryUnitTest {
|
||||||
};
|
};
|
||||||
|
|
||||||
//when
|
//when
|
||||||
Try<Response> recovered = new JavaslangTry(httpClient).getResponse()
|
Try<Response> recovered = new VavrTry(httpClient).getResponse()
|
||||||
.recover(r -> Match(r).of(
|
.recover(r -> Match(r).of(
|
||||||
Case(instanceOf(ClientException.class), defaultResponse)
|
Case($(instanceOf(ClientException.class)), defaultResponse)
|
||||||
));
|
));
|
||||||
|
|
||||||
//then
|
//then
|
||||||
|
@ -93,10 +91,10 @@ public class JavaslangTryUnitTest {
|
||||||
};
|
};
|
||||||
|
|
||||||
//when
|
//when
|
||||||
Try<Response> recovered = new JavaslangTry(httpClient).getResponse()
|
Try<Response> recovered = new VavrTry(httpClient).getResponse()
|
||||||
.recover(r -> Match(r).of(
|
.recover(r -> Match(r).of(
|
||||||
Case(instanceOf(ClientException.class), defaultResponse),
|
Case($(instanceOf(ClientException.class)), defaultResponse),
|
||||||
Case(instanceOf(IllegalArgumentException.class), defaultResponse)
|
Case($(instanceOf(IllegalArgumentException.class)), defaultResponse)
|
||||||
));
|
));
|
||||||
|
|
||||||
//then
|
//then
|
Loading…
Reference in New Issue