Author: scolebourne
Date: Tue Feb 7 15:10:36 2006
New Revision: 375766
URL: http://svn.apache.org/viewcvs?rev=375766&view=rev
Log:
Add single argument constructor for IfClosure
bug 38495, from Matt Benson
Modified:
jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/ClosureUtils.java
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/IfClosure.java
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestClosureUtils.java
Modified: jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html?rev=375766&r1=375765&r2=375766&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html (original)
+++ jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html Tue Feb 7
15:10:36 2006
@@ -79,6 +79,7 @@
<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>
Modified:
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/ClosureUtils.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/ClosureUtils.java?rev=375766&r1=375765&r2=375766&view=diff
==============================================================================
---
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/ClosureUtils.java
(original)
+++
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/ClosureUtils.java
Tue Feb 7 15:10:36 2006
@@ -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 @@
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
+ * @author Matt Benson
*/
public class ClosureUtils {
@@ -224,6 +225,23 @@
*/
public static Closure chainedClosure(Collection closures) {
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);
}
/**
Modified:
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/IfClosure.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/IfClosure.java?rev=375766&r1=375765&r2=375766&view=diff
==============================================================================
---
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/IfClosure.java
(original)
+++
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/IfClosure.java
Tue Feb 7 15:10:36 2006
@@ -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 @@
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
+ * @author Matt Benson
*/
public class IfClosure implements Closure, Serializable {
@@ -43,6 +44,22 @@
/**
* 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.
*
* @param predicate predicate to switch on
* @param trueClosure closure used if true
@@ -58,6 +75,21 @@
throw new IllegalArgumentException("Closures must not be null");
}
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);
}
/**
Modified:
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestClosureUtils.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestClosureUtils.java?rev=375766&r1=375765&r2=375766&view=diff
==============================================================================
---
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestClosureUtils.java
(original)
+++
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestClosureUtils.java
Tue Feb 7 15:10:36 2006
@@ -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 @@
*/
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 @@
} 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 @@
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");
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]