Skip to content

Commit 8fdfff0

Browse files
authored
feat: Add Signal.not (#22842)
1 parent 656ce84 commit 8fdfff0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

signals/src/main/java/com/vaadin/signals/Signal.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ static <T> Signal<T> computed(Supplier<T> computation) {
131131
return new ComputedSignal<>(computation);
132132
}
133133

134+
/**
135+
* Crates a new computed signal containing the negation of the provided
136+
* boolean-valued signal. <code>null</code> values are preserved as
137+
* <code>null</code>.
138+
*
139+
* @param signal
140+
* the boolean-valued signal to negate, not <code>null</code>
141+
* @return the negated signal, not <code>null</code>
142+
*/
143+
static Signal<Boolean> not(Signal<Boolean> signal) {
144+
return Objects.requireNonNull(signal)
145+
.map(value -> value == null ? null : !value);
146+
}
147+
134148
/**
135149
* Runs the provided supplier in a transaction. All signal operations
136150
* performed within the transaction will be staged and atomically committed

signals/src/test/java/com/vaadin/signals/impl/ComputedSignalTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ void map_countCallbackInvocations_invocationsAreNotCached() {
136136
assertEquals(2, count.get());
137137
}
138138

139+
@Test
140+
void not_booleanInputs_negatedOutputs() {
141+
ValueSignal<Boolean> signal = new ValueSignal<>(Boolean.TRUE);
142+
Signal<Boolean> negated = Signal.not(signal);
143+
144+
assertFalse(negated.value());
145+
146+
signal.value(false);
147+
assertTrue(negated.value());
148+
149+
signal.value(null);
150+
assertNull(negated.value());
151+
}
152+
139153
@Test
140154
void callback_updateOtherSignal_signalUpdated() {
141155
ValueSignal<String> other = new ValueSignal<>("value");

0 commit comments

Comments
 (0)