/
CommonFilter.java
74 lines (64 loc) · 2 KB
/
CommonFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.gen5.engine;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.joining;
import java.util.Collection;
/**
* A collection of common builders for {@link DiscoveryRequest} elements.
*
* @since 5.0
*/
class CommonFilter {
@SafeVarargs
public static <T> DiscoveryFilter<T> combine(DiscoveryFilter<T>... filters) {
if (filters == null) {
return alwaysIncluded();
}
else {
return combine(asList(filters));
}
}
public static <T> DiscoveryFilter<T> combine(Collection<DiscoveryFilter<T>> filters) {
if (filters == null || filters.isEmpty()) {
return alwaysIncluded();
}
else if (filters.size() == 1) {
return filters.iterator().next();
}
else {
return new AndAllDiscoveryFilter<>(filters);
}
}
private static <T> DiscoveryFilter<T> alwaysIncluded() {
return (any) -> FilterResult.included("Always included");
}
private static class AndAllDiscoveryFilter<T> implements DiscoveryFilter<T> {
private final Collection<DiscoveryFilter<T>> filters;
public AndAllDiscoveryFilter(Collection<DiscoveryFilter<T>> filters) {
this.filters = filters;
}
@Override
public FilterResult filter(T element) {
// @formatter:off
return this.filters.stream()
.map(filter -> filter.filter(element))
.filter(FilterResult::excluded)
.findFirst()
.orElse(FilterResult.included("Element was included by all filters."));
// @formatter:on
}
@Override
public String toString() {
return this.filters.stream().map(Object::toString).map(s -> format("(%s)", s)).collect(joining(" and "));
}
}
}