2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.util.HashSet;
25 /** Utility class to maintain a set of inclusions and exclusions.
26 * <p>Maintains a set of included and excluded elements. The method {@link #matches(Object)}
27 * will return true IFF the passed object is not in the excluded set AND ( either the
28 * included set is empty OR the object is in the included set)
29 * <p>The type of the underlying {@link Set} used may be passed into the
30 * constructor, so special sets like Servlet PathMap may be used.
32 * @param <ITEM> The type of element
34 public class IncludeExclude<ITEM>
36 private final Set<ITEM> _includes;
37 private final Predicate<ITEM> _includePredicate;
38 private final Set<ITEM> _excludes;
39 private final Predicate<ITEM> _excludePredicate;
41 private static class SetContainsPredicate<ITEM> implements Predicate<ITEM>
43 private final Set<ITEM> set;
45 public SetContainsPredicate(Set<ITEM> set)
51 public boolean test(ITEM item)
53 return set.contains(item);
58 * Default constructor over {@link HashSet}
60 public IncludeExclude()
66 * Construct an IncludeExclude
67 * @param setClass The type of {@link Set} to using internally
68 * @param predicate A predicate function to test if a passed ITEM is matched by the passed SET}
70 public <SET extends Set<ITEM>> IncludeExclude(Class<SET> setClass)
74 _includes = setClass.newInstance();
75 _excludes = setClass.newInstance();
77 if(_includes instanceof Predicate) {
78 _includePredicate = (Predicate<ITEM>)_includes;
80 _includePredicate = new SetContainsPredicate<>(_includes);
83 if(_excludes instanceof Predicate) {
84 _excludePredicate = (Predicate<ITEM>)_excludes;
86 _excludePredicate = new SetContainsPredicate<>(_excludes);
89 catch (InstantiationException | IllegalAccessException e)
91 throw new RuntimeException(e);
96 * Construct an IncludeExclude
98 * @param includeSet the Set of items that represent the included space
99 * @param includePredicate the Predicate for included item testing (null for simple {@link Set#contains(Object)} test)
100 * @param excludeSet the Set of items that represent the excluded space
101 * @param excludePredicate the Predicate for excluded item testing (null for simple {@link Set#contains(Object)} test)
103 public <SET extends Set<ITEM>> IncludeExclude(Set<ITEM> includeSet, Predicate<ITEM> includePredicate, Set<ITEM> excludeSet, Predicate<ITEM> excludePredicate)
105 _includes = includeSet;
106 _includePredicate = includePredicate;
107 _excludes = excludeSet;
108 _excludePredicate = excludePredicate;
111 public void include(ITEM element)
113 _includes.add(element);
116 public void include(ITEM... element)
118 for (ITEM e: element)
122 public void exclude(ITEM element)
124 _excludes.add(element);
127 public void exclude(ITEM... element)
129 for (ITEM e: element)
133 public boolean matches(ITEM e)
135 if (!_includes.isEmpty() && !_includePredicate.test(e))
137 return !_excludePredicate.test(e);
142 return _includes.size()+_excludes.size();
145 public Set<ITEM> getIncluded()
150 public Set<ITEM> getExcluded()
162 public String toString()
164 return String.format("%s@%x{i=%s,ip=%s,e=%s,ep=%s}",this.getClass().getSimpleName(),hashCode(),_includes,_includePredicate,_excludes,_excludePredicate);