Skip to content

Commit 3797b05

Browse files
authored
Add Option#mapTry() (#2977)
Add `mapTry` method to Option, which is a shortcut for `.toTry().mapTry`: * when mapping an optional using a checked method, I find it intuitive that the type changes to a Try, and `mapTry` methods exist on other types already * I initially added it onto `Value` but this ran into an issue where a method with same name but return value already exists on subtype`Future` ---- Rebase of #2950
1 parent 5f6207c commit 3797b05

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

vavr/src/main/java/io/vavr/control/Option.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package io.vavr.control;
2020

21+
import io.vavr.CheckedFunction1;
2122
import io.vavr.PartialFunction;
2223
import io.vavr.Tuple;
2324
import io.vavr.Value;
@@ -391,6 +392,20 @@ default <U> Option<U> map(Function<? super T, ? extends U> mapper) {
391392
return isEmpty() ? none() : some(mapper.apply(get()));
392393
}
393394

395+
396+
/**
397+
* Converts this to a {@link Try}, then runs the given checked function if this is a {@link Try.Success},
398+
* passing the result of the current expression to it.
399+
*
400+
* @param <U> The new component type
401+
* @param mapper A checked function
402+
* @return a {@code Try}
403+
* @throws NullPointerException if {@code mapper} is null
404+
*/
405+
default <U> Try<U> mapTry(CheckedFunction1<? super T, ? extends U> mapper) {
406+
return toTry().mapTry(mapper);
407+
}
408+
394409
/**
395410
* Folds either the {@code None} or the {@code Some} side of the Option value.
396411
*

vavr/src/test/java/io/vavr/control/OptionTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.vavr.PartialFunction;
2626
import io.vavr.Serializables;
2727
import org.junit.jupiter.api.Assertions;
28+
import org.junit.jupiter.api.Nested;
2829
import org.junit.jupiter.api.Test;
2930

3031
import java.util.*;
@@ -361,6 +362,35 @@ public void shouldMapNone() {
361362
assertThat(Option.<Integer> none().map(String::valueOf)).isEqualTo(Option.none());
362363
}
363364

365+
@Nested
366+
class MapTry {
367+
@Test
368+
public void shouldMapTrySome() {
369+
assertThat(Option.of(1).mapTry(String::valueOf)).isEqualTo(Try.success("1"));
370+
}
371+
372+
@Test
373+
public void shouldMapTryNone() {
374+
Try<String> result = Option.none().mapTry(String::valueOf);
375+
assertThat(result.isFailure()).isTrue();
376+
assertThat(result.getCause().getClass()).isEqualTo(NoSuchElementException.class);
377+
assertThat(result.getCause().getMessage()).isEqualTo("No value present");
378+
}
379+
380+
@Test
381+
public void shouldMapTryCheckedException() {
382+
Try<Integer> result = Option.of("a")
383+
.mapTry(this::checkedFunction);
384+
assertThat(result.isFailure()).isTrue();
385+
assertThat(result.getCause().getClass()).isEqualTo(Exception.class);
386+
assertThat(result.getCause().getMessage()).isEqualTo("message");
387+
}
388+
389+
private Integer checkedFunction(String string) throws Exception {
390+
throw new Exception("message");
391+
}
392+
}
393+
364394
// -- flatMap
365395

366396
@Test

0 commit comments

Comments
 (0)