]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/RegexSet.java
Merge "Update notes about password security"
[gigi.git] / lib / jetty / org / eclipse / jetty / util / RegexSet.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.AbstractSet;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.Set;
26 import java.util.regex.Pattern;
27
28 /**
29  * A Set of Regular expressions strings.
30  * <p>
31  * Provides the efficient {@link #matches(String)} method to check for a match against all the combined Regex's
32  */
33 public class RegexSet extends AbstractSet<String> implements Predicate<String>
34 {
35     private final Set<String> _patterns=new HashSet<String>();
36     private final Set<String> _unmodifiable=Collections.unmodifiableSet(_patterns);
37     private Pattern _pattern;
38     
39     @Override
40     public Iterator<String> iterator()
41     {
42         return _unmodifiable.iterator();
43     }
44
45     @Override
46     public int size()
47     {
48         return _patterns.size();
49     }
50     
51     @Override
52     public boolean add(String pattern)
53     {
54         boolean added = _patterns.add(pattern);
55         if (added)
56             updatePattern();
57         return added;
58     }
59     
60     @Override
61     public boolean remove(Object pattern)
62     {
63         boolean removed = _patterns.remove(pattern);
64
65         if (removed)
66             updatePattern();
67         return removed;
68     }
69
70     @Override
71     public boolean isEmpty()
72     {
73         return _patterns.isEmpty();
74     }
75
76     @Override
77     public void clear()
78     {
79         _patterns.clear();
80         _pattern=null;
81     }
82
83     private void updatePattern()
84     {
85         StringBuilder builder = new StringBuilder();
86         builder.append("^(");
87         for (String pattern: _patterns)
88         {
89             if (builder.length()>2)
90                 builder.append('|');
91             builder.append('(');
92             builder.append(pattern);
93             builder.append(')');
94         }
95         builder.append(")$");
96         _pattern = Pattern.compile(builder.toString());   
97     }
98     
99     @Override
100     public boolean test(String s)
101     {
102         return _pattern!=null && _pattern.matcher(s).matches();
103     }
104
105     public boolean matches(String s)
106     {
107         return _pattern!=null && _pattern.matcher(s).matches();
108     }
109 }