]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/IncludeExclude.java
updating jetty to jetty-9.2.16.v2016040
[gigi.git] / lib / jetty / org / eclipse / jetty / util / IncludeExclude.java
1 //
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.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.util;
20
21 import java.util.HashSet;
22 import java.util.Set;
23
24
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.
31  * <p>
32  * @param <ITEM> The type of element
33  */
34 public class IncludeExclude<ITEM> 
35 {
36     private final Set<ITEM> _includes;
37     private final Predicate<ITEM> _includePredicate;
38     private final Set<ITEM> _excludes;
39     private final Predicate<ITEM> _excludePredicate;
40     
41     private static class SetContainsPredicate<ITEM> implements Predicate<ITEM>
42     {
43         private final Set<ITEM> set;
44         
45         public SetContainsPredicate(Set<ITEM> set)
46         {
47             this.set = set;
48         }
49         
50         @Override
51         public boolean test(ITEM item)
52         {
53             return set.contains(item);
54         }
55     }
56     
57     /**
58      * Default constructor over {@link HashSet}
59      */
60     public IncludeExclude()
61     {
62         this(HashSet.class);
63     }
64     
65     /**
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}
69      */
70     public <SET extends Set<ITEM>> IncludeExclude(Class<SET> setClass)
71     {
72         try
73         {
74             _includes = setClass.newInstance();
75             _excludes = setClass.newInstance();
76             
77             if(_includes instanceof Predicate) {
78                 _includePredicate = (Predicate<ITEM>)_includes;
79             } else {
80                 _includePredicate = new SetContainsPredicate<>(_includes);
81             }
82             
83             if(_excludes instanceof Predicate) {
84                 _excludePredicate = (Predicate<ITEM>)_excludes;
85             } else {
86                 _excludePredicate = new SetContainsPredicate<>(_excludes);
87             }
88         }
89         catch (InstantiationException | IllegalAccessException e)
90         {
91             throw new RuntimeException(e);
92         }
93     }
94     
95     /**
96      * Construct an IncludeExclude
97      * 
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)
102      */
103     public <SET extends Set<ITEM>> IncludeExclude(Set<ITEM> includeSet, Predicate<ITEM> includePredicate, Set<ITEM> excludeSet, Predicate<ITEM> excludePredicate)
104     {
105         _includes = includeSet;
106         _includePredicate = includePredicate;
107         _excludes = excludeSet;
108         _excludePredicate = excludePredicate;
109     }
110     
111     public void include(ITEM element)
112     {
113         _includes.add(element);
114     }
115     
116     public void include(ITEM... element)
117     {
118         for (ITEM e: element)
119             _includes.add(e);
120     }
121
122     public void exclude(ITEM element)
123     {
124         _excludes.add(element);
125     }
126     
127     public void exclude(ITEM... element)
128     {
129         for (ITEM e: element)
130             _excludes.add(e);
131     }
132     
133     public boolean matches(ITEM e)
134     {
135         if (!_includes.isEmpty() && !_includePredicate.test(e))
136             return false;
137         return !_excludePredicate.test(e);
138     }
139     
140     public int size()
141     {
142         return _includes.size()+_excludes.size();
143     }
144     
145     public Set<ITEM> getIncluded()
146     {
147         return _includes;
148     }
149     
150     public Set<ITEM> getExcluded()
151     {
152         return _excludes;
153     }
154
155     public void clear()
156     {
157         _includes.clear();
158         _excludes.clear();
159     }
160
161     @Override
162     public String toString()
163     {
164         return String.format("%s@%x{i=%s,ip=%s,e=%s,ep=%s}",this.getClass().getSimpleName(),hashCode(),_includes,_includePredicate,_excludes,_excludePredicate);
165     }
166 }