Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions vavr/src/main/java/io/vavr/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,26 @@ default T getOrNull() {
return isEmpty() ? null : get();
}

/**
* Maps the underlying value to another fixed value.
*
* @param value value to replace the contents with
* @param <U> The new component type
* @return A new value
*/
default <U> Value<U> mapTo(U value) {
return map((ignored) -> value);
}

/**
* Maps the underlying value to Void
*
* @return A new value of type Void
*/
default Value<Void> mapToVoid() {
return map((ignored) -> null);
}

/**
* Checks if this {@code Value} is asynchronously (short: async) computed.
* <p>
Expand Down
5 changes: 5 additions & 0 deletions vavr/src/main/java/io/vavr/collection/TreeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,11 @@ public <U> SortedSet<U> zipWithIndex(BiFunction<? super T, ? super Integer, ? ex
return TreeSet.ofAll(Comparators.naturalComparator(), iterator().zipWithIndex(mapper));
}

@Override
public TreeSet<Void> mapToVoid() {
return map((o1, o2) -> 0, ignored -> null);
}

// -- Object

@Override
Expand Down
19 changes: 19 additions & 0 deletions vavr/src/test/java/io/vavr/AbstractValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,25 @@ public void shouldReturnValueWhenGetOrNullOfNonEmpty() {
assertThat(of(1).getOrNull()).isEqualTo(1);
}

// -- mapTo

@TestTemplate
public void shouldExecuteMapToCorrectly() {
assertThat(empty().mapTo(1)).isEqualTo(empty().mapTo(2));
assertThat(of(2).mapTo(1)).isEqualTo(of(3).mapTo(1));
assertThat(of(2).mapTo(1)).isEqualTo(of(3).map(ignored -> 1));
assertThat(of(3).mapTo(2)).isEqualTo(of(1).map(ignored -> 2));
}

// -- mapToVoid

@TestTemplate
public void shouldExecuteMapToVoidCorrectly() {
assertThat(empty().mapToVoid()).isEqualTo(empty());
assertThat(of(1).mapToVoid()).isEqualTo(of(1).mapTo(null));
assertThat(of(1).mapToVoid()).isEqualTo(of(1).map(ignored -> null));
}

// -- forEach

@TestTemplate
Expand Down
7 changes: 7 additions & 0 deletions vavr/src/test/java/io/vavr/collection/BitSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,13 @@ public void shouldReturnTailOfNonEmptyHavingReversedOrder() {
// BitSet can't have reverse order
}

@Test
@Override
public void shouldExecuteMapToVoidCorrectly() {
assertThat(empty().mapToVoid()).isEqualTo(empty());
assertThat(of(1).mapToVoid()).isEqualTo(of((Integer)null));
}

// -- classes

private static final class Mapper<T> implements Serializable {
Expand Down
7 changes: 7 additions & 0 deletions vavr/src/test/java/io/vavr/collection/TreeSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,11 @@ public void shouldZipAllEmptyAndNonNil() {
@Disabled
public void shouldZipAllNonEmptyAndNil() {
}

@Test
@Override
public void shouldExecuteMapToVoidCorrectly() {
assertThat(empty().mapToVoid()).isEqualTo(empty());
assertThat(of(1).map(ignored -> null)).isEqualTo(of(1).mapToVoid());
}
}