You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2013/11/08 01:20:07 UTC
svn commit: r1539877 - in
/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core:
Limit.java Offset.java
Author: mbenson
Date: Fri Nov 8 00:20:07 2013
New Revision: 1539877
URL: http://svn.apache.org/r1539877
Log:
use AtomicInteger for Limit/Offset state, plus Validate dog food
Modified:
commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/Limit.java
commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/Offset.java
Modified: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/Limit.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/Limit.java?rev=1539877&r1=1539876&r2=1539877&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/Limit.java (original)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/Limit.java Fri Nov 8 00:20:07 2013
@@ -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 @@ public final class Limit implements Null
/**
* 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;
Modified: commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/Offset.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/Offset.java?rev=1539877&r1=1539876&r2=1539877&view=diff
==============================================================================
--- commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/Offset.java (original)
+++ commons/proper/functor/trunk/core/src/main/java/org/apache/commons/functor/core/Offset.java Fri Nov 8 00:20:07 2013
@@ -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 @@ public final class Offset implements Nul
/**
* 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;