geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblev...@apache.org
Subject svn commit: r1085848 - in /geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder: UrlSet.java filter/Filters.java
Date Sun, 27 Mar 2011 01:13:27 GMT
Author: dblevins
Date: Sun Mar 27 01:13:27 2011
New Revision: 1085848

URL: http://svn.apache.org/viewvc?rev=1085848&view=rev
Log:
Cleaned up the UrlSet so that it now can use a Filter
Some of the internal implementations of filtering were removed and now reuse Filters
Filters.invert(Filter) method added which can flip the meaning of a filter, and safely unflip
it.

Modified:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/filter/Filters.java

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java?rev=1085848&r1=1085847&r2=1085848&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/UrlSet.java Sun
Mar 27 01:13:27 2011
@@ -16,6 +16,10 @@
  */
 package org.apache.xbean.finder;
 
+import org.apache.xbean.finder.filter.Filter;
+import org.apache.xbean.finder.filter.Filters;
+import org.apache.xbean.finder.filter.RegexFilter;
+
 import java.net.URL;
 import java.net.MalformedURLException;
 import java.util.Collection;
@@ -28,6 +32,9 @@ import java.util.Arrays;
 import java.io.IOException;
 import java.io.File;
 
+import static org.apache.xbean.finder.filter.Filters.invert;
+import static org.apache.xbean.finder.filter.Filters.patterns;
+
 /**
  * @version $Rev$ $Date$
  */
@@ -96,7 +103,7 @@ public class UrlSet {
     }
 
     public UrlSet exclude(String pattern) throws MalformedURLException {
-        return exclude(matching(pattern));
+        return filter(invert(patterns(pattern)));
     }
 
     /**
@@ -142,17 +149,21 @@ public class UrlSet {
         return urlSet;
     }
 
-    public UrlSet matching(String pattern) {
+    public UrlSet filter(Filter filter) {
         Map<String, URL> urls = new HashMap<String, URL>();
         for (Map.Entry<String, URL> entry : this.urls.entrySet()) {
             String url = entry.getKey();
-            if (url.matches(pattern)){
+            if (filter.accept(url)){
                 urls.put(url, entry.getValue());
             }
         }
         return new UrlSet(urls);
     }
 
+    public UrlSet matching(String pattern) {
+        return filter(patterns(pattern));
+    }
+
     public UrlSet relative(File file) throws MalformedURLException {
         String urlPath = file.toURI().toURL().toExternalForm();
         Map<String, URL> urls = new HashMap<String, URL>();

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/filter/Filters.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/filter/Filters.java?rev=1085848&r1=1085847&r2=1085848&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/filter/Filters.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/filter/Filters.java
Sun Mar 27 01:13:27 2011
@@ -86,6 +86,26 @@ public class Filters {
         return new FilterList(unwrapped);
     }
 
+    /**
+     * Will invert the meaning of this filter by wrapping it with
+     * a filter that negates the return of the accept method.
+     *
+     * If the passed in filter is already wrapped, it will be
+     * unwrapped and returned.  This is to prevent endless wrapping
+     * if the invert method is called many times.
+     * 
+     * @param filter
+     * @return
+     */
+    public static Filter invert(Filter filter) {
+        if (filter instanceof NegativeFilter) {
+            NegativeFilter negativeFilter = (NegativeFilter) filter;
+            return negativeFilter.getFilter();
+        }
+
+        return new NegativeFilter(filter);
+    }
+
     private static void unwrap(List<Filter> filters, Set<Filter> unwrapped) {
         for (Filter filter : filters) {
             if (filter instanceof FilterList) {
@@ -97,5 +117,21 @@ public class Filters {
         }
     }
 
+    private static final class NegativeFilter implements Filter {
+        private final Filter filter;
+
+        public NegativeFilter(Filter filter) {
+            this.filter = filter;
+        }
+
+        @Override
+        public boolean accept(String name) {
+            return !filter.accept(name);
+        }
+
+        public Filter getFilter() {
+            return filter;
+        }
+    }
 
 }



Mime
View raw message