[Java 11502] (#12625)
* [JAVA-11502] Added vavr-modules (parent) * [JAVA-11502] Added vavr(submodule) to vavr-modules(parent) * [JAVA-11502] Added vavr-2(submodule) to vavr-modules(parent) * [JAVA-11502] Added java-vavr-stream(submodule) to vavr-modules(parent) * [JAVA-11502] deleted modules that were moved + cleanup Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com> Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
This commit is contained in:
8
vavr-modules/vavr-2/README.md
Normal file
8
vavr-modules/vavr-2/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
## Vavr
|
||||
|
||||
This module contains articles about Vavr.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Introduction to Vavr’s Either](https://www.baeldung.com/vavr-either)
|
||||
- [Interoperability Between Java and Vavr](https://www.baeldung.com/java-vavr)
|
||||
- [[<-- prev]](/vavr)
|
||||
27
vavr-modules/vavr-2/pom.xml
Normal file
27
vavr-modules/vavr-2/pom.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>vavr-2</artifactId>
|
||||
<name>vavr-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>vavr-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr-test</artifactId>
|
||||
<version>${vavr.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<vavr.version>0.9.1</vavr.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.vavr.either;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import io.vavr.control.Either;
|
||||
|
||||
public class EitherDemo {
|
||||
|
||||
public static Object[] computeWithoutEitherUsingArray(int marks) {
|
||||
Object[] results = new Object[2];
|
||||
if (marks < 85) {
|
||||
results[0] = "Marks not acceptable";
|
||||
} else {
|
||||
results[1] = marks;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public static Map<String, Object> computeWithoutEitherUsingMap(int marks) {
|
||||
Map<String, Object> results = new HashMap<>();
|
||||
if (marks < 85) {
|
||||
results.put("FAILURE", "Marks not acceptable");
|
||||
} else {
|
||||
results.put("SUCCESS", marks);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
static Either<String, Integer> computeWithEither(int marks) {
|
||||
if (marks < 85) {
|
||||
return Either.left("Marks not acceptable");
|
||||
} else {
|
||||
return Either.right(marks);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.vavr.either;
|
||||
|
||||
import io.vavr.control.Either;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class EitherUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMarks_whenPassNumber_thenExpectNumber() {
|
||||
Either<String, Integer> result = EitherDemo.computeWithEither(100);
|
||||
int marks = result.right()
|
||||
.getOrElseThrow(x -> new IllegalStateException());
|
||||
|
||||
assertEquals(100, marks);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMarks_whenFailNumber_thenExpectErrorMesssage() {
|
||||
Either<String, Integer> result = EitherDemo.computeWithEither(50);
|
||||
String error = result.left()
|
||||
.getOrNull();
|
||||
|
||||
assertEquals("Marks not acceptable", error);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPassMarks_whenModified_thenExpectNumber() {
|
||||
Either<String, Integer> result = EitherDemo.computeWithEither(90);
|
||||
int marks = result.right()
|
||||
.map(x -> x * 2)
|
||||
.get();
|
||||
|
||||
assertEquals(180, marks);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.baeldung.vavr.interoperability;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import io.vavr.collection.HashMap;
|
||||
import io.vavr.collection.LinkedHashSet;
|
||||
import io.vavr.collection.List;
|
||||
import io.vavr.collection.Map;
|
||||
import io.vavr.collection.Set;
|
||||
import io.vavr.collection.Stream;
|
||||
|
||||
public class CollectionsInteroperabilityUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenParams_whenVavrList_thenReturnJavaList() {
|
||||
List<String> vavrStringList = List.of("JAVA", "Javascript", "Scala");
|
||||
|
||||
java.util.List<String> javaStringList = vavrStringList.toJavaList();
|
||||
assertTrue(javaStringList instanceof java.util.List);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParams_whenVavrStream_thenReturnJavaStream() {
|
||||
Stream<String> vavrStream = Stream.of("JAVA", "Javascript", "Scala");
|
||||
|
||||
java.util.stream.Stream<String> javaParallelStream = vavrStream.toJavaParallelStream();
|
||||
assertTrue(javaParallelStream instanceof java.util.stream.Stream);
|
||||
|
||||
java.util.List<String> javaStringList = vavrStream.toJavaList();
|
||||
assertTrue(javaStringList instanceof java.util.List);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParams_whenVavrMap_thenReturnJavaMap() {
|
||||
Map<String, String> vavrMap = HashMap.of("1", "a", "2", "b", "3", "c");
|
||||
|
||||
java.util.Map<String, String> javaMap = vavrMap.toJavaMap();
|
||||
assertTrue(javaMap instanceof java.util.Map);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParams_whenJavaList_thenReturnVavrListUsingOfAll() {
|
||||
java.util.List<String> javaList = Arrays.asList("Java", "Haskell", "Scala");
|
||||
List<String> vavrList = List.ofAll(javaList);
|
||||
assertTrue(vavrList instanceof io.vavr.collection.List);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParams_whenJavaStream_thenReturnVavrListUsingOfAll() {
|
||||
java.util.stream.Stream<String> javaStream = Arrays.asList("Java", "Haskell", "Scala")
|
||||
.stream();
|
||||
Stream<String> vavrStream = Stream.ofAll(javaStream);
|
||||
assertTrue(vavrStream instanceof io.vavr.collection.Stream);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void givenParams_whenVavrListConverted_thenException() {
|
||||
java.util.List<String> javaList = List.of("Java", "Haskell", "Scala")
|
||||
.asJava();
|
||||
javaList.add("Python");
|
||||
assertEquals(4, javaList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParams_whenVavrListConvertedToMutable_thenRetunMutableList() {
|
||||
java.util.List<String> javaList = List.of("Java", "Haskell", "Scala")
|
||||
.asJavaMutable();
|
||||
javaList.add("Python");
|
||||
assertEquals(4, javaList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParams_WhenVavarListConvertedToLinkedSet_thenReturnLinkedSet() {
|
||||
List<String> vavrList = List.of("Java", "Haskell", "Scala", "Java");
|
||||
Set<String> linkedSet = vavrList.toLinkedSet();
|
||||
assertEquals(3, linkedSet.size());
|
||||
assertTrue(linkedSet instanceof LinkedHashSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParams_WhenVavrList_thenReturnJavaOptional() {
|
||||
List<String> vavrList = List.of("Java");
|
||||
Optional<String> optional = vavrList.toJavaOptional();
|
||||
assertEquals("Java", optional.get());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user