Skip to content

Commit ea5afe3

Browse files
authored
New feature: .mapTo(U) and .mapToVoid() for Value<T> (#3086)
1 parent d4b0871 commit ea5afe3

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

vavr/src/main/java/io/vavr/Value.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,26 @@ default T getOrNull() {
416416
return isEmpty() ? null : get();
417417
}
418418

419+
/**
420+
* Maps the underlying value to another fixed value.
421+
*
422+
* @param value value to replace the contents with
423+
* @param <U> The new component type
424+
* @return A new value
425+
*/
426+
default <U> Value<U> mapTo(U value) {
427+
return map((ignored) -> value);
428+
}
429+
430+
/**
431+
* Maps the underlying value to Void
432+
*
433+
* @return A new value of type Void
434+
*/
435+
default Value<Void> mapToVoid() {
436+
return map((ignored) -> null);
437+
}
438+
419439
/**
420440
* Checks if this {@code Value} is asynchronously (short: async) computed.
421441
* <p>

vavr/src/main/java/io/vavr/collection/TreeSet.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,11 @@ public <U> SortedSet<U> zipWithIndex(BiFunction<? super T, ? super Integer, ? ex
10171017
return TreeSet.ofAll(Comparators.naturalComparator(), iterator().zipWithIndex(mapper));
10181018
}
10191019

1020+
@Override
1021+
public TreeSet<Void> mapToVoid() {
1022+
return map((o1, o2) -> 0, ignored -> null);
1023+
}
1024+
10201025
// -- Object
10211026

10221027
@Override

vavr/src/test/java/io/vavr/AbstractValueTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,25 @@ public void shouldReturnValueWhenGetOrNullOfNonEmpty() {
218218
assertThat(of(1).getOrNull()).isEqualTo(1);
219219
}
220220

221+
// -- mapTo
222+
223+
@TestTemplate
224+
public void shouldExecuteMapToCorrectly() {
225+
assertThat(empty().mapTo(1)).isEqualTo(empty().mapTo(2));
226+
assertThat(of(2).mapTo(1)).isEqualTo(of(3).mapTo(1));
227+
assertThat(of(2).mapTo(1)).isEqualTo(of(3).map(ignored -> 1));
228+
assertThat(of(3).mapTo(2)).isEqualTo(of(1).map(ignored -> 2));
229+
}
230+
231+
// -- mapToVoid
232+
233+
@TestTemplate
234+
public void shouldExecuteMapToVoidCorrectly() {
235+
assertThat(empty().mapToVoid()).isEqualTo(empty());
236+
assertThat(of(1).mapToVoid()).isEqualTo(of(1).mapTo(null));
237+
assertThat(of(1).mapToVoid()).isEqualTo(of(1).map(ignored -> null));
238+
}
239+
221240
// -- forEach
222241

223242
@TestTemplate

vavr/src/test/java/io/vavr/collection/BitSetTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,13 @@ public void shouldReturnTailOfNonEmptyHavingReversedOrder() {
565565
// BitSet can't have reverse order
566566
}
567567

568+
@Test
569+
@Override
570+
public void shouldExecuteMapToVoidCorrectly() {
571+
assertThat(empty().mapToVoid()).isEqualTo(empty());
572+
assertThat(of(1).mapToVoid()).isEqualTo(of((Integer)null));
573+
}
574+
568575
// -- classes
569576

570577
private static final class Mapper<T> implements Serializable {

vavr/src/test/java/io/vavr/collection/TreeSetTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,11 @@ public void shouldZipAllEmptyAndNonNil() {
411411
@Disabled
412412
public void shouldZipAllNonEmptyAndNil() {
413413
}
414+
415+
@Test
416+
@Override
417+
public void shouldExecuteMapToVoidCorrectly() {
418+
assertThat(empty().mapToVoid()).isEqualTo(empty());
419+
assertThat(of(1).map(ignored -> null)).isEqualTo(of(1).mapToVoid());
420+
}
414421
}

0 commit comments

Comments
 (0)