View Javadoc

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