Skip to content

Commit 4462b49

Browse files
committed
implement DescribedPredicate#negate()
overwriting `java.util.function.Predicate#negate()` to return a `DescribedPredicate` Resolves #1480 Signed-off-by: Manfred Hanke <[email protected]>
1 parent 507f675 commit 4462b49

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

archunit/src/main/java/com/tngtech/archunit/base/DescribedPredicate.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public String getDescription() {
5151
return description;
5252
}
5353

54+
/**
55+
* @return A {@link DescribedPredicate} that will return {@code true} whenever this predicate would return {@code false}, and vice versa.
56+
*/
57+
@Override
58+
@PublicAPI(usage = ACCESS)
59+
public DescribedPredicate<T> negate() {
60+
return new NotPredicate<>(this);
61+
}
62+
5463
/**
5564
* Overwrites the description of this {@link DescribedPredicate}. E.g.
5665
*
@@ -176,7 +185,7 @@ public static <T> DescribedPredicate<T> doNot(DescribedPredicate<? super T> pred
176185

177186
/**
178187
* @param predicate Any {@link DescribedPredicate}
179-
* @return A predicate that will return {@code true} whenever the original predicate would return {@code false} and vice versa.
188+
* @return A predicate that will return {@code true} whenever the original predicate would return {@code false}, and vice versa.
180189
* @param <T> The type of object the {@link DescribedPredicate predicate} applies to
181190
*/
182191
@PublicAPI(usage = ACCESS)

archunit/src/test/java/com/tngtech/archunit/base/DescribedPredicateTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,25 @@ public void describe_works() {
200200
@DataProvider
201201
public static Object[][] not_scenarios() {
202202
return $$(
203-
$(new NotScenario("not") {
203+
$(new NotScenario("not", ".negate") {
204+
@Override
205+
<T> DescribedPredicate<T> apply(DescribedPredicate<T> input) {
206+
return input.negate();
207+
}
208+
}),
209+
$(new NotScenario("not", "not") {
204210
@Override
205211
<T> DescribedPredicate<T> apply(DescribedPredicate<T> input) {
206212
return not(input);
207213
}
208214
}),
209-
$(new NotScenario("do not") {
215+
$(new NotScenario("do not", "doNot") {
210216
@Override
211217
<T> DescribedPredicate<T> apply(DescribedPredicate<T> input) {
212218
return doNot(input);
213219
}
214220
}),
215-
$(new NotScenario("does not") {
221+
$(new NotScenario("does not", "doesNot") {
216222
@Override
217223
<T> DescribedPredicate<T> apply(DescribedPredicate<T> input) {
218224
return doesNot(input);
@@ -279,16 +285,18 @@ private Function<Object, Integer> constant(int integer) {
279285

280286
private abstract static class NotScenario {
281287
private final String expectedPrefix;
288+
private final String methodName;
282289

283-
NotScenario(String expectedPrefix) {
290+
NotScenario(String expectedPrefix, String methodName) {
284291
this.expectedPrefix = expectedPrefix;
292+
this.methodName = methodName;
285293
}
286294

287295
abstract <T> DescribedPredicate<T> apply(DescribedPredicate<T> input);
288296

289297
@Override
290298
public String toString() {
291-
return expectedPrefix;
299+
return expectedPrefix + " (via " + methodName + ")";
292300
}
293301
}
294302

0 commit comments

Comments
 (0)