use AtomicInteger for Limit/Offset state, plus Validate dog food
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/functor/trunk@1539877 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/core/src/main/java/org/apache/commons/functor/core/Limit.java b/core/src/main/java/org/apache/commons/functor/core/Limit.java
index d57d5d4..17180c9 100644
--- a/core/src/main/java/org/apache/commons/functor/core/Limit.java
+++ b/core/src/main/java/org/apache/commons/functor/core/Limit.java
@@ -17,9 +17,12 @@
*/
package org.apache.commons.functor.core;
+import java.util.concurrent.atomic.AtomicInteger;
+
import org.apache.commons.functor.BinaryPredicate;
import org.apache.commons.functor.NullaryPredicate;
import org.apache.commons.functor.Predicate;
+import org.apache.commons.lang3.Validate;
/**
* A predicate that returns <code>true</code>
@@ -39,26 +42,24 @@
/**
* The current number of times the predicate has been invoked.
*/
- private int current;
+ private final AtomicInteger state = new AtomicInteger();
/**
* Create a new Limit.
* @param count limit
*/
public Limit(int count) {
- if (count < 0) {
- throw new IllegalArgumentException("Argument must be a non-negative integer.");
- }
+ Validate.isTrue(count >= 0, "Argument must be a non-negative integer.");
this.max = count;
}
/**
* {@inheritDoc}
*/
- public synchronized boolean test() {
+ public boolean test() {
// stop incrementing when we've hit max, so we don't loop around
- if (current < max) {
- current++;
+ if (state.get() < max) {
+ state.incrementAndGet();
return true;
}
return false;
diff --git a/core/src/main/java/org/apache/commons/functor/core/Offset.java b/core/src/main/java/org/apache/commons/functor/core/Offset.java
index 2c37bde..93fd2ae 100644
--- a/core/src/main/java/org/apache/commons/functor/core/Offset.java
+++ b/core/src/main/java/org/apache/commons/functor/core/Offset.java
@@ -16,9 +16,12 @@
*/
package org.apache.commons.functor.core;
+import java.util.concurrent.atomic.AtomicInteger;
+
import org.apache.commons.functor.BinaryPredicate;
import org.apache.commons.functor.NullaryPredicate;
import org.apache.commons.functor.Predicate;
+import org.apache.commons.lang3.Validate;
/**
* A predicate that returns <code>false</code>
@@ -39,26 +42,24 @@
/**
* The current number of times the predicate has been invoked.
*/
- private int current;
+ private AtomicInteger state = new AtomicInteger();
/**
* Create a new Offset.
* @param count offset
*/
public Offset(int count) {
- if (count < 0) {
- throw new IllegalArgumentException("Argument must be a non-negative integer.");
- }
+ Validate.isTrue(count >= 0, "Argument must be a non-negative integer.");
this.min = count;
}
/**
* {@inheritDoc}
*/
- public synchronized boolean test() {
+ public boolean test() {
// stop incrementing when we've hit max, so we don't loop around
- if (current < min) {
- current++;
+ if (state.get() < min) {
+ state.incrementAndGet();
return false;
}
return true;