Add TransformedPredicate, which transforms before calling a predicate

bug 26946, from Alban Peignier


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131603 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-03-13 16:34:46 +00:00
parent d877995019
commit 78238bdc12
5 changed files with 138 additions and 2 deletions

View File

@ -25,6 +25,7 @@ No interface changes, or deprecations have occurred.
<center><h3>NEW CLASSES</h3></center>
<ul>
<li>[26946] TransformedPredicate - A predicate where the input object is transformed</li>
</ul>
<center><h3>ENHANCEMENTS</h3></center>

View File

@ -183,6 +183,9 @@
<contributor>
<name>Kasper Nielsen</name>
</contributor>
<contributor>
<name>Alban Peignier</name>
</contributor>
<contributor>
<name>Steve Phelps</name>
</contributor>

View File

@ -34,6 +34,7 @@ import org.apache.commons.collections.functors.NullIsTruePredicate;
import org.apache.commons.collections.functors.NullPredicate;
import org.apache.commons.collections.functors.OnePredicate;
import org.apache.commons.collections.functors.OrPredicate;
import org.apache.commons.collections.functors.TransformedPredicate;
import org.apache.commons.collections.functors.TransformerPredicate;
import org.apache.commons.collections.functors.TruePredicate;
import org.apache.commons.collections.functors.UniquePredicate;
@ -63,7 +64,7 @@ import org.apache.commons.collections.functors.UniquePredicate;
* All the supplied predicates are Serializable.
*
* @since Commons Collections 3.0
* @version $Revision: 1.16 $ $Date: 2004/02/18 01:15:42 $
* @version $Revision: 1.17 $ $Date: 2004/03/13 16:34:46 $
*
* @author Stephen Colebourne
* @author Ola Berg
@ -453,4 +454,20 @@ public class PredicateUtils {
return NullIsTruePredicate.getInstance(predicate);
}
// Transformed
//-----------------------------------------------------------------------
/**
* Creates a predicate that transforms the input object before passing it
* to the predicate.
*
* @param transformer the transformer to call first
* @param predicate the predicate to call with the result of the transform
* @return the predicate
* @throws IllegalArgumentException if the transformer or the predicate is null
* @since Commons Collections 3.1
*/
public static Predicate transformedPredicate(Transformer transformer, Predicate predicate) {
return TransformedPredicate.getInstance(transformer, predicate);
}
}

View File

@ -0,0 +1,93 @@
/*
* Copyright 2001-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.functors;
import java.io.Serializable;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.Transformer;
/**
* Predicate implementation that transforms the given object before invoking
* another <code>Predicate</code>.
*
* @since Commons Collections 3.1
* @version $Revision: 1.1 $ $Date: 2004/03/13 16:34:46 $
* @author Alban Peignier
* @author Stephen Colebourne
*/
public final class TransformedPredicate implements Predicate, Serializable {
/** Serial version UID */
static final long serialVersionUID = -5596090919668315834L;
/** The transformer to call */
private final Transformer transformer;
/** The predicate to call */
private final Predicate predicate;
/**
* Factory to create the predicate.
*
* @param transformer the transformer to call
* @param predicate the predicate to call with the result of the transform
* @return the predicate
* @throws IllegalArgumentException if the transformer or the predicate is null
*/
public static Predicate getInstance(Transformer transformer, Predicate predicate) {
if (transformer == null) {
throw new IllegalArgumentException("The transformer to call must not be null");
}
if (predicate == null) {
throw new IllegalArgumentException("The predicate to call must not be null");
}
return new TransformedPredicate(transformer, predicate);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
*/
public TransformedPredicate(Transformer transformer, Predicate predicate) {
this.transformer = transformer;
this.predicate = predicate;
}
/**
* Return the predicate result.
*/
public boolean evaluate(Object object) {
Object result = transformer.transform(object);
return predicate.evaluate(result);
}
/**
* Gets the predicate in use.
* @return the predicate
*/
public Predicate getPredicate() {
return predicate;
}
/**
* Gets the transformer in use.
* @return the transformer
*/
public Transformer getTransformer() {
return transformer;
}
}

View File

@ -18,7 +18,9 @@ package org.apache.commons.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
@ -29,7 +31,7 @@ import junit.textui.TestRunner;
* Tests the org.apache.commons.collections.PredicateUtils class.
*
* @since Commons Collections 3.0
* @version $Revision: 1.7 $ $Date: 2004/02/18 01:20:35 $
* @version $Revision: 1.8 $ $Date: 2004/03/13 16:34:46 $
*
* @author Stephen Colebourne
*/
@ -813,4 +815,24 @@ public class TestPredicateUtils extends junit.framework.TestCase {
fail();
}
// transformed
//------------------------------------------------------------------
public void testTransformedPredicate() {
assertEquals(true, PredicateUtils.transformedPredicate(
TransformerUtils.nopTransformer(),
PredicateUtils.truePredicate()).evaluate(new Object()));
Map map = new HashMap();
map.put(Boolean.TRUE, "Hello");
Transformer t = TransformerUtils.mapTransformer(map);
Predicate p = PredicateUtils.equalPredicate("Hello");
assertEquals(false, PredicateUtils.transformedPredicate(t, p).evaluate(null));
assertEquals(true, PredicateUtils.transformedPredicate(t, p).evaluate(Boolean.TRUE));
try {
PredicateUtils.transformedPredicate(null, null);
fail();
} catch (IllegalArgumentException ex) {}
}
}