You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/10/20 21:26:32 UTC

[commons-beanutils] branch master updated: Convert Collections4 to java.util.function (#8)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-beanutils.git


The following commit(s) were added to refs/heads/master by this push:
     new 2ad4ad3  Convert Collections4 to java.util.function (#8)
2ad4ad3 is described below

commit 2ad4ad3c9ca4aa8840a15c7506ff73c0876dc9a3
Author: Melloware <me...@gmail.com>
AuthorDate: Sun Oct 20 17:26:25 2019 -0400

    Convert Collections4 to java.util.function (#8)
    
    * BeanPredicate switched from CommonsCollections to java.util.function.Predicate.
    
    * BeanPredicate switched from CommonsCollections to java.util.function.Predicate.
---
 .../apache/commons/beanutils2/BeanPredicate.java   | 33 ++++++++++++++++------
 .../commons/beanutils2/BeanPredicateTestCase.java  | 23 ++++++++-------
 2 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/src/main/java/org/apache/commons/beanutils2/BeanPredicate.java b/src/main/java/org/apache/commons/beanutils2/BeanPredicate.java
index 3583d28..934cf5e 100644
--- a/src/main/java/org/apache/commons/beanutils2/BeanPredicate.java
+++ b/src/main/java/org/apache/commons/beanutils2/BeanPredicate.java
@@ -18,8 +18,8 @@
 package org.apache.commons.beanutils2;
 
 import java.lang.reflect.InvocationTargetException;
+import java.util.function.Predicate;
 
-import org.apache.commons.collections4.Predicate;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -29,14 +29,14 @@ import org.apache.commons.logging.LogFactory;
  * </p>
  *
  */
-public class BeanPredicate implements Predicate {
+public class BeanPredicate implements Predicate<Object> {
 
     private final Log log = LogFactory.getLog(this.getClass());
 
     /** Name of the property whose value will be predicated */
     private String propertyName;
     /** <code>Predicate</code> to be applied to the property value */
-    private Predicate predicate;
+    private Predicate<Object> predicate;
 
     /**
      * Constructs a <code>BeanPredicate</code> that applies the given
@@ -50,6 +50,18 @@ public class BeanPredicate implements Predicate {
         this.propertyName = propertyName;
         this.predicate = predicate;
     }
+    
+    /**
+     * Evaluates the given object by applying the {@link #getPredicate()}
+     * to a property value named by {@link #getPropertyName()}.
+     *
+     * @param object The object being evaluated
+     * @return the result of the predicate evaluation
+     * @throws IllegalArgumentException when the property cannot be evaluated
+     */
+    public boolean evaluate(Object object) {
+        return test(object);
+    }
 
     /**
      * Evaluates the given object by applying the {@link #getPredicate()}
@@ -60,13 +72,12 @@ public class BeanPredicate implements Predicate {
      * @throws IllegalArgumentException when the property cannot be evaluated
      */
     @Override
-    public boolean evaluate(final Object object) {
-
+    public boolean test(Object object) {
         boolean evaluation = false;
 
         try {
             final Object propValue = PropertyUtils.getProperty( object, propertyName );
-            evaluation = predicate.evaluate(propValue);
+            evaluation = predicate.test(propValue);
         } catch (final IllegalArgumentException e) {
             final String errorMsg = "Problem during evaluation.";
             log.error("ERROR: " + errorMsg, e);
@@ -87,7 +98,7 @@ public class BeanPredicate implements Predicate {
 
         return evaluation;
     }
-
+    
     /**
      * Gets the name of the property whose value is to be predicated.
      * in the evaluation.
@@ -111,7 +122,7 @@ public class BeanPredicate implements Predicate {
      * during {@link #evaluate}.
      * @return <code>Predicate</code>, not null
      */
-    public Predicate getPredicate() {
+    public Predicate<Object> getPredicate() {
         return predicate;
     }
 
@@ -120,8 +131,12 @@ public class BeanPredicate implements Predicate {
      * during {@link #evaluate(Object)}.
      * @param predicate <code>Predicate</code>, not null
      */
-    public void setPredicate(final Predicate predicate) {
+    public void setPredicate(final Predicate<Object> predicate) {
         this.predicate = predicate;
     }
 
+
+
+
+
 }
diff --git a/src/test/java/org/apache/commons/beanutils2/BeanPredicateTestCase.java b/src/test/java/org/apache/commons/beanutils2/BeanPredicateTestCase.java
index 8025c72..e361195 100644
--- a/src/test/java/org/apache/commons/beanutils2/BeanPredicateTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/BeanPredicateTestCase.java
@@ -17,14 +17,13 @@
 
 package org.apache.commons.beanutils2;
 
-import org.apache.commons.collections4.functors.EqualPredicate;
-import org.apache.commons.collections4.functors.InstanceofPredicate;
-import org.apache.commons.collections4.functors.NotPredicate;
-import org.apache.commons.collections4.functors.NullPredicate;
+import java.util.function.Predicate;
 
 import junit.framework.TestCase;
 
 /**
+ * Unit test for {@link BeanPredicate}
+ *
  */
 public class BeanPredicateTestCase extends TestCase {
 
@@ -33,29 +32,29 @@ public class BeanPredicateTestCase extends TestCase {
     }
 
     public void testEqual() {
-        final BeanPredicate predicate =
-            new BeanPredicate("stringProperty",new EqualPredicate("foo"));
+        Predicate<String> p = (s) -> s.equals("foo");
+        final BeanPredicate predicate = new BeanPredicate("stringProperty", p);
         assertTrue(predicate.evaluate(new TestBean("foo")));
         assertTrue(!predicate.evaluate(new TestBean("bar")));
     }
 
     public void testNotEqual() {
-        final BeanPredicate predicate =
-            new BeanPredicate("stringProperty",new NotPredicate( new EqualPredicate("foo")));
+        Predicate<String> p = (s) -> !s.equals("foo");
+        final BeanPredicate predicate = new BeanPredicate("stringProperty", p);
         assertTrue(!predicate.evaluate(new TestBean("foo")));
         assertTrue(predicate.evaluate(new TestBean("bar")));
     }
 
     public void testInstanceOf() {
-        final BeanPredicate predicate =
-            new BeanPredicate("stringProperty",new InstanceofPredicate( String.class ));
+        Predicate<String> p = (s) -> (s instanceof String);
+        final BeanPredicate predicate = new BeanPredicate("stringProperty", p);
         assertTrue(predicate.evaluate(new TestBean("foo")));
         assertTrue(predicate.evaluate(new TestBean("bar")));
     }
 
     public void testNull() {
-        final BeanPredicate predicate =
-            new BeanPredicate("stringProperty", NullPredicate.INSTANCE);
+        Predicate<String> p = (s) -> s == null;
+        final BeanPredicate predicate = new BeanPredicate("stringProperty", p);
         final String nullString = null;
         assertTrue(predicate.evaluate(new TestBean(nullString)));
         assertTrue(!predicate.evaluate(new TestBean("bar")));