View Javadoc

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