|
19 | 19 | import java.util.Collection; |
20 | 20 | import java.util.List; |
21 | 21 |
|
| 22 | +/** |
| 23 | + * Strategy interface for filtering collections using one or more {@link DataFilter} instances. |
| 24 | + * |
| 25 | + * <p>Implementations provided here are: |
| 26 | + * |
| 27 | + * <ul> |
| 28 | + * <li>{@link #denial()} — keep items that pass <em>all</em> filters (logical AND). |
| 29 | + * <li>{@link #acceptance()} — keep items that pass <em>any</em> filter (logical OR). |
| 30 | + * <li>{@link #acceptAll()} — ignore filters and keep all items. |
| 31 | + * <li>{@link #denyAll()} — ignore filters and keep no items. |
| 32 | + * </ul> |
| 33 | + * |
| 34 | + * <p><strong>Null-handling:</strong> For convenience and safety, all built-in modes treat {@code |
| 35 | + * data == null} as empty and {@code filters == null} or empty as "no filters", which accepts all |
| 36 | + * items (except for {@link #denyAll()}). |
| 37 | + * |
| 38 | + * <p><em>Note:</em> This interface uses private static helper methods (requires Java 9+). |
| 39 | + * |
| 40 | + * @param <T> the element type |
| 41 | + */ |
| 42 | +@FunctionalInterface |
22 | 43 | public interface FilterMode<T> { |
23 | 44 |
|
| 45 | + /** |
| 46 | + * Applies this filter mode to the provided data using the provided filters. |
| 47 | + * |
| 48 | + * @param data the input elements; may be {@code null} (treated as empty) |
| 49 | + * @param filters the filters to evaluate; may be {@code null} or empty |
| 50 | + * @return a new {@link List} containing the elements that satisfy this mode |
| 51 | + * @implNote Implementations should avoid mutating the input collections and should generally |
| 52 | + * preserve input iteration order. |
| 53 | + */ |
24 | 54 | List<T> apply(Collection<T> data, Collection<? extends DataFilter<? super T>> filters); |
25 | 55 |
|
| 56 | + /** |
| 57 | + * A mode that keeps only items which pass <em>all</em> filters (logical AND). |
| 58 | + * |
| 59 | + * <p>Behavior: |
| 60 | + * |
| 61 | + * <ul> |
| 62 | + * <li>If {@code data} is {@code null} or empty → returns an empty list. |
| 63 | + * <li>If {@code filters} is {@code null} or empty → returns a copy of {@code data}. |
| 64 | + * <li>Otherwise → includes an item only if every filter returns {@code true} for it. |
| 65 | + * </ul> |
| 66 | + * |
| 67 | + * @param <T> the element type |
| 68 | + * @return a {@link FilterMode} implementing AND semantics |
| 69 | + */ |
26 | 70 | static <T> FilterMode<T> denial() { |
27 | 71 | return (data, filters) -> { |
28 | 72 | if (data == null || data.isEmpty()) return new ArrayList<>(); |
29 | 73 | if (filters == null || filters.isEmpty()) return new ArrayList<>(data); |
| 74 | + |
30 | 75 | List<T> out = new ArrayList<>(data.size()); |
31 | | - outer: |
32 | 76 | for (T item : data) { |
33 | | - for (DataFilter<? super T> f : filters) { |
34 | | - if (!f.filter(item)) continue outer; |
| 77 | + if (passesAll(item, filters)) { |
| 78 | + out.add(item); |
35 | 79 | } |
36 | | - out.add(item); |
37 | 80 | } |
38 | 81 | return out; |
39 | 82 | }; |
40 | 83 | } |
41 | 84 |
|
| 85 | + /** |
| 86 | + * A mode that keeps items which pass <em>any</em> filter (logical OR). |
| 87 | + * |
| 88 | + * <p>Behavior: |
| 89 | + * |
| 90 | + * <ul> |
| 91 | + * <li>If {@code data} is {@code null} or empty → returns an empty list. |
| 92 | + * <li>If {@code filters} is {@code null} or empty → returns a copy of {@code data}. |
| 93 | + * <li>Otherwise → includes an item if at least one filter returns {@code true} for it. |
| 94 | + * </ul> |
| 95 | + * |
| 96 | + * @param <T> the element type |
| 97 | + * @return a {@link FilterMode} implementing OR semantics |
| 98 | + */ |
42 | 99 | static <T> FilterMode<T> acceptance() { |
43 | 100 | return (data, filters) -> { |
44 | 101 | if (data == null || data.isEmpty()) return new ArrayList<>(); |
45 | 102 | if (filters == null || filters.isEmpty()) return new ArrayList<>(data); |
| 103 | + |
46 | 104 | List<T> out = new ArrayList<>(); |
47 | | - outer: |
48 | 105 | for (T item : data) { |
49 | | - for (DataFilter<? super T> f : filters) { |
50 | | - if (f.filter(item)) { |
51 | | - out.add(item); |
52 | | - continue outer; |
53 | | - } |
| 106 | + if (passesAny(item, filters)) { |
| 107 | + out.add(item); |
54 | 108 | } |
55 | 109 | } |
56 | 110 | return out; |
57 | 111 | }; |
58 | 112 | } |
59 | 113 |
|
60 | | - /** Ignore filters and accept all items */ |
| 114 | + /** |
| 115 | + * A mode that ignores filters and accepts all items. |
| 116 | + * |
| 117 | + * <p>Behavior: |
| 118 | + * |
| 119 | + * <ul> |
| 120 | + * <li>If {@code data} is {@code null} → returns an empty list. |
| 121 | + * <li>Otherwise → returns a copy of {@code data}. |
| 122 | + * </ul> |
| 123 | + * |
| 124 | + * @param <T> the element type |
| 125 | + * @return a {@link FilterMode} that accepts everything |
| 126 | + */ |
61 | 127 | static <T> FilterMode<T> acceptAll() { |
62 | | - return (data, filters) -> new ArrayList<>(data); |
| 128 | + return (data, filters) -> data == null ? new ArrayList<>() : new ArrayList<>(data); |
63 | 129 | } |
64 | 130 |
|
65 | | - /** Ignore filters and deny all items */ |
| 131 | + /** |
| 132 | + * A mode that ignores filters and denies all items. |
| 133 | + * |
| 134 | + * @param <T> the element type |
| 135 | + * @return a {@link FilterMode} that accepts nothing |
| 136 | + */ |
66 | 137 | static <T> FilterMode<T> denyAll() { |
67 | 138 | return (data, filters) -> new ArrayList<>(); |
68 | 139 | } |
| 140 | + |
| 141 | + /** |
| 142 | + * Returns {@code true} if the given {@code item} passes <em>all</em> {@code filters}. |
| 143 | + * |
| 144 | + * @param item the item to test |
| 145 | + * @param filters the filters to evaluate (assumed non-null/non-empty by callers) |
| 146 | + * @param <T> the element type |
| 147 | + * @return {@code true} if every filter returns {@code true} for {@code item} |
| 148 | + */ |
| 149 | + private static <T> boolean passesAll( |
| 150 | + T item, Collection<? extends DataFilter<? super T>> filters) { |
| 151 | + for (DataFilter<? super T> f : filters) { |
| 152 | + if (!f.filter(item)) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + } |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Returns {@code true} if the given {@code item} passes <em>any</em> of the {@code filters}. |
| 161 | + * |
| 162 | + * @param item the item to test |
| 163 | + * @param filters the filters to evaluate (assumed non-null/non-empty by callers) |
| 164 | + * @param <T> the element type |
| 165 | + * @return {@code true} if at least one filter returns {@code true} for {@code item} |
| 166 | + */ |
| 167 | + private static <T> boolean passesAny( |
| 168 | + T item, Collection<? extends DataFilter<? super T>> filters) { |
| 169 | + for (DataFilter<? super T> f : filters) { |
| 170 | + if (f.filter(item)) { |
| 171 | + return true; |
| 172 | + } |
| 173 | + } |
| 174 | + return false; |
| 175 | + } |
69 | 176 | } |
0 commit comments