Add single argument constructor for IfClosure

bug 38495, from Matt Benson

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@375766 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-02-07 23:10:36 +00:00
parent 1c8ccc351e
commit c56039ade9
4 changed files with 74 additions and 11 deletions

View File

@ -79,6 +79,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
<li>ListOrderedMap - additional list-like method, setValue(int,Object)</li>
<li>ListOrderedMap - additional method, put(int,Object,Object)</li>
<li>PriorityBuffer - now Serializable [36163]</li>
<li>IfClosure - add single argument constructor [38495]</li>
</ul>
<center><h3>BUG FIXES</h3></center>

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2004 The Apache Software Foundation
* Copyright 2002-2004,2006 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.
@ -51,6 +51,7 @@ import org.apache.commons.collections.functors.WhileClosure;
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matt Benson
*/
public class ClosureUtils {
@ -226,6 +227,23 @@ public class ClosureUtils {
return ChainedClosure.getInstance(closures);
}
/**
* Create a new Closure that calls another closure based on the
* result of the specified predicate.
*
* @see org.apache.commons.collections.functors.IfClosure
*
* @param predicate the validating predicate
* @param trueClosure the closure called if the predicate is true
* @return the <code>if</code> closure
* @throws IllegalArgumentException if the predicate is null
* @throws IllegalArgumentException if the closure is null
* @since Commons Collections 3.2
*/
public static Closure ifClosure(Predicate predicate, Closure trueClosure) {
return IfClosure.getInstance(predicate, trueClosure);
}
/**
* Create a new Closure that calls one of two closures depending
* on the specified predicate.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2004 The Apache Software Foundation
* Copyright 2001-2004,2006 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.
@ -28,6 +28,7 @@ import org.apache.commons.collections.Predicate;
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matt Benson
*/
public class IfClosure implements Closure, Serializable {
@ -41,6 +42,22 @@ public class IfClosure implements Closure, Serializable {
/** The closure to use if false */
private final Closure iFalseClosure;
/**
* Factory method that performs validation.
* <p>
* This factory creates a closure that performs no action when
* the predicate is false.
*
* @param predicate predicate to switch on
* @param trueClosure closure used if true
* @return the <code>if</code> closure
* @throws IllegalArgumentException if either argument is null
* @since Commons Collections 3.2
*/
public static Closure getInstance(Predicate predicate, Closure trueClosure) {
return getInstance(predicate, trueClosure, NOPClosure.INSTANCE);
}
/**
* Factory method that performs validation.
*
@ -60,6 +77,21 @@ public class IfClosure implements Closure, Serializable {
return new IfClosure(predicate, trueClosure, falseClosure);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
* <p>
* This constructor creates a closure that performs no action when
* the predicate is false.
*
* @param predicate predicate to switch on, not null
* @param trueClosure closure used if true, not null
* @since Commons Collections 3.2
*/
public IfClosure(Predicate predicate, Closure trueClosure) {
this(predicate, trueClosure, NOPClosure.INSTANCE);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2004 The Apache Software Foundation
* Copyright 2001-2004,2006 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.
@ -37,9 +37,7 @@ import org.apache.commons.collections.functors.NOPClosure;
*/
public class TestClosureUtils extends junit.framework.TestCase {
private static final Object cObject = new Object();
private static final Object cString = "Hello";
private static final Object cInteger = new Integer(6);
/**
* Construct
@ -244,12 +242,21 @@ public class TestClosureUtils extends junit.framework.TestCase {
} catch (IllegalArgumentException ex) {}
}
// switchClosure
// ifClosure
//------------------------------------------------------------------
public void testSwitchClosure() {
public void testIfClosure() {
MockClosure a = new MockClosure();
MockClosure b = new MockClosure();
MockClosure b = null;
ClosureUtils.ifClosure(PredicateUtils.truePredicate(), a).execute(null);
assertEquals(1, a.count);
a = new MockClosure();
ClosureUtils.ifClosure(PredicateUtils.falsePredicate(), a).execute(null);
assertEquals(0, a.count);
a = new MockClosure();
b = new MockClosure();
ClosureUtils.ifClosure(PredicateUtils.truePredicate(), a, b).execute(null);
assertEquals(1, a.count);
assertEquals(0, b.count);
@ -259,9 +266,14 @@ public class TestClosureUtils extends junit.framework.TestCase {
ClosureUtils.ifClosure(PredicateUtils.falsePredicate(), a, b).execute(null);
assertEquals(0, a.count);
assertEquals(1, b.count);
a = new MockClosure();
b = new MockClosure();
}
// switchClosure
//------------------------------------------------------------------
public void testSwitchClosure() {
MockClosure a = new MockClosure();
MockClosure b = new MockClosure();
ClosureUtils.switchClosure(
new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")},
new Closure[] {a, b}).execute("WELL");