/
IncludeExcludeSet.java
272 lines (239 loc) · 8.07 KB
/
IncludeExcludeSet.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
/**
* Utility class to maintain a set of inclusions and exclusions.
* <p>Maintains a set of included and excluded elements. The method {@link #test(Object)}
* will return true IFF the passed object is not in the excluded set AND ( either the
* included set is empty OR the object is in the included set)
* <p>The type of the underlying {@link Set} used may be passed into the
* constructor, so special sets like Servlet PathMap may be used.
* <p>
*
* @param <T> The type of element of the set (often a pattern)
* @param <P> The type of the instance passed to the predicate
*/
public class IncludeExcludeSet<T, P> implements Predicate<P>
{
private final Set<T> _includes;
private final Predicate<P> _includePredicate;
private final Set<T> _excludes;
private final Predicate<P> _excludePredicate;
private static class SetContainsPredicate<T> implements Predicate<T>
{
private final Set<T> set;
public SetContainsPredicate(Set<T> set)
{
this.set = set;
}
@Override
public boolean test(T item)
{
return set.contains(item);
}
@Override
public String toString()
{
return "CONTAINS";
}
}
/**
* Default constructor over {@link HashSet}
*/
public IncludeExcludeSet()
{
this(HashSet.class);
}
/**
* Construct an IncludeExclude.
*
* @param setClass The type of {@link Set} to using internally to hold patterns. Two instances will be created.
* one for include patterns and one for exclude patters. If the class is also a {@link Predicate},
* then it is also used as the item test for the set, otherwise a {@link SetContainsPredicate} instance
* is created.
* @param <SET> The type of a set to use as the backing store
*/
public <SET extends Set<T>> IncludeExcludeSet(Class<SET> setClass)
{
try
{
_includes = setClass.getDeclaredConstructor().newInstance();
_excludes = setClass.getDeclaredConstructor().newInstance();
if (_includes instanceof Predicate)
{
_includePredicate = (Predicate<P>)_includes;
}
else
{
_includePredicate = new SetContainsPredicate(_includes);
}
if (_excludes instanceof Predicate)
{
_excludePredicate = (Predicate<P>)_excludes;
}
else
{
_excludePredicate = new SetContainsPredicate(_excludes);
}
}
catch (RuntimeException e)
{
throw e;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
/**
* Construct an IncludeExclude
*
* @param includeSet the Set of items that represent the included space
* @param includePredicate the Predicate for included item testing (null for simple {@link Set#contains(Object)} test)
* @param excludeSet the Set of items that represent the excluded space
* @param excludePredicate the Predicate for excluded item testing (null for simple {@link Set#contains(Object)} test)
* @param <SET> The type of a set to use as the backing store
*/
public <SET extends Set<T>> IncludeExcludeSet(Set<T> includeSet, Predicate<P> includePredicate, Set<T> excludeSet, Predicate<P> excludePredicate)
{
Objects.requireNonNull(includeSet, "Include Set");
Objects.requireNonNull(includePredicate, "Include Predicate");
Objects.requireNonNull(excludeSet, "Exclude Set");
Objects.requireNonNull(excludePredicate, "Exclude Predicate");
_includes = includeSet;
_includePredicate = includePredicate;
_excludes = excludeSet;
_excludePredicate = excludePredicate;
}
public void include(T element)
{
_includes.add(element);
}
public void include(T... element)
{
for (T e : element)
{
_includes.add(e);
}
}
public void exclude(T element)
{
_excludes.add(element);
}
public void exclude(T... element)
{
for (T e : element)
{
_excludes.add(e);
}
}
@Deprecated
public boolean matches(P t)
{
return test(t);
}
@Override
public boolean test(P t)
{
if (!_includes.isEmpty() && !_includePredicate.test(t))
return false;
return !_excludePredicate.test(t);
}
/**
* Test Included and not Excluded
*
* @param item The item to test
* @return Boolean.TRUE if item is included, Boolean.FALSE if item is excluded or null if neither
*/
public Boolean isIncludedAndNotExcluded(P item)
{
if (_excludePredicate.test(item))
return Boolean.FALSE;
if (_includePredicate.test(item))
return Boolean.TRUE;
return null;
}
public boolean hasIncludes()
{
return !_includes.isEmpty();
}
public boolean hasExcludes()
{
return !_excludes.isEmpty();
}
public int size()
{
return _includes.size() + _excludes.size();
}
public Set<T> getIncluded()
{
return _includes;
}
public Set<T> getExcluded()
{
return _excludes;
}
public void clear()
{
_includes.clear();
_excludes.clear();
}
@Override
public String toString()
{
return String.format("%s@%x{i=%s,ip=%s,e=%s,ep=%s}", this.getClass().getSimpleName(), hashCode(),
_includes,
_includePredicate == _includes ? "SELF" : _includePredicate,
_excludes,
_excludePredicate == _excludes ? "SELF" : _excludePredicate);
}
public boolean isEmpty()
{
return _includes.isEmpty() && _excludes.isEmpty();
}
/**
* Match items in combined IncludeExcludeSets.
* @param item1 The item to match against set1
* @param set1 A IncludeExcludeSet to match item1 against
* @param item2 The item to match against set2
* @param set2 A IncludeExcludeSet to match item2 against
* @param <T1> The type of item1
* @param <T2> The type of item2
* @return True IFF <ul>
* <li>Neither item is excluded from their respective sets</li>
* <li>Both sets have no includes OR at least one of the items is included in its respective set</li>
* </ul>
*/
public static <T1, T2> boolean matchCombined(T1 item1, IncludeExcludeSet<?, T1> set1, T2 item2, IncludeExcludeSet<?, T2> set2)
{
Boolean match1 = set1.isIncludedAndNotExcluded(item1);
Boolean match2 = set2.isIncludedAndNotExcluded(item2);
// if we are excluded from either set, then we do not match
if (Boolean.FALSE.equals(match1) || Boolean.FALSE.equals(match2))
return false;
// If either set has any includes, then we must be included by one of them
if (set1.hasIncludes() || set2.hasIncludes())
return Boolean.TRUE.equals(match1) || Boolean.TRUE.equals(match2);
// If not excluded and no includes, then we match
return true;
}
}