Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce boolean functions to simplify combining Mono<Boolean> inputs #161

Merged
merged 2 commits into from
Aug 14, 2018
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
95 changes: 95 additions & 0 deletions reactor-extra/src/main/java/reactor/bool/BooleanUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.bool;

import reactor.core.publisher.Mono;

/**
* Functions to help compose results with {@link Mono}-wrapped {@literal boolean} values.
*
* @author Greg Turnquist
*/
public final class BooleanUtils {

private BooleanUtils() {
}

/**
* A boolean, wrapped in a {@link Mono}, inverted by the NOT operator.
*
* @param value boolean
* @return {@literal !value} wrapped in a {@link Mono}
*/
public static Mono<Boolean> not(Mono<Boolean> value) {
return value.map(bool -> !bool);
}

/**
* Two booleans, wrapped in {@link Mono}, combined with the AND operator.
*
* @param value1 first boolean
* @param value2 second boolean
* @return {@literal value1 && value2} wrapped in a {@link Mono}
*/
public static Mono<Boolean> and(Mono<Boolean> value1, Mono<Boolean> value2) {
return Mono.zip(value1, value2, (bool1, bool2) -> bool1 && bool2);
}


/**
* Two booleans, wrapped in {@link Mono}, combined with the exclusive-OR operator.
*
* @param value1 first boolean
* @param value2 second boolean
* @return {@literal value1 XOR value2} wrapped in a {@link Mono}
*/
public static Mono<Boolean> xor(Mono<Boolean> value1, Mono<Boolean> value2) {
return Mono.zip(value1, value2, (bool1, bool2) -> (bool1 != bool2) && (bool1 || bool2));
}

/**
* Two booleans, wrapped in {@link Mono}, combined with the OR operator.
*
* @param value1 first boolean
* @param value2 second boolean
* @return {@literal value1 || value2} wrapped in a {@link Mono}
*/
public static Mono<Boolean> or(Mono<Boolean> value1, Mono<Boolean> value2) {
return Mono.zip(value1, value2, (bool1, bool2) -> bool1 || bool2);
}

/**
* Two booleans, wrapped in {@link Mono}, combined with the NOT-AND (NAND) operator.
*
* @param value1 first boolean
* @param value2 second boolean
* @return {@literal !(value1 && value2)} wrapped in a {@link Mono}
*/
public static Mono<Boolean> nand(Mono<Boolean> value1, Mono<Boolean> value2) {
return not(and(value1, value2));
}

/**
* Two booleans, wrapped in {@link Mono}, combined with the NOT-OR (NOR) operator.
*
* @param value1 first boolean
* @param value2 second boolean
* @return {@literal !(value1 || value2)} wrapped in a {@link Mono}
*/
public static Mono<Boolean> nor(Mono<Boolean> value1, Mono<Boolean> value2) {
return not(or(value1, value2));
}
}
124 changes: 124 additions & 0 deletions reactor-extra/src/test/java/reactor/bool/BooleanUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package reactor.bool;

import static org.assertj.core.api.Assertions.assertThat;
import static reactor.bool.BooleanUtils.*;
import static reactor.bool.BooleanUtils.not;

import org.junit.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

/**
* @author Greg Turnquist
*/
public class BooleanUtilsTest {

private static final Mono<Boolean> TRUE = Mono.just(true);
private static final Mono<Boolean> FALSE = Mono.just(false);

@Test
public void testNot() {
StepVerifier.create(not(TRUE))
.assertNext(bool -> assertThat(bool).as("not(TRUE)").isFalse())
.verifyComplete();

StepVerifier.create(not(FALSE))
.assertNext(bool -> assertThat(bool).as("not(FALSE)").isTrue())
.verifyComplete();
}

@Test
public void testAnd() {
StepVerifier.create(and(TRUE, TRUE))
.assertNext(bool -> assertThat(bool).as("and(TRUE, TRUE)").isTrue())
.verifyComplete();

StepVerifier.create(and(TRUE, FALSE))
.assertNext(bool -> assertThat(bool).as("and(TRUE, FALSE)").isFalse())
.verifyComplete();

StepVerifier.create(and(FALSE, TRUE))
.assertNext(bool -> assertThat(bool).as("and(FALSE, TRUE)").isFalse())
.verifyComplete();

StepVerifier.create(and(FALSE, FALSE))
.assertNext(bool -> assertThat(bool).as("and(FALSE, FALSE)").isFalse())
.verifyComplete();
}

@Test
public void testOr() {
StepVerifier.create(or(TRUE, TRUE))
.assertNext(bool -> assertThat(bool).as("or(TRUE, TRUE)").isTrue())
.verifyComplete();

StepVerifier.create(or(TRUE, FALSE))
.assertNext(bool -> assertThat(bool).as("or(TRUE, FALSE)").isTrue())
.verifyComplete();

StepVerifier.create(or(FALSE, TRUE))
.assertNext(bool -> assertThat(bool).as("or(FALSE, TRUE)").isTrue())
.verifyComplete();

StepVerifier.create(or(FALSE, FALSE))
.assertNext(bool -> assertThat(bool).as("or(FALSE, FALSE)").isFalse())
.verifyComplete();
}

@Test
public void testNand() {
StepVerifier.create(nand(TRUE, TRUE))
.assertNext(bool -> assertThat(bool).as("nand(TRUE, TRUE)").isFalse())
.verifyComplete();

StepVerifier.create(nand(TRUE, FALSE))
.assertNext(bool -> assertThat(bool).as("nand(TRUE, FALSE)").isTrue())
.verifyComplete();

StepVerifier.create(nand(FALSE, TRUE))
.assertNext(bool -> assertThat(bool).as("nand(FALSE, TRUE)").isTrue())
.verifyComplete();

StepVerifier.create(nand(FALSE, FALSE))
.assertNext(bool -> assertThat(bool).as("nand(FALSE, FALSE)").isTrue())
.verifyComplete();
}

@Test
public void testNor() {
StepVerifier.create(nor(TRUE, TRUE))
.assertNext(bool -> assertThat(bool).as("nor(TRUE, TRUE)").isFalse())
.verifyComplete();

StepVerifier.create(nor(TRUE, FALSE))
.assertNext(bool -> assertThat(bool).as("nor(TRUE, FALSE)").isFalse())
.verifyComplete();

StepVerifier.create(nor(FALSE, TRUE))
.assertNext(bool -> assertThat(bool).as("nor(FALSE, TRUE)").isFalse())
.verifyComplete();

StepVerifier.create(nor(FALSE, FALSE))
.assertNext(bool -> assertThat(bool).as("nor(FALSE, FALSE)").isTrue())
.verifyComplete();
}

@Test
public void testXor() {
StepVerifier.create(xor(TRUE, TRUE))
.assertNext(bool -> assertThat(bool).as("xor(TRUE, TRUE)").isFalse())
.verifyComplete();

StepVerifier.create(xor(TRUE, FALSE))
.assertNext(bool -> assertThat(bool).as("xor(TRUE, FALSE)").isTrue())
.verifyComplete();

StepVerifier.create(xor(FALSE, TRUE))
.assertNext(bool -> assertThat(bool).as("xor(FALSE, TRUE)").isTrue())
.verifyComplete();

StepVerifier.create(xor(FALSE, FALSE))
.assertNext(bool -> assertThat(bool).as("xor(FALSE, FALSE)").isFalse())
.verifyComplete();
}
}