Permalink
Browse files

Merge concept of AndFilter and RefineFilter. AndFilter was a generic …

…mess, RefineFilter did 'the right thing', but And filter should have been done the way that RefineFilter was done. So, copy guts of RefineFilter to AndFilter, and delete RefineFilter. Fix up tests. Fix up JavaDoc
  • Loading branch information...
1 parent 9e45b99 commit a9ec482b76014eff6ad6c705e40256d020a5a8b6 @rolfl rolfl committed Mar 15, 2012
View
9 core/src/java/org/jdom2/filter/AbstractFilter.java
@@ -1,6 +1,6 @@
/*--
- Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
+ Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -66,6 +66,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* Partial implementation of {@link Filter}.
*
* @author Bradley S. Huffman
+ * @author Rolf Lear
* @param <T> The Generic type of content returned by this Filter
*/
public abstract class AbstractFilter<T> implements Filter<T> {
@@ -126,12 +127,12 @@ public final boolean matches(Object content) {
}
@Override
- public final Filter<?> and(Filter<?> filter) {
- return new AndFilter<Object>(this, filter);
+ public final Filter<T> and(Filter<?> filter) {
+ return new AndFilter<T>(filter, this);
}
@Override
public <R> Filter<R> refine(Filter<R> filter) {
- return new RefineFilter<R>(this, filter);
+ return new AndFilter<R>(this, filter);
}
}
View
63 core/src/java/org/jdom2/filter/AndFilter.java
@@ -1,6 +1,6 @@
/*--
- Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
+ Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -60,6 +60,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* <b>and</b> operation.
*
* @author Bradley S. Huffman
+ * @author Rolf Lear
* @param <T> The Generic type of content returned by this Filter
*/
final class AndFilter<T> extends AbstractFilter<T> {
@@ -69,65 +70,53 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*/
private static final long serialVersionUID = 200L;
- // Filter for left side of logical <b>and</b>.
- private final Filter<? extends T> left;
+ private final Filter<?> base;
+ private final Filter<T> refiner;
- // Filter for right side of logical <b>and</b>.
- private final Filter<? extends T> right;
-
- /**
- * Match if only both supplied filters match.
- *
- * @param left left side of logical <b>and</b>
- * @param right right side of logical <b>and</b>
- * @throws IllegalArgumentException if either supplied filter is null
- */
- public AndFilter(Filter<? extends T> left, Filter<? extends T> right) {
- if ((left == null) || (right == null)) {
- throw new IllegalArgumentException("null filter not allowed");
+ public AndFilter(Filter<?> base, Filter<T> refiner) {
+ if (base == null || refiner == null) {
+ throw new NullPointerException("Cannot have a null base or refiner filter");
}
- this.left = left;
- this.right = right;
+ this.base = base;
+ this.refiner = refiner;
}
@Override
public T filter(Object content) {
- T ret = left.filter(content);
- if (ret != null) {
- return right.filter(ret);
+ Object o = base.filter(content);
+ if (o != null) {
+ return refiner.filter(content);
}
return null;
}
-
+
+ @Override
+ public int hashCode() {
+ return base.hashCode() ^ refiner.hashCode();
+ }
+
@Override
public boolean equals(Object obj) {
- if (this == obj) {
+ if (obj == this) {
return true;
}
-
- if (obj instanceof AndFilter) {
- AndFilter<?> filter = (AndFilter<?>) obj;
- if ((left.equals(filter.left) && right.equals(filter.right)) ||
- (left.equals(filter.right) && right.equals(filter.left))) {
- return true;
- }
+ if (obj instanceof AndFilter<?>) {
+ AndFilter<?> them = (AndFilter<?>)obj;
+ return (base.equals(them.base) && refiner.equals(them.refiner)) ||
+ (refiner.equals(them.base) && base.equals(them.refiner));
}
+
return false;
}
@Override
- public int hashCode() {
- return (left.hashCode()) ^ right.hashCode();
- }
-
- @Override
public String toString() {
return new StringBuilder(64)
.append("[AndFilter: ")
- .append(left.toString())
+ .append(base.toString())
.append(",\n")
.append(" ")
- .append(right.toString())
+ .append(refiner.toString())
.append("]")
.toString();
}
View
2 core/src/java/org/jdom2/filter/AttributeFilter.java
@@ -1,6 +1,6 @@
/*--
- Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
+ Copyright (C) 2011-2012 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
View
2 core/src/java/org/jdom2/filter/ContentFilter.java
@@ -1,6 +1,6 @@
/*--
- Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
+ Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
View
2 core/src/java/org/jdom2/filter/ElementFilter.java
@@ -1,6 +1,6 @@
/*--
- Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
+ Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
View
14 core/src/java/org/jdom2/filter/Filter.java
@@ -1,6 +1,6 @@
/*--
- Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
+ Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -62,13 +62,15 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*
* @author Jools Enticknap
* @author Bradley S. Huffman
+ * @author Rolf Lear
* @param <T> The Generic type of content returned by this Filter
*/
public interface Filter <T> extends java.io.Serializable {
/**
- * Filter the input list of all content except that which matches the Filter.
+ * Filter the input list keeping only the items that match the Filter.
+ *
* @param content The content to filter.
* @return a new read-only RandomAccess list of the filtered input content.
*/
@@ -109,17 +111,19 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
public Filter<? extends Object> or(Filter<?> filter);
/**
- * Creates an ANDing filter
+ * Creates an ANDing filter. The generic type of the result is the same as
+ * this Filter.
+ *
* @param filter a second Filter to AND with.
* @return a new Filter instance that returns the 'intersection' of this
* filter and the specified filter.
*/
- public Filter<?> and(Filter<?> filter);
+ public Filter<T> and(Filter<?> filter);
/**
* This is similar to the and(Filter) method except the generic type is
* different.
- * @param <R> The Generic type of the retuned data is taken from the input
+ * @param <R> The Generic type of the returned data is taken from the input
* instance.
* @param filter The filter to refine our results with.
* @return A Filter that requires content to both match our instance and the
View
2 core/src/java/org/jdom2/filter/NegateFilter.java
@@ -1,6 +1,6 @@
/*--
- Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
+ Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
View
2 core/src/java/org/jdom2/filter/OrFilter.java
@@ -1,6 +1,6 @@
/*--
- Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
+ Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
View
9 core/src/java/org/jdom2/filter/PassThroughFilter.java
@@ -52,14 +52,21 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*/
- package org.jdom2.filter;
+package org.jdom2.filter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.RandomAccess;
+/**
+ * A filter that does not actual filtering.
+ * This is made available through {@link Filters#fpassthrough()}.
+ *
+ * @author Rolf Lear
+ *
+ */
final class PassThroughFilter extends AbstractFilter<Object> {
/**
* JDOM2 Serialization: Default mechanism
View
117 core/src/java/org/jdom2/filter/RefineFilter.java
@@ -1,117 +0,0 @@
-/*--
-
- Copyright (C) 2011-2012 Jason Hunter & Brett McLaughlin.
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
-
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions, and the following disclaimer.
-
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions, and the disclaimer that follows
- these conditions in the documentation and/or other materials
- provided with the distribution.
-
- 3. The name "JDOM" must not be used to endorse or promote products
- derived from this software without prior written permission. For
- written permission, please contact <request_AT_jdom_DOT_org>.
-
- 4. Products derived from this software may not be called "JDOM", nor
- may "JDOM" appear in their name, without prior written permission
- from the JDOM Project Management <request_AT_jdom_DOT_org>.
-
- In addition, we request (but do not require) that you include in the
- end-user documentation provided with the redistribution and/or in the
- software itself an acknowledgement equivalent to the following:
- "This product includes software developed by the
- JDOM Project (http://www.jdom.org/)."
- Alternatively, the acknowledgment may be graphical using the logos
- available at http://www.jdom.org/images/logos.
-
- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- SUCH DAMAGE.
-
- This software consists of voluntary contributions made by many
- individuals on behalf of the JDOM Project and was originally
- created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
- Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
- on the JDOM Project, please see <http://www.jdom.org/>.
-
- */
-
-package org.jdom2.filter;
-
-/**
- * Combines two Filters in sequence. This is similar to AndFilter, but differs
- * in the way it deals with the Generic types of the arguments. It only
- * returns the results of the second filter, thus all output is in the generic
- * type of that second Filter instance.
- *
- * @author Rolf Lear
- *
- * @param <T> The generic type of the refining filter.
- */
-final class RefineFilter<T> extends AbstractFilter<T> {
-
- /**
- * JDOM2 Serialization: Default mechanism
- */
- private static final long serialVersionUID = 200L;
-
- private final Filter<?> base;
- private final Filter<T> refiner;
-
- public RefineFilter(Filter<?> base, Filter<T> refiner) {
- if (base == null || refiner == null) {
- throw new NullPointerException("Cannot have a null base or refiner filter");
- }
- this.base = base;
- this.refiner = refiner;
- }
-
- @Override
- public T filter(Object content) {
- Object o = base.filter(content);
- if (o != null) {
- return refiner.filter(content);
- }
- return null;
- }
-
- @Override
- public int hashCode() {
- return base.hashCode() ^ (~refiner.hashCode());
- }
-
- @Override
- public boolean equals(Object obj) {
- if (obj == this) {
- return true;
- }
- if (obj instanceof RefineFilter<?>) {
- RefineFilter<?> them = (RefineFilter<?>)obj;
- return base.equals(them.base) && refiner.equals(them.refiner);
- }
-
- return false;
- }
-
- @Override
- public String toString() {
- return "[RefinerFilter: Refine " + base + " with " + refiner + "]";
- }
-
-}
View
55 core/src/java/org/jdom2/filter/TextOnlyFilter.java
@@ -1,10 +1,65 @@
+/*--
+
+Copyright (C) 2012 Jason Hunter & Brett McLaughlin.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions, and the disclaimer that follows
+ these conditions in the documentation and/or other materials
+ provided with the distribution.
+
+ 3. The name "JDOM" must not be used to endorse or promote products
+ derived from this software without prior written permission. For
+ written permission, please contact <request_AT_jdom_DOT_org>.
+
+ 4. Products derived from this software may not be called "JDOM", nor
+ may "JDOM" appear in their name, without prior written permission
+ from the JDOM Project Management <request_AT_jdom_DOT_org>.
+
+ In addition, we request (but do not require) that you include in the
+ end-user documentation provided with the redistribution and/or in the
+ software itself an acknowledgement equivalent to the following:
+ "This product includes software developed by the
+ JDOM Project (http://www.jdom.org/)."
+ Alternatively, the acknowledgment may be graphical using the logos
+ available at http://www.jdom.org/images/logos.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many
+ individuals on behalf of the JDOM Project and was originally
+ created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
+ Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
+ on the JDOM Project, please see <http://www.jdom.org/>.
+
+ */
+
package org.jdom2.filter;
import org.jdom2.Text;
import org.jdom2.Content.CType;
/**
* A filter that matches Text, but not CDATA content.
+ * This filter is made accessible through {@link Filters#textOnly()}
*
* @author Rolf Lear
*
View
3 core/src/java/org/jdom2/filter/package.html
@@ -5,6 +5,7 @@
getContent(Filter) and getDescendants(Filter). Filters are also used
extensively in the XPath API. The <code>Filters</code> class provides access
to a large number of useful filters, and also a sampling of generally useful
- filters is provided here. Alternate filters can be user defined.
+ filters is provided here. Additional filters can be user defined, and that
+ is made easier by extending thr AbstractFilter class.
</body>
View
8 test/src/java/org/jdom2/test/cases/filter/AbstractTestFilter.java
@@ -262,7 +262,7 @@ protected Namespace getTestNamespace() {
}
try {
- Filter<?> and = af.and(null);
+ Filter<F> and = af.and(null);
fail ("expected an exception from " + and);
} catch (RuntimeException re) {
// good
@@ -287,7 +287,7 @@ protected Namespace getTestNamespace() {
Filter<?> bfor = nf.or(af);
assertFilterEquals(afor, bfor);
- Filter<?> afand = UnitTestUtil.deSerialize(af).and(nf);
+ Filter<F> afand = UnitTestUtil.deSerialize(af).and(nf);
Filter<?> bfand = nf.and(af);
assertFilterEquals(afand, bfand);
@@ -392,7 +392,7 @@ private final void exerciseCore(Filter<? extends Content> ef, Parent parent, Cal
}
try {
- Filter<?> and = af.and(null);
+ Filter<F> and = af.and(null);
fail ("expected an exception from " + and);
} catch (RuntimeException re) {
// good
@@ -417,7 +417,7 @@ private final void exerciseCore(Filter<? extends Content> ef, Parent parent, Cal
Filter<?> bfor = nf.or(af);
assertFilterEquals(afor, bfor);
- Filter<?> afand = UnitTestUtil.deSerialize(af).and(nf);
+ Filter<F> afand = UnitTestUtil.deSerialize(af).and(nf);
Filter<?> bfand = nf.and(af);
assertFilterEquals(afand, bfand);

0 comments on commit a9ec482

Please sign in to comment.