File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed
main/java/com/vaadin/signals
test/java/com/vaadin/signals/impl Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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" );
You can’t perform that action at this time.
0 commit comments