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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.util.AbstractSet;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.Iterator;
26 import java.util.regex.Pattern;
29 * A Set of Regular expressions strings.
31 * Provides the efficient {@link #matches(String)} method to check for a match against all the combined Regex's
33 public class RegexSet extends AbstractSet<String> implements Predicate<String>
35 private final Set<String> _patterns=new HashSet<String>();
36 private final Set<String> _unmodifiable=Collections.unmodifiableSet(_patterns);
37 private Pattern _pattern;
40 public Iterator<String> iterator()
42 return _unmodifiable.iterator();
48 return _patterns.size();
52 public boolean add(String pattern)
54 boolean added = _patterns.add(pattern);
61 public boolean remove(Object pattern)
63 boolean removed = _patterns.remove(pattern);
71 public boolean isEmpty()
73 return _patterns.isEmpty();
83 private void updatePattern()
85 StringBuilder builder = new StringBuilder();
87 for (String pattern: _patterns)
89 if (builder.length()>2)
92 builder.append(pattern);
96 _pattern = Pattern.compile(builder.toString());
100 public boolean test(String s)
102 return _pattern!=null && _pattern.matcher(s).matches();
105 public boolean matches(String s)
107 return _pattern!=null && _pattern.matcher(s).matches();