Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 31cb28011777f682fec560736af4948cc0e8f563 (plain) (blame)
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
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  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 #matches(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 <ITEM> The type of element
 */
public class IncludeExclude<ITEM> 
{
    private final Set<ITEM> _includes;
    private final Predicate<ITEM> _includePredicate;
    private final Set<ITEM> _excludes;
    private final Predicate<ITEM> _excludePredicate;
    
    private static class SetContainsPredicate<ITEM> implements Predicate<ITEM>
    {
        private final Set<ITEM> set;
        
        public SetContainsPredicate(Set<ITEM> set)
        {
            this.set = set;
        }
        
        @Override
        public boolean test(ITEM item)
        {
            return set.contains(item);
        }
    }
    
    /**
     * Default constructor over {@link HashSet}
     */
    @SuppressWarnings("unchecked")
    public IncludeExclude()
    {
        this(HashSet.class);
    }
    
    /**
     * Construct an IncludeExclude.
     * <p>
     * If the {@link Set} class also implements {@link Predicate}, then that Predicate is
     * used to match against the set, otherwise a simple {@link Set#contains(Object)} test is used.
     * @param setClass The type of {@link Set} to using internally
     * @param <SET> the {@link Set} type
     */
    @SuppressWarnings("unchecked")
    public <SET extends Set<ITEM>> IncludeExclude(Class<SET> setClass)
    {
        try
        {
            _includes = setClass.newInstance();
            _excludes = setClass.newInstance();
            
            if(_includes instanceof Predicate) {
                _includePredicate = (Predicate<ITEM>)_includes;
            } else {
                _includePredicate = new SetContainsPredicate<>(_includes);
            }
            
            if(_excludes instanceof Predicate) {
                _excludePredicate = (Predicate<ITEM>)_excludes;
            } else {
                _excludePredicate = new SetContainsPredicate<>(_excludes);
            }
        }
        catch (InstantiationException | IllegalAccessException 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
     * @param excludeSet the Set of items that represent the excluded space
     * @param excludePredicate the Predicate for excluded item testing
     * @param <SET> the {@link Set} type
     */
    public <SET extends Set<ITEM>> IncludeExclude(Set<ITEM> includeSet, Predicate<ITEM> includePredicate, Set<ITEM> excludeSet, Predicate<ITEM> 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(ITEM element)
    {
        _includes.add(element);
    }
    
    public void include(@SuppressWarnings("unchecked") ITEM... element)
    {
        for (ITEM e: element)
            _includes.add(e);
    }

    public void exclude(ITEM element)
    {
        _excludes.add(element);
    }
    
    public void exclude(@SuppressWarnings("unchecked") ITEM... element)
    {
        for (ITEM e: element)
            _excludes.add(e);
    }
    
    public boolean matches(ITEM e)
    {
        if (!_includes.isEmpty() && !_includePredicate.test(e))
            return false;
        return !_excludePredicate.test(e);
    }
    
    public int size()
    {
        return _includes.size()+_excludes.size();
    }
    
    public Set<ITEM> getIncluded()
    {
        return _includes;
    }
    
    public Set<ITEM> 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,_excludes,_excludePredicate);
    }
}

Back to the top