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.http.pathmap;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.List;
26 import java.util.TreeSet;
28 import org.eclipse.jetty.util.Predicate;
31 * A Set of PathSpec strings.
33 * Used by {@link org.eclipse.jetty.util.IncludeExclude} logic
35 public class PathSpecSet implements Set<String>, Predicate<String>
37 private final Set<PathSpec> specs = new TreeSet<>();
40 public boolean test(String s)
42 for (PathSpec spec : specs)
53 public boolean isEmpty()
55 return specs.isEmpty();
59 public Iterator<String> iterator()
61 return new Iterator<String>()
63 private Iterator<PathSpec> iter = specs.iterator();
66 public boolean hasNext()
68 return iter.hasNext();
74 PathSpec spec = iter.next();
79 return spec.getDeclaration();
85 throw new UnsupportedOperationException("Remove not supported by this Iterator");
97 public boolean contains(Object o)
99 if (o instanceof PathSpec)
101 return specs.contains(o);
103 if (o instanceof String)
105 return specs.contains(toPathSpec((String)o));
110 private PathSpec asPathSpec(Object o)
116 if (o instanceof PathSpec)
120 if (o instanceof String)
122 return toPathSpec((String)o);
124 return toPathSpec(o.toString());
127 private PathSpec toPathSpec(String rawSpec)
129 if ((rawSpec == null) || (rawSpec.length() < 1))
131 throw new RuntimeException("Path Spec String must start with '^', '/', or '*.': got [" + rawSpec + "]");
133 if (rawSpec.charAt(0) == '^')
135 return new RegexPathSpec(rawSpec);
139 return new ServletPathSpec(rawSpec);
144 public Object[] toArray()
146 return toArray(new String[specs.size()]);
150 public <T> T[] toArray(T[] a)
153 for (PathSpec spec : specs)
155 a[i++] = (T)spec.getDeclaration();
161 public boolean add(String e)
163 return specs.add(toPathSpec(e));
167 public boolean remove(Object o)
169 return specs.remove(asPathSpec(o));
173 public boolean containsAll(Collection<?> coll)
175 for (Object o : coll)
177 if (!specs.contains(asPathSpec(o)))
184 public boolean addAll(Collection<? extends String> coll)
188 for (String s : coll)
197 public boolean retainAll(Collection<?> coll)
199 List<PathSpec> collSpecs = new ArrayList<>();
200 for (Object o : coll)
202 collSpecs.add(asPathSpec(o));
204 return specs.retainAll(collSpecs);
208 public boolean removeAll(Collection<?> coll)
210 List<PathSpec> collSpecs = new ArrayList<>();
211 for (Object o : coll)
213 collSpecs.add(asPathSpec(o));
215 return specs.removeAll(collSpecs);