Idiomatic refactor (#2161)

This commit is contained in:
Grzegorz Piwowarek 2017-06-26 16:37:03 +02:00 committed by GitHub
parent 590cb07df6
commit 04b9bd0382
1 changed files with 35 additions and 32 deletions

View File

@ -1,17 +1,17 @@
package com.baeldung.vavr.exception.handling;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import io.vavr.API;
import io.vavr.CheckedFunction1;
import io.vavr.Value;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.vavr.API;
import io.vavr.CheckedFunction1;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class VavrExceptionHandlingUnitTest {
@ -54,12 +54,16 @@ public class VavrExceptionHandlingUnitTest {
public void exceptionCausingMethod_UsingLift_ThenSuccess() {
validIntegersOnly.stream().map(CheckedFunction1.lift(i -> readFromFile(i)))
.map(i -> i.getOrElse(-1))
.forEach(i -> {Assert.assertNotSame(-1, i);LOG.debug("{}", i);});
.forEach(i -> {
Assert.assertNotSame(-1, i);
LOG.debug("{}", i);
});
}
@Test
public void exceptionCausingMethod_UsingLift_ThenFailure() {
integers.stream().map(CheckedFunction1.lift(i -> readFromFile(i)))
integers.stream()
.map(CheckedFunction1.lift(i -> readFromFile(i)))
.map(i -> i.getOrElse(-1))
.forEach(i -> LOG.debug("{}", i));
@ -67,11 +71,10 @@ public class VavrExceptionHandlingUnitTest {
@Test
public void exceptionCausingMethod_UsingTry_ThenSuccess() {
integers.stream().map(CheckedFunction1.liftTry(i -> readFromFile(i)))
.map(i -> i.onFailure(e -> Assert.assertTrue(e instanceof IOException)))
.map(i -> i.onSuccess(e -> LOG.debug("{}", e)))
.forEach(i -> {});
integers.stream()
.map(CheckedFunction1.liftTry(VavrExceptionHandlingUnitTest::readFromFile))
.flatMap(Value::toJavaStream)
.forEach(i -> LOG.debug("{}", i));
}
private static Integer readFromFile(Integer i) throws IOException {