Skip to content

Commit

Permalink
Add BooleanMatcher.isNot() operator
Browse files Browse the repository at this point in the history
Also add some tests to BooleanTemplate
  • Loading branch information
asereda-gs committed May 6, 2020
1 parent 3427fa2 commit c9c6305
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Expand Up @@ -20,24 +20,44 @@
import org.immutables.criteria.expression.Operators;

/**
* Very simple matcher for booleans just has {@code true} / {@code false} checks.
* Very simple matcher for booleans has just {@code true} / {@code false} checks.
*
* @param <R> root criteria type
*/
public interface BooleanMatcher<R> extends Matcher {

/**
* Predicate {@code this == true}
*/
default R isTrue() {
return is(true);
}

/**
* Predicate {@code this == false}
*/
default R isFalse() {
return is(false);
}

/**
* Equivalent to {@code this == value}
* @param value boolean value
*/
default R is(boolean value) {
return Matchers.extract(this).applyAndCreateRoot(e -> Expressions.binaryCall(Operators.EQUAL, e, Expressions.constant(value)));
}

/**
* For some interpreters (backends) {@code value != false} is not equivalent to {@code value == true}
* (eg. Three-Valued-Logic). Creating separate method based on {@link Operators#NOT_EQUAL} operator.
*
* @see <a href="https://modern-sql.com/concept/three-valued-logic">Three Valued Logic</a>
*/
default R isNot(boolean value) {
return Matchers.extract(this).applyAndCreateRoot(e -> Expressions.binaryCall(Operators.NOT_EQUAL, e, Expressions.constant(value)));
}

/**
* Self-type for this matcher
*/
Expand Down
Expand Up @@ -145,6 +145,19 @@ void projection() {
.hasContentInAnyOrder("id=id1 value=true nullable=false optional=false", "id=id2 value=false nullable=true optional=true", "id=id3 value=false nullable=null optional=<empty>");
}

@Test
void isNot() {
repository.insert(generator.get().withId("id1").withValue(true).withNullable(false).withBoxed(Boolean.TRUE).withOptional(Optional.of(false)));
repository.insert(generator.get().withId("id2").withValue(false).withNullable(null).withBoxed(Boolean.FALSE).withOptional(Optional.empty()));


ids(holder.value.isNot(true)).isOf("id2");
ids(holder.value.isNot(false)).isOf("id1");

ids(holder.nullable.isNot(true)).notEmpty();
ids(holder.nullable.isNot(false)).not().has("id1");
}

private IterableChecker<List<String>, String> ids(BooleanHolderCriteria criteria) {
return CriteriaChecker.<TypeHolder.BooleanHolder>ofReader(repository.find(criteria)).toList(TypeHolder.BooleanHolder::id);
}
Expand Down

0 comments on commit c9c6305

Please sign in to comment.