Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Vosburgh2015-10-16 17:08:20 -0400
committerBrian Vosburgh2015-10-16 17:08:20 -0400
commit806185961a09d34efb657ee8ec3938d9ee38818d (patch)
tree9b5447ff5e07534b2f59b9c2725a157caf8fb07e
parentaff96c7056f450e7a378fe7592c5fc343b0c8536 (diff)
downloadwebtools.dali-806185961a09d34efb657ee8ec3938d9ee38818d.tar.gz
webtools.dali-806185961a09d34efb657ee8ec3938d9ee38818d.tar.xz
webtools.dali-806185961a09d34efb657ee8ec3938d9ee38818d.zip
rework IntReferences
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractIntReference.java90
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractModifiableIntReference.java203
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SimpleIntReference.java220
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SynchronizedInt.java767
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/reference/IntReference.java70
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/reference/ModifiableIntReference.java154
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractIntReferenceTests.java77
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/JptCommonUtilityReferenceTests.java1
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SimpleIntReferenceTests.java22
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedIntTests.java8
10 files changed, 1163 insertions, 449 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractIntReference.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractIntReference.java
index de42987acc..00f7789449 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractIntReference.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractIntReference.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2015 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -9,6 +9,7 @@
******************************************************************************/
package org.eclipse.jpt.common.utility.internal.reference;
+import org.eclipse.jpt.common.utility.predicate.IntPredicate;
import org.eclipse.jpt.common.utility.reference.IntReference;
/**
@@ -25,12 +26,12 @@ public abstract class AbstractIntReference
super();
}
- public boolean equals(int v) {
- return this.getValue() == v;
+ public boolean equals(int i) {
+ return this.getValue() == i;
}
- public boolean notEqual(int v) {
- return this.getValue() != v;
+ public boolean notEqual(int i) {
+ return this.getValue() != i;
}
public boolean isZero() {
@@ -41,20 +42,20 @@ public abstract class AbstractIntReference
return this.getValue() != 0;
}
- public boolean isGreaterThan(int v) {
- return this.getValue() > v;
+ public boolean isGreaterThan(int i) {
+ return this.getValue() > i;
}
- public boolean isGreaterThanOrEqual(int v) {
- return this.getValue() >= v;
+ public boolean isGreaterThanOrEqual(int i) {
+ return this.getValue() >= i;
}
- public boolean isLessThan(int v) {
- return this.getValue() < v;
+ public boolean isLessThan(int i) {
+ return this.getValue() < i;
}
- public boolean isLessThanOrEqual(int v) {
- return this.getValue() <= v;
+ public boolean isLessThanOrEqual(int i) {
+ return this.getValue() <= i;
}
public boolean isPositive() {
@@ -73,57 +74,46 @@ public abstract class AbstractIntReference
return this.isGreaterThanOrEqual(0);
}
- public int abs() {
- return Math.abs(this.getValue());
+ public boolean isMemberOf(IntPredicate predicate) {
+ return predicate.evaluate(this.getValue());
}
- public int neg() {
- return -this.getValue();
+ public boolean isNotMemberOf(IntPredicate predicate) {
+ return ! this.isMemberOf(predicate);
}
- public int add(int v) {
- return this.getValue() + v;
- }
-
- public int subtract(int v) {
- return this.getValue() - v;
- }
-
- public int multiply(int v) {
- return this.getValue() * v;
- }
- public int divide(int v) {
- return this.getValue() / v;
- }
+ // ********** Comparable **********
- public int remainder(int v) {
- return this.getValue() % v;
+ public int compareTo(IntReference other) {
+ int thisValue = this.getValue();
+ int otherValue = other.getValue();
+ return (thisValue < otherValue) ? -1 : ((thisValue == otherValue) ? 0 : 1);
}
- public int min(int v) {
- return Math.min(this.getValue(), v);
- }
- public int max(int v) {
- return Math.max(this.getValue(), v);
- }
+ // ********** standard methods **********
- public double pow(int v) {
- return Math.pow(this.getValue(), v);
+ /**
+ * Object identity is critical to int references.
+ * There is no reason for two different int references to be
+ * <em>equal</em>.
+ *
+ * @see #equals(int)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ return super.equals(obj);
}
-
- // ********** Comparable implementation **********
-
- public int compareTo(IntReference ref) {
- int v = ref.getValue();
- return (this.getValue() < v) ? -1 : ((this.getValue() == v) ? 0 : 1);
+ /**
+ * @see #equals(Object)
+ */
+ @Override
+ public int hashCode() {
+ return super.hashCode();
}
-
- // ********** standard methods **********
-
@Override
public String toString() {
return '[' + String.valueOf(this.getValue()) + ']';
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractModifiableIntReference.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractModifiableIntReference.java
new file mode 100644
index 0000000000..34593085eb
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractModifiableIntReference.java
@@ -0,0 +1,203 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2015 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0, which accompanies this distribution
+ * and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.reference;
+
+import org.eclipse.jpt.common.utility.internal.BitTools;
+import org.eclipse.jpt.common.utility.reference.ModifiableIntReference;
+
+/**
+ * Implement some of the methods in {@link ModifiableIntReference} that can
+ * be defined in terms of the other methods.
+ * Subclasses need only implement<ul>
+ * <li>{@link #getValue()}
+ * <li>{@link #setValue(int)}
+ * </ul>
+ */
+public abstract class AbstractModifiableIntReference
+ extends AbstractIntReference
+ implements ModifiableIntReference
+{
+ protected AbstractModifiableIntReference() {
+ super();
+ }
+
+ public int setZero() {
+ return this.setValue(0);
+ }
+
+ public int increment() {
+ int v = this.getValue();
+ v++;
+ this.setValue(v);
+ return v;
+ }
+
+ public int incrementExact() {
+ int v = this.getValue();
+ v = Math.incrementExact(v);
+ this.setValue(v);
+ return v;
+ }
+
+ public int decrement() {
+ int v = this.getValue();
+ v--;
+ this.setValue(v);
+ return v;
+ }
+
+ public int decrementExact() {
+ int v = this.getValue();
+ v = Math.decrementExact(v);
+ this.setValue(v);
+ return v;
+ }
+
+ public int halve() {
+ int v = this.getValue();
+ v = BitTools.half(v);
+ this.setValue(v);
+ return v;
+ }
+
+ public int twice() {
+ int v = this.getValue();
+ v = BitTools.twice(v);
+ this.setValue(v);
+ return v;
+ }
+
+ public int twiceExact() {
+ int v = this.getValue();
+ v = Math.multiplyExact(v, 2);
+ this.setValue(v);
+ return v;
+ }
+
+ public int abs() {
+ int v = this.getValue();
+ v = Math.abs(v);
+ this.setValue(v);
+ return v;
+ }
+
+ public int negate() {
+ int v = this.getValue();
+ v = -v;
+ this.setValue(v);
+ return v;
+ }
+
+ public int negateExact() {
+ int v = this.getValue();
+ v = Math.negateExact(v);
+ this.setValue(v);
+ return v;
+ }
+
+ public int add(int i) {
+ int v = this.getValue();
+ v += i;
+ this.setValue(v);
+ return v;
+ }
+
+ public int addExact(int i) {
+ int v = this.getValue();
+ v = Math.addExact(v, i);
+ this.setValue(v);
+ return v;
+ }
+
+ public int subtract(int i) {
+ int v = this.getValue();
+ v -= i;
+ this.setValue(v);
+ return v;
+ }
+
+ public int subtractExact(int i) {
+ int v = this.getValue();
+ v = Math.subtractExact(v, i);
+ this.setValue(v);
+ return v;
+ }
+
+ public int multiply(int i) {
+ int v = this.getValue();
+ v = v * i;
+ this.setValue(v);
+ return v;
+ }
+
+ public int multiplyExact(int i) {
+ int v = this.getValue();
+ v = Math.multiplyExact(v, i);
+ this.setValue(v);
+ return v;
+ }
+
+ public int divide(int i) {
+ int v = this.getValue();
+ v = v / i;
+ this.setValue(v);
+ return v;
+ }
+
+ public int floorDivide(int i) {
+ int v = this.getValue();
+ v = Math.floorDiv(v, i);
+ this.setValue(v);
+ return v;
+ }
+
+ public int remainder(int i) {
+ int v = this.getValue();
+ v = v % i;
+ this.setValue(v);
+ return v;
+ }
+
+ public int floorRemainder(int i) {
+ int v = this.getValue();
+ v = Math.floorMod(v, i);
+ this.setValue(v);
+ return v;
+ }
+
+ public int min(int i) {
+ int v = this.getValue();
+ v = Math.min(v, i);
+ this.setValue(v);
+ return v;
+ }
+
+ public int max(int i) {
+ int v = this.getValue();
+ v = Math.max(v, i);
+ this.setValue(v);
+ return v;
+ }
+
+ public boolean commit(int newValue, int expectedValue) {
+ if (this.getValue() == expectedValue) {
+ this.setValue(newValue);
+ return true;
+ }
+ return false;
+ }
+
+ public int swap(ModifiableIntReference other) {
+ int otherValue = other.getValue();
+ other.setValue(this.getValue());
+ this.setValue(otherValue);
+ return otherValue;
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SimpleIntReference.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SimpleIntReference.java
index dcf4217028..e1059caeed 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SimpleIntReference.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SimpleIntReference.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2012 Oracle. All rights reserved.
+ * Copyright (c) 2007, 2015 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -10,6 +10,8 @@
package org.eclipse.jpt.common.utility.internal.reference;
import java.io.Serializable;
+import org.eclipse.jpt.common.utility.internal.BitTools;
+import org.eclipse.jpt.common.utility.predicate.IntPredicate;
import org.eclipse.jpt.common.utility.reference.IntReference;
import org.eclipse.jpt.common.utility.reference.ModifiableIntReference;
@@ -34,7 +36,7 @@ public final class SimpleIntReference
// ********** constructors **********
/**
- * Construct a <code>int</code> reference with the specified initial value.
+ * Construct an <code>int</code> reference with the specified initial value.
*/
public SimpleIntReference(int count) {
super();
@@ -42,25 +44,25 @@ public final class SimpleIntReference
}
/**
- * Construct a <code>int</code> reference with an initial value of zero.
+ * Construct an <code>int</code> reference with an initial value of zero.
*/
public SimpleIntReference() {
this(0);
}
- // ********** methods **********
+ // ********** IntReference **********
public int getValue() {
return this.value;
}
- public boolean equals(int v) {
- return this.value == v;
+ public boolean equals(int i) {
+ return this.value == i;
}
- public boolean notEqual(int v) {
- return this.value != v;
+ public boolean notEqual(int i) {
+ return this.value != i;
}
public boolean isZero() {
@@ -71,102 +73,194 @@ public final class SimpleIntReference
return this.value != 0;
}
- public boolean isGreaterThan(int v) {
- return this.value > v;
+ public boolean isGreaterThan(int i) {
+ return this.value > i;
}
- public boolean isGreaterThanOrEqual(int v) {
- return this.value >= v;
+ public boolean isGreaterThanOrEqual(int i) {
+ return this.value >= i;
}
- public boolean isLessThan(int v) {
- return this.value < v;
+ public boolean isLessThan(int i) {
+ return this.value < i;
}
- public boolean isLessThanOrEqual(int v) {
- return this.value <= v;
+ public boolean isLessThanOrEqual(int i) {
+ return this.value <= i;
}
public boolean isPositive() {
- return this.isGreaterThan(0);
+ return this.value > 0;
}
public boolean isNotPositive() {
- return this.isLessThanOrEqual(0);
+ return this.value <= 0;
}
public boolean isNegative() {
- return this.isLessThan(0);
+ return this.value < 0;
}
public boolean isNotNegative() {
- return this.isGreaterThanOrEqual(0);
+ return this.value >= 0;
+ }
+
+ public boolean isMemberOf(IntPredicate predicate) {
+ return predicate.evaluate(this.value);
+ }
+
+ public boolean isNotMemberOf(IntPredicate predicate) {
+ return ! predicate.evaluate(this.value);
+ }
+
+
+ // ********** ModifiableIntReference **********
+
+ public int setValue(int value) {
+ int old = this.value;
+ this.value = value;
+ return old;
+ }
+
+ public int setZero() {
+ return this.setValue(0);
+ }
+
+ public int increment() {
+ return ++this.value;
+ }
+
+ public int incrementExact() {
+ this.value = Math.incrementExact(this.value);
+ return this.value;
+ }
+
+ public int decrement() {
+ return --this.value;
+ }
+
+ public int decrementExact() {
+ this.value = Math.decrementExact(this.value);
+ return this.value;
+ }
+
+ public int halve() {
+ this.value = BitTools.half(this.value);
+ return this.value;
+ }
+
+ public int twice() {
+ this.value = BitTools.twice(this.value);
+ return this.value;
+ }
+
+ public int twiceExact() {
+ this.value = Math.multiplyExact(this.value, 2);
+ return this.value;
}
public int abs() {
- return Math.abs(this.value);
+ this.value = Math.abs(this.value);
+ return this.value;
}
- public int neg() {
- return -this.value;
+ public int negate() {
+ this.value = -this.value;
+ return this.value;
}
- public int add(int v) {
- return this.value + v;
+ public int negateExact() {
+ this.value = Math.negateExact(this.value);
+ return this.value;
}
- public int subtract(int v) {
- return this.value - v;
+ public int add(int i) {
+ this.value += i;
+ return this.value;
}
- public int multiply(int v) {
- return this.value * v;
+ public int addExact(int i) {
+ this.value = Math.addExact(this.value, i);
+ return this.value;
}
- public int divide(int v) {
- return this.value / v;
+ public int subtract(int i) {
+ this.value -= i;
+ return this.value;
}
- public int remainder(int v) {
- return this.value % v;
+ public int subtractExact(int i) {
+ this.value = Math.subtractExact(this.value, i);
+ return this.value;
}
- public int min(int v) {
- return Math.min(this.value, v);
+ public int multiply(int i) {
+ this.value = this.value * i;
+ return this.value;
}
- public int max(int v) {
- return Math.max(this.value, v);
+ public int multiplyExact(int i) {
+ this.value = Math.multiplyExact(this.value, i);
+ return this.value;
}
- public double pow(int v) {
- return Math.pow(this.value, v);
+ public int divide(int i) {
+ this.value = this.value / i;
+ return this.value;
}
- public int setValue(int value) {
- int old = this.value;
- this.value = value;
- return old;
+ public int floorDivide(int i) {
+ this.value = Math.floorDiv(this.value, i);
+ return this.value;
}
- public int setZero() {
- return this.setValue(0);
+ public int remainder(int i) {
+ this.value = this.value % i;
+ return this.value;
}
- public int increment() {
- return ++this.value;
+ public int floorRemainder(int i) {
+ this.value = Math.floorMod(this.value, i);
+ return this.value;
}
- public int decrement() {
- return --this.value;
+ public int min(int i) {
+ this.value = Math.min(this.value, i);
+ return this.value;
+ }
+
+ public int max(int i) {
+ this.value = Math.max(this.value, i);
+ return this.value;
+ }
+
+ public boolean commit(int newValue, int expectedValue) {
+ if (this.value == expectedValue) {
+ this.value = newValue;
+ return true;
+ }
+ return false;
+ }
+
+ public int swap(ModifiableIntReference other) {
+ if (other == this) {
+ return this.value;
+ }
+ int otherValue = other.getValue();
+ if (otherValue == this.value) {
+ return this.value;
+ }
+ other.setValue(this.value);
+ this.value = otherValue;
+ return otherValue;
}
- // ********** Comparable implementation **********
+ // ********** Comparable **********
- public int compareTo(IntReference ref) {
- int v = ref.getValue();
- return (this.value < v) ? -1 : ((this.value == v) ? 0 : 1);
+ public int compareTo(IntReference other) {
+ int otherValue = other.getValue();
+ return (this.value < otherValue) ? -1 : ((this.value == otherValue) ? 0 : 1);
}
@@ -181,6 +275,26 @@ public final class SimpleIntReference
}
}
+ /**
+ * Object identity is critical to int references.
+ * There is no reason for two different int references to be
+ * <em>equal</em>.
+ *
+ * @see #equals(int)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ return super.equals(obj);
+ }
+
+ /**
+ * @see #equals(Object)
+ */
+ @Override
+ public int hashCode() {
+ return super.hashCode();
+ }
+
@Override
public String toString() {
return '[' + String.valueOf(this.value) + ']';
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SynchronizedInt.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SynchronizedInt.java
index f97386fe41..02a5781f67 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SynchronizedInt.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/SynchronizedInt.java
@@ -10,8 +10,10 @@
package org.eclipse.jpt.common.utility.internal.reference;
import java.io.Serializable;
-
import org.eclipse.jpt.common.utility.command.Command;
+import org.eclipse.jpt.common.utility.internal.BitTools;
+import org.eclipse.jpt.common.utility.internal.predicate.int_.IntPredicateTools;
+import org.eclipse.jpt.common.utility.predicate.IntPredicate;
import org.eclipse.jpt.common.utility.reference.IntReference;
import org.eclipse.jpt.common.utility.reference.ModifiableIntReference;
@@ -73,7 +75,7 @@ public class SynchronizedInt
}
- // ********** methods **********
+ // ********** IntReference **********
public int getValue() {
synchronized (this.mutex) {
@@ -81,15 +83,15 @@ public class SynchronizedInt
}
}
- public boolean equals(int v) {
+ public boolean equals(int i) {
synchronized (this.mutex) {
- return this.value == v;
+ return this.value == i;
}
}
- public boolean notEqual(int v) {
+ public boolean notEqual(int i) {
synchronized (this.mutex) {
- return this.value != v;
+ return this.value != i;
}
}
@@ -105,27 +107,27 @@ public class SynchronizedInt
}
}
- public boolean isGreaterThan(int v) {
+ public boolean isGreaterThan(int i) {
synchronized (this.mutex) {
- return this.value > v;
+ return this.value > i;
}
}
- public boolean isGreaterThanOrEqual(int v) {
+ public boolean isGreaterThanOrEqual(int i) {
synchronized (this.mutex) {
- return this.value >= v;
+ return this.value >= i;
}
}
- public boolean isLessThan(int v) {
+ public boolean isLessThan(int i) {
synchronized (this.mutex) {
- return this.value < v;
+ return this.value < i;
}
}
- public boolean isLessThanOrEqual(int v) {
+ public boolean isLessThanOrEqual(int i) {
synchronized (this.mutex) {
- return this.value <= v;
+ return this.value <= i;
}
}
@@ -145,146 +147,461 @@ public class SynchronizedInt
return this.isGreaterThanOrEqual(0);
}
- public int abs() {
+ public boolean isMemberOf(IntPredicate predicate) {
synchronized (this.mutex) {
- return Math.abs(this.value);
+ return predicate.evaluate(this.value);
}
}
- public int neg() {
+ public boolean isNotMemberOf(IntPredicate predicate) {
synchronized (this.mutex) {
- return -this.value;
+ return ! predicate.evaluate(this.value);
}
}
- public int add(int v) {
+
+ // ********** ModifiableIntReference **********
+
+ /**
+ * If the value changes, all waiting threads are notified.
+ */
+ public int setValue(int value) {
synchronized (this.mutex) {
- return this.value + v;
+ return this.setValue_(value);
}
}
- public int subtract(int v) {
+ /**
+ * Pre-condition: synchronized
+ */
+ private int setValue_(int v) {
+ int old = this.value;
+ return (old == v) ? old : this.setValue_(v, old);
+ }
+
+ /**
+ * Pre-condition: synchronized and new value is different
+ */
+ private int setChangedValue_(int v) {
+ return this.setValue_(v, this.value);
+ }
+
+ /**
+ * Pre-condition: synchronized and new value is different
+ */
+ private int setValue_(int v, int old) {
+ this.value = v;
+ this.mutex.notifyAll();
+ return old;
+ }
+
+ public int setZero() {
+ return this.setValue(0);
+ }
+
+ public int increment() {
synchronized (this.mutex) {
- return this.value - v;
+ this.value++;
+ this.mutex.notifyAll();
+ return this.value;
}
}
- public int multiply(int v) {
+ public int incrementExact() {
synchronized (this.mutex) {
- return this.value * v;
+ this.value = Math.incrementExact(this.value);
+ this.mutex.notifyAll();
+ return this.value;
}
}
- public int divide(int v) {
+ public int decrement() {
synchronized (this.mutex) {
- return this.value / v;
+ this.value--;
+ this.mutex.notifyAll();
+ return this.value;
}
}
- public int remainder(int v) {
+ public int decrementExact() {
synchronized (this.mutex) {
- return this.value % v;
+ this.value = Math.decrementExact(this.value);
+ this.mutex.notifyAll();
+ return this.value;
}
}
- public int min(int v) {
+ public int halve() {
synchronized (this.mutex) {
- return Math.min(this.value, v);
+ return this.halve_();
}
}
- public int max(int v) {
+ /**
+ * Pre-condition: synchronized
+ */
+ private int halve_() {
+ int old = this.value;
+ this.value = BitTools.half(old);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int twice() {
synchronized (this.mutex) {
- return Math.max(this.value, v);
+ return this.twice_();
}
}
- public double pow(int v) {
+ /**
+ * Pre-condition: synchronized
+ */
+ private int twice_() {
+ int old = this.value;
+ this.value = BitTools.twice(old);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int twiceExact() {
synchronized (this.mutex) {
- return Math.pow(this.value, v);
+ return this.twiceExact_();
}
}
/**
- * If the value changes, all waiting threads are notified.
+ * Pre-condition: synchronized
*/
- public int setValue(int value) {
+ private int twiceExact_() {
+ int old = this.value;
+ this.value = Math.multiplyExact(old, 2);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int abs() {
synchronized (this.mutex) {
- return this.setValue_(value);
+ return this.abs_();
}
}
/**
* Pre-condition: synchronized
*/
- private int setValue_(int v) {
+ private int abs_() {
int old = this.value;
- return (old == v) ? old : this.setValue_(v, old);
+ this.value = Math.abs(old);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int negate() {
+ synchronized (this.mutex) {
+ this.value = -this.value;
+ this.mutex.notifyAll();
+ return this.value;
+ }
+ }
+
+ public int negateExact() {
+ synchronized (this.mutex) {
+ this.value = Math.negateExact(this.value);
+ this.mutex.notifyAll();
+ return this.value;
+ }
+ }
+
+ public int add(int i) {
+ synchronized (this.mutex) {
+ return this.add_(i);
+ }
}
/**
- * Pre-condition: synchronized and new value is different
+ * Pre-condition: synchronized
*/
- private int setChangedValue_(int v) {
- return this.setValue_(v, this.value);
+ private int add_(int i) {
+ int old = this.value;
+ this.value += i;
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int addExact(int i) {
+ synchronized (this.mutex) {
+ return this.addExact_(i);
+ }
}
/**
- * Pre-condition: synchronized and new value is different
+ * Pre-condition: synchronized
*/
- private int setValue_(int v, int old) {
- this.value = v;
- this.mutex.notifyAll();
- return old;
+ private int addExact_(int i) {
+ int old = this.value;
+ this.value = Math.addExact(this.value, i);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int subtract(int i) {
+ synchronized (this.mutex) {
+ return this.subtract_(i);
+ }
}
/**
- * Set the value to zero. If the value changes, all waiting
- * threads are notified. Return the previous value.
+ * Pre-condition: synchronized
*/
- public int setZero() {
- return this.setValue(0);
+ private int subtract_(int i) {
+ int old = this.value;
+ this.value -= i;
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int subtractExact(int i) {
+ synchronized (this.mutex) {
+ return this.subtractExact_(i);
+ }
}
/**
- * Increment the value by one.
- * Return the new value.
+ * Pre-condition: synchronized
*/
- public int increment() {
+ private int subtractExact_(int i) {
+ int old = this.value;
+ this.value = Math.subtractExact(this.value, i);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int multiply(int i) {
synchronized (this.mutex) {
- this.value++;
+ return this.multiply_(i);
+ }
+ }
+
+ /**
+ * Pre-condition: synchronized
+ */
+ private int multiply_(int i) {
+ int old = this.value;
+ this.value = this.value * i;
+ if (this.value != old) {
this.mutex.notifyAll();
- return this.value;
+ }
+ return this.value;
+ }
+
+ public int multiplyExact(int i) {
+ synchronized (this.mutex) {
+ return this.multiplyExact_(i);
}
}
/**
- * Decrement the value by one.
- * Return the new value.
+ * Pre-condition: synchronized
*/
- public int decrement() {
+ private int multiplyExact_(int i) {
+ int old = this.value;
+ this.value = Math.multiplyExact(this.value, i);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int divide(int i) {
synchronized (this.mutex) {
- this.value--;
+ return this.divide_(i);
+ }
+ }
+
+ /**
+ * Pre-condition: synchronized
+ */
+ private int divide_(int i) {
+ int old = this.value;
+ this.value = this.value / i;
+ if (this.value != old) {
this.mutex.notifyAll();
- return this.value;
+ }
+ return this.value;
+ }
+
+ public int floorDivide(int i) {
+ synchronized (this.mutex) {
+ return this.floorDivide_(i);
+ }
+ }
+
+ /**
+ * Pre-condition: synchronized
+ */
+ private int floorDivide_(int i) {
+ int old = this.value;
+ this.value = Math.floorDiv(this.value, i);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int remainder(int i) {
+ synchronized (this.mutex) {
+ return this.remainder_(i);
}
}
/**
- * If the current value is the specified expected value, set it to the
- * specified new value. Return the previous value.
+ * Pre-condition: synchronized
+ */
+ private int remainder_(int i) {
+ int old = this.value;
+ this.value = this.value % i;
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int floorRemainder(int i) {
+ synchronized (this.mutex) {
+ return this.floorRemainder_(i);
+ }
+ }
+
+ /**
+ * Pre-condition: synchronized
*/
- public int compareAndSwap(int expectedValue, int newValue) {
+ private int floorRemainder_(int i) {
+ int old = this.value;
+ this.value = Math.floorMod(this.value, i);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int min(int i) {
synchronized (this.mutex) {
- return this.compareAndSwap_(expectedValue, newValue);
+ return this.min_(i);
}
}
/**
* Pre-condition: synchronized
*/
- private int compareAndSwap_(int expectedValue, int newValue) {
- return (this.value == expectedValue) ? this.setValue_(newValue) : this.value;
+ private int min_(int i) {
+ int old = this.value;
+ this.value = Math.min(this.value, i);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public int max(int i) {
+ synchronized (this.mutex) {
+ return this.max_(i);
+ }
+ }
+
+ /**
+ * Pre-condition: synchronized
+ */
+ private int max_(int i) {
+ int old = this.value;
+ this.value = Math.max(this.value, i);
+ if (this.value != old) {
+ this.mutex.notifyAll();
+ }
+ return this.value;
+ }
+
+ public boolean commit(int newValue, int expectedValue) {
+ synchronized (this.mutex) {
+ return this.commit_(newValue, expectedValue);
+ }
+ }
+
+ /**
+ * Pre-condition: synchronized
+ */
+ private boolean commit_(int newValue, int expectedValue) {
+ if (this.value == expectedValue) {
+ this.setValue_(newValue);
+ return true;
+ }
+ return false;
+ }
+
+ public int swap(ModifiableIntReference other) {
+ if (other == this) {
+ return this.getValue();
+ }
+ if (other instanceof SynchronizedInt) {
+ return this.swap_((SynchronizedInt) other);
+ }
+
+ int thisValue = 0;
+ int otherValue = other.getValue();
+ synchronized (this.mutex) {
+ thisValue = this.value;
+ if (thisValue != otherValue) {
+ this.setChangedValue_(otherValue);
+ }
+ }
+ other.setValue(thisValue);
+ return otherValue;
+ }
+
+ /**
+ * Atomically swap the value of this synchronized int with the value of
+ * the specified synchronized int. Make assumptions about the value of
+ * <em>identity hash code</em> to avoid deadlock when two synchronized
+ * ints swap values with each other simultaneously.
+ * If either value changes, the corresponding waiting threads are notified.
+ * Return the new value.
+ */
+ public int swap(SynchronizedInt other) {
+ return (other == this) ? this.getValue() : this.swap_(other);
+ }
+
+ /**
+ * Pre-condition: not same object
+ */
+ private int swap_(SynchronizedInt other) {
+ boolean thisFirst = System.identityHashCode(this) < System.identityHashCode(other);
+ SynchronizedInt first = thisFirst ? this : other;
+ SynchronizedInt second = thisFirst ? other : this;
+ synchronized (first.mutex) {
+ synchronized (second.mutex) {
+ int thisValue = this.value;
+ int otherValue = other.value;
+ if (thisValue == otherValue) {
+ return thisValue; // nothing changes
+ }
+ other.setChangedValue_(thisValue);
+ return this.setChangedValue_(otherValue);
+ }
+ }
}
/**
@@ -300,43 +617,50 @@ public class SynchronizedInt
/**
* Suspend the current thread until the value changes
- * to the specified value. If the value is already the
- * specified value, return immediately.
+ * to belong to the set specified by the specified predicate.
+ * If the value already belongs to the set, return immediately.
*/
- public void waitUntilEqual(int v) throws InterruptedException {
+ public void waitUntil(IntPredicate predicate) throws InterruptedException {
synchronized (this.mutex) {
- this.waitUntilEqual_(v);
+ this.waitUntil_(predicate);
}
}
/**
* Pre-condition: synchronized
*/
- private void waitUntilEqual_(int v) throws InterruptedException {
- while (this.value != v) {
+ private void waitUntil_(IntPredicate predicate) throws InterruptedException {
+ while ( ! predicate.evaluate(this.value)) {
this.mutex.wait();
}
}
/**
* Suspend the current thread until the value changes
- * to something other than the specified value. If the
- * value is already <em>not</em> the specified value,
- * return immediately.
+ * to no longer belong to the set specified by the specified predicate.
+ * If the value already does not belong to the set, return immediately.
*/
- public void waitUntilNotEqual(int v) throws InterruptedException {
- synchronized (this.mutex) {
- this.waitUntilNotEqual_(v);
- }
+ public void waitUntilNot(IntPredicate predicate) throws InterruptedException {
+ this.waitUntil(IntPredicateTools.not(predicate));
}
/**
- * Pre-condition: synchronized
+ * Suspend the current thread until the value changes
+ * to the specified value. If the value is already the
+ * specified value, return immediately.
*/
- private void waitUntilNotEqual_(int v) throws InterruptedException {
- while (this.value == v) {
- this.mutex.wait();
- }
+ public void waitUntilEqual(int i) throws InterruptedException {
+ this.waitUntil(IntPredicateTools.isEqual(i));
+ }
+
+ /**
+ * Suspend the current thread until the value changes
+ * to something other than the specified value. If the
+ * value is already <em>not</em> the specified value,
+ * return immediately.
+ */
+ public void waitUntilNotEqual(int i) throws InterruptedException {
+ this.waitUntil(IntPredicateTools.notEqual(i));
}
/**
@@ -362,19 +686,8 @@ public class SynchronizedInt
* to a value greater than the specified value. If the value is already
* greater than the specified value, return immediately.
*/
- public void waitUntilGreaterThan(int v) throws InterruptedException {
- synchronized (this.mutex) {
- this.waitUntilGreaterThan_(v);
- }
- }
-
- /**
- * Pre-condition: synchronized
- */
- private void waitUntilGreaterThan_(int v) throws InterruptedException {
- while (this.value <= v) {
- this.mutex.wait();
- }
+ public void waitUntilGreaterThan(int i) throws InterruptedException {
+ this.waitUntil(IntPredicateTools.isGreaterThan(i));
}
/**
@@ -382,19 +695,8 @@ public class SynchronizedInt
* to a value greater than or equal to the specified value. If the value is already
* greater than or equal the specified value, return immediately.
*/
- public void waitUntilGreaterThanOrEqual(int v) throws InterruptedException {
- synchronized (this.mutex) {
- this.waitUntilGreaterThanOrEqual_(v);
- }
- }
-
- /**
- * Pre-condition: synchronized
- */
- private void waitUntilGreaterThanOrEqual_(int v) throws InterruptedException {
- while (this.value < v) {
- this.mutex.wait();
- }
+ public void waitUntilGreaterThanOrEqual(int i) throws InterruptedException {
+ this.waitUntil(IntPredicateTools.isGreaterThanOrEqual(i));
}
/**
@@ -402,19 +704,8 @@ public class SynchronizedInt
* to a value less than the specified value. If the value is already
* less than the specified value, return immediately.
*/
- public void waitUntilLessThan(int v) throws InterruptedException {
- synchronized (this.mutex) {
- this.waitUntilLessThan_(v);
- }
- }
-
- /**
- * Pre-condition: synchronized
- */
- private void waitUntilLessThan_(int v) throws InterruptedException {
- while (this.value >= v) {
- this.mutex.wait();
- }
+ public void waitUntilLessThan(int i) throws InterruptedException {
+ this.waitUntil(IntPredicateTools.isLessThan(i));
}
/**
@@ -422,19 +713,8 @@ public class SynchronizedInt
* to a value less than or equal to the specified value. If the value is already
* less than or equal the specified value, return immediately.
*/
- public void waitUntilLessThanOrEqual(int v) throws InterruptedException {
- synchronized (this.mutex) {
- this.waitUntilLessThanOrEqual_(v);
- }
- }
-
- /**
- * Pre-condition: synchronized
- */
- private void waitUntilLessThanOrEqual_(int v) throws InterruptedException {
- while (this.value > v) {
- this.mutex.wait();
- }
+ public void waitUntilLessThanOrEqual(int i) throws InterruptedException {
+ this.waitUntil(IntPredicateTools.isLessThanOrEqual(i));
}
/**
@@ -481,10 +761,10 @@ public class SynchronizedInt
* the value immediately.
* Return the previous value.
*/
- public int waitToSetValue(int v) throws InterruptedException {
+ public int waitToSetValue(int i) throws InterruptedException {
synchronized (this.mutex) {
- this.waitUntilNotEqual_(v);
- return this.setChangedValue_(v);
+ this.waitUntil_(IntPredicateTools.notEqual(i));
+ return this.setChangedValue_(i);
}
}
@@ -508,9 +788,9 @@ public class SynchronizedInt
* set the value to the specified new value immediately.
* Return the previous value.
*/
- public int waitToSwap(int expectedValue, int newValue) throws InterruptedException {
+ public int waitToCommit(int newValue, int expectedValue) throws InterruptedException {
synchronized (this.mutex) {
- this.waitUntilEqual_(expectedValue);
+ this.waitUntil_(IntPredicateTools.isEqual(expectedValue));
return this.setValue_(newValue);
}
}
@@ -520,68 +800,76 @@ public class SynchronizedInt
/**
* Suspend the current thread until the value changes
- * to the specified value or the specified time-out occurs.
+ * to belong to the set specified by the specified predicate
+ * or the specified time-out occurs.
* The time-out is specified in milliseconds. Return <code>true</code>
- * if the specified value was achieved; return <code>false</code>
- * if a time-out occurred.
- * If the value is already the specified value, return true immediately.
+ * if the value now beloongs to the set; return <code>false</code>
+ * if a time-out occurred. If the value already belongs to the set,
+ * return <code>true</code> immediately.
* If the time-out is zero, wait indefinitely.
*/
- public boolean waitUntilEqual(int v, long timeout) throws InterruptedException {
+ public boolean waitUntil(IntPredicate predicate, long timeout) throws InterruptedException {
synchronized (this.mutex) {
- return this.waitUntilEqual_(v, timeout);
+ return this.waitUntil_(predicate, timeout);
}
}
/**
* Pre-condition: synchronized
*/
- private boolean waitUntilEqual_(int v, long timeout) throws InterruptedException {
+ private boolean waitUntil_(IntPredicate predicate, long timeout) throws InterruptedException {
if (timeout == 0L) {
- this.waitUntilEqual_(v); // wait indefinitely until notified
+ this.waitUntil_(predicate); // wait indefinitely until notified
return true; // if it ever comes back, the condition was met
}
long stop = System.currentTimeMillis() + timeout;
long remaining = timeout;
- while ((this.value != v) && (remaining > 0L)) {
+ while (( ! predicate.evaluate(this.value)) && (remaining > 0L)) {
this.mutex.wait(remaining);
remaining = stop - System.currentTimeMillis();
}
- return (this.value == v);
+ return predicate.evaluate(this.value);
}
/**
- * Suspend the current thread until the value changes to something
- * other than the specified value or the specified time-out occurs.
+ * Suspend the current thread until the value changes
+ * to belong to the set specified by the specified predicate
+ * or the specified time-out occurs.
* The time-out is specified in milliseconds. Return <code>true</code>
- * if the specified value was removed; return <code>false</code> if a
- * time-out occurred. If the value is already <em>not</em> the specified
- * value, return <code>true</code> immediately.
+ * if the value now beloongs to the set; return <code>false</code>
+ * if a time-out occurred. If the value already belongs to the set,
+ * return <code>true</code> immediately.
* If the time-out is zero, wait indefinitely.
*/
- public boolean waitUntilNotEqual(int v, long timeout) throws InterruptedException {
- synchronized (this.mutex) {
- return this.waitUntilNotEqual_(v, timeout);
- }
+ public boolean waitUntilNot(IntPredicate predicate, long timeout) throws InterruptedException {
+ return this.waitUntil(IntPredicateTools.not(predicate), timeout);
}
/**
- * Pre-condition: synchronized
+ * Suspend the current thread until the value changes
+ * to the specified value or the specified time-out occurs.
+ * The time-out is specified in milliseconds. Return <code>true</code>
+ * if the specified value was achieved; return <code>false</code>
+ * if a time-out occurred.
+ * If the value is already the specified value, return true immediately.
+ * If the time-out is zero, wait indefinitely.
*/
- private boolean waitUntilNotEqual_(int v, long timeout) throws InterruptedException {
- if (timeout == 0L) {
- this.waitUntilNotEqual_(v); // wait indefinitely until notified
- return true; // if it ever comes back, the condition was met
- }
+ public boolean waitUntilEqual(int i, long timeout) throws InterruptedException {
+ return this.waitUntil(IntPredicateTools.isEqual(i), timeout);
+ }
- long stop = System.currentTimeMillis() + timeout;
- long remaining = timeout;
- while ((this.value == v) && (remaining > 0L)) {
- this.mutex.wait(remaining);
- remaining = stop - System.currentTimeMillis();
- }
- return (this.value != v);
+ /**
+ * Suspend the current thread until the value changes to something
+ * other than the specified value or the specified time-out occurs.
+ * The time-out is specified in milliseconds. Return <code>true</code>
+ * if the specified value was removed; return <code>false</code> if a
+ * time-out occurred. If the value is already <em>not</em> the specified
+ * value, return <code>true</code> immediately.
+ * If the time-out is zero, wait indefinitely.
+ */
+ public boolean waitUntilNotEqual(int i, long timeout) throws InterruptedException {
+ return this.waitUntil(IntPredicateTools.notEqual(i), timeout);
}
/**
@@ -619,28 +907,8 @@ public class SynchronizedInt
* If the value is already greater than the specified value, return immediately.
* If the time-out is zero, wait indefinitely.
*/
- public boolean waitUntilGreaterThan(int v, long timeout) throws InterruptedException {
- synchronized (this.mutex) {
- return this.waitUntilGreaterThan_(v, timeout);
- }
- }
-
- /**
- * Pre-condition: synchronized
- */
- private boolean waitUntilGreaterThan_(int v, long timeout) throws InterruptedException {
- if (timeout == 0L) {
- this.waitUntilGreaterThan_(v); // wait indefinitely until notified
- return true; // if it ever comes back, the condition was met
- }
-
- long stop = System.currentTimeMillis() + timeout;
- long remaining = timeout;
- while ((this.value <= v) && (remaining > 0L)) {
- this.mutex.wait(remaining);
- remaining = stop - System.currentTimeMillis();
- }
- return (this.value > v);
+ public boolean waitUntilGreaterThan(int i, long timeout) throws InterruptedException {
+ return this.waitUntil(IntPredicateTools.isGreaterThan(i), timeout);
}
/**
@@ -653,28 +921,8 @@ public class SynchronizedInt
* return immediately.
* If the time-out is zero, wait indefinitely.
*/
- public boolean waitUntilGreaterThanOrEqual(int v, long timeout) throws InterruptedException {
- synchronized (this.mutex) {
- return this.waitUntilGreaterThanOrEqual_(v, timeout);
- }
- }
-
- /**
- * Pre-condition: synchronized
- */
- private boolean waitUntilGreaterThanOrEqual_(int v, long timeout) throws InterruptedException {
- if (timeout == 0L) {
- this.waitUntilGreaterThanOrEqual_(v); // wait indefinitely until notified
- return true; // if it ever comes back, the condition was met
- }
-
- long stop = System.currentTimeMillis() + timeout;
- long remaining = timeout;
- while ((this.value < v) && (remaining > 0L)) {
- this.mutex.wait(remaining);
- remaining = stop - System.currentTimeMillis();
- }
- return (this.value >= v);
+ public boolean waitUntilGreaterThanOrEqual(int i, long timeout) throws InterruptedException {
+ return this.waitUntil(IntPredicateTools.isGreaterThanOrEqual(i), timeout);
}
/**
@@ -686,28 +934,8 @@ public class SynchronizedInt
* If the value is already less than the specified value, return immediately.
* If the time-out is zero, wait indefinitely.
*/
- public boolean waitUntilLessThan(int v, long timeout) throws InterruptedException {
- synchronized (this.mutex) {
- return this.waitUntilLessThan_(v, timeout);
- }
- }
-
- /**
- * Pre-condition: synchronized
- */
- private boolean waitUntilLessThan_(int v, long timeout) throws InterruptedException {
- if (timeout == 0L) {
- this.waitUntilLessThan_(v); // wait indefinitely until notified
- return true; // if it ever comes back, the condition was met
- }
-
- long stop = System.currentTimeMillis() + timeout;
- long remaining = timeout;
- while ((this.value >= v) && (remaining > 0L)) {
- this.mutex.wait(remaining);
- remaining = stop - System.currentTimeMillis();
- }
- return (this.value < v);
+ public boolean waitUntilLessThan(int i, long timeout) throws InterruptedException {
+ return this.waitUntil(IntPredicateTools.isLessThan(i), timeout);
}
/**
@@ -720,28 +948,8 @@ public class SynchronizedInt
* return immediately.
* If the time-out is zero, wait indefinitely.
*/
- public boolean waitUntilLessThanOrEqual(int v, long timeout) throws InterruptedException {
- synchronized (this.mutex) {
- return this.waitUntilLessThanOrEqual_(v, timeout);
- }
- }
-
- /**
- * Pre-condition: synchronized
- */
- private boolean waitUntilLessThanOrEqual_(int v, long timeout) throws InterruptedException {
- if (timeout == 0L) {
- this.waitUntilLessThanOrEqual_(v); // wait indefinitely until notified
- return true; // if it ever comes back, the condition was met
- }
-
- long stop = System.currentTimeMillis() + timeout;
- long remaining = timeout;
- while ((this.value > v) && (remaining > 0L)) {
- this.mutex.wait(remaining);
- remaining = stop - System.currentTimeMillis();
- }
- return (this.value <= v);
+ public boolean waitUntilLessThanOrEqual(int i, long timeout) throws InterruptedException {
+ return this.waitUntil(IntPredicateTools.isLessThanOrEqual(i), timeout);
}
/**
@@ -810,11 +1018,11 @@ public class SynchronizedInt
* the value immediately and return <code>true</code>.
* If the time-out is zero, wait indefinitely.
*/
- public boolean waitToSetValue(int v, long timeout) throws InterruptedException {
+ public boolean waitToSetValue(int i, long timeout) throws InterruptedException {
synchronized (this.mutex) {
- boolean success = this.waitUntilNotEqual_(v, timeout);
+ boolean success = this.waitUntil_(IntPredicateTools.notEqual(i), timeout);
if (success) {
- this.setChangedValue_(v);
+ this.setChangedValue_(i);
}
return success;
}
@@ -852,9 +1060,9 @@ public class SynchronizedInt
* Return the previous value.
* If the time-out is zero, wait indefinitely.
*/
- public boolean waitToSwap(int expectedValue, int newValue, long timeout) throws InterruptedException {
+ public boolean waitToCommit(int newValue, int expectedValue, long timeout) throws InterruptedException {
synchronized (this.mutex) {
- boolean success = this.waitUntilEqual_(expectedValue, timeout);
+ boolean success = this.waitUntil_(IntPredicateTools.isEqual(expectedValue), timeout);
if (success) {
this.setValue_(newValue);
}
@@ -882,9 +1090,9 @@ public class SynchronizedInt
// ********** Comparable implementation **********
- public int compareTo(IntReference ref) {
+ public int compareTo(IntReference other) {
int thisValue = this.getValue();
- int otherValue = ref.getValue();
+ int otherValue = other.getValue();
return (thisValue < otherValue) ? -1 : ((thisValue == otherValue) ? 0 : 1);
}
@@ -902,6 +1110,26 @@ public class SynchronizedInt
}
}
+ /**
+ * Object identity is critical to int references.
+ * There is no reason for two different int references to be
+ * <em>equal</em>.
+ *
+ * @see #equals(int)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ return super.equals(obj);
+ }
+
+ /**
+ * @see #equals(Object)
+ */
+ @Override
+ public int hashCode() {
+ return super.hashCode();
+ }
+
@Override
public String toString() {
return '[' + String.valueOf(this.getValue()) + ']';
@@ -912,5 +1140,4 @@ public class SynchronizedInt
s.defaultWriteObject();
}
}
-
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/reference/IntReference.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/reference/IntReference.java
index d6617c626d..83722569f8 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/reference/IntReference.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/reference/IntReference.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010, 2013 Oracle. All rights reserved.
+ * Copyright (c) 2010, 2015 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -9,6 +9,8 @@
******************************************************************************/
package org.eclipse.jpt.common.utility.reference;
+import org.eclipse.jpt.common.utility.predicate.IntPredicate;
+
/**
* Interface for a container for holding an <code>int</code>.
* <p>
@@ -32,13 +34,13 @@ public interface IntReference
* Return whether the current <code>int</code> value is equal to the
* specified value.
*/
- boolean equals(int v);
+ boolean equals(int i);
/**
* Return whether the current <code>int</code> value is not equal to
* the specified value.
*/
- boolean notEqual(int v);
+ boolean notEqual(int i);
/**
* Return whether the current <code>int</code> value is zero.
@@ -54,25 +56,25 @@ public interface IntReference
* Return whether the current <code>int</code> value is greater than
* the specified value.
*/
- boolean isGreaterThan(int v);
+ boolean isGreaterThan(int i);
/**
* Return whether the current <code>int</code> value is greater than
* or equal to the specified value.
*/
- boolean isGreaterThanOrEqual(int v);
+ boolean isGreaterThanOrEqual(int i);
/**
* Return whether the current <code>int</code> value is less than
* the specified value.
*/
- boolean isLessThan(int v);
+ boolean isLessThan(int i);
/**
* Return whether the current <code>int</code> value is less than
* or equal to the specified value.
*/
- boolean isLessThanOrEqual(int v);
+ boolean isLessThanOrEqual(int i);
/**
* Return whether the current <code>int</code> value is positive.
@@ -97,56 +99,14 @@ public interface IntReference
boolean isNotNegative();
/**
- * Return the absolute value of the current <code>int</code> value.
- */
- int abs();
-
- /**
- * Return the negative value of the current <code>int</code> value.
- */
- int neg();
-
- /**
- * Return the current <code>int</code> value plus the specified value.
- */
- int add(int v);
-
- /**
- * Return current <code>int</code> value minus the specified value.
- */
- int subtract(int v);
-
- /**
- * Return current <code>int</code> value multiplied by the specified value.
- */
- int multiply(int v);
-
- /**
- * Return current <code>int</code> value divided by the specified value.
- */
- int divide(int v);
-
- /**
- * Return the remainder of the current <code>int</code> value divided by
- * the specified value.
- */
- int remainder(int v);
-
- /**
- * Return the minimum of the current <code>int</code> value and
- * the specified value.
- */
- int min(int v);
-
- /**
- * Return the maximum of the current <code>int</code> value and
- * the specified value.
+ * Return whether the current <code>int</code> value is a
+ * member of the set specified by the specified predicate.
*/
- int max(int v);
+ boolean isMemberOf(IntPredicate predicate);
/**
- * Return the current <code>int</code> value raised to the power
- * of the specified value.
+ * Return whether the current <code>int</code> value is not a
+ * member of the set specified by the specified predicate.
*/
- double pow(int v);
+ boolean isNotMemberOf(IntPredicate predicate);
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/reference/ModifiableIntReference.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/reference/ModifiableIntReference.java
index 97071a119a..e23135ac9e 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/reference/ModifiableIntReference.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/reference/ModifiableIntReference.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010, 2012 Oracle. All rights reserved.
+ * Copyright (c) 2010, 2015 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -35,12 +35,160 @@ public interface ModifiableIntReference
int setZero();
/**
- * Increment and return the <code>int</code> value.
+ * Increment the <code>int</code> value by one.
+ * Return the new value.
*/
int increment();
/**
- * Decrement and return the <code>int</code> value.
+ * Increment the <code>int</code> value by one.
+ * Return the new value.
+ * @see Math#incrementExact(int)
+ */
+ int incrementExact();
+
+ /**
+ * Decrement the <code>int</code> value by one.
+ * Return the new value.
*/
int decrement();
+
+ /**
+ * Decrement the <code>int</code> value by one.
+ * Return the new value.
+ * @see Math#decrementExact(int)
+ */
+ int decrementExact();
+
+ /**
+ * Halve the <code>int</code> value.
+ * Return the new value.
+ */
+ int halve();
+
+ /**
+ * Double the <code>int</code> value.
+ * Return the new value.
+ */
+ int twice();
+
+ /**
+ * Double the <code>int</code> value.
+ * Return the new value.
+ */
+ int twiceExact();
+
+ /**
+ * Set <code>int</code> value to its absolute value.
+ * Return the new value.
+ */
+ int abs();
+
+ /**
+ * Set <code>int</code> value to its negative value.
+ * Return the new value.
+ */
+ int negate();
+
+ /**
+ * Set <code>int</code> value to its negative value.
+ * Return the new value.
+ * @see Math#negateExact(int)
+ */
+ int negateExact();
+
+ /**
+ * Add the specified value to the <code>int</code> value.
+ * Return the new value.
+ */
+ int add(int i);
+
+ /**
+ * Add the specified value to the <code>int</code> value.
+ * Return the new value.
+ * @see Math#addExact(int, int)
+ */
+ int addExact(int i);
+
+ /**
+ * Subtract the specified value from the <code>int</code> value.
+ * Return the new value.
+ */
+ int subtract(int i);
+
+ /**
+ * Subtract the specified value from the <code>int</code> value.
+ * Return the new value.
+ * @see Math#subtractExact(int, int)
+ */
+ int subtractExact(int i);
+
+ /**
+ * Multiply the <code>int</code> value by the specified value.
+ * Return the new value.
+ */
+ int multiply(int i);
+
+ /**
+ * Multiply the <code>int</code> value by the specified value.
+ * Return the new value.
+ * @see Math#multiplyExact(int, int)
+ */
+ int multiplyExact(int i);
+
+ /**
+ * Divide the <code>int</code> value by the specified value.
+ * Return the new value.
+ */
+ int divide(int i);
+
+ /**
+ * Divide the <code>int</code> value by the specified value.
+ * Return the new value.
+ * @see Math#floorDiv(int, int)
+ */
+ int floorDivide(int i);
+
+ /**
+ * Divide the <code>int</code> value by the specified value and set the
+ * <code>int</code> value to the remainder.
+ * Return the new value.
+ */
+ int remainder(int i);
+
+ /**
+ * Divide the <code>int</code> value by the specified value and set the
+ * <code>int</code> value to the remainder.
+ * Return the new value.
+ * @see Math#floorMod(int, int)
+ */
+ int floorRemainder(int i);
+
+ /**
+ * Set the <code>int</code> value to the minimum of itself and the
+ * specified value.
+ * Return the new value.
+ */
+ int min(int i);
+
+ /**
+ * Set the <code>int</code> value to the maximum of itself and the
+ * specified value.
+ * Return the new value.
+ */
+ int max(int i);
+
+ /**
+ * Set the <code>int</code> value to the specified new value if it is
+ * currently the specified expected value.
+ * Return whether the set was successful.
+ */
+ boolean commit(int newValue, int expectedValue);
+
+ /**
+ * Swap the <code>int</code> value of the <code>int</code> reference with
+ * the value of the specified <code>int</code> reference.
+ * Return the new value.
+ */
+ int swap(ModifiableIntReference other);
}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractIntReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractIntReferenceTests.java
new file mode 100644
index 0000000000..9926fb90a7
--- /dev/null
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/AbstractIntReferenceTests.java
@@ -0,0 +1,77 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0, which accompanies this distribution
+ * and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.tests.internal.reference;
+
+import org.eclipse.jpt.common.utility.internal.reference.AbstractIntReference;
+import junit.framework.TestCase;
+
+@SuppressWarnings("nls")
+public class AbstractIntReferenceTests
+ extends TestCase
+{
+ public AbstractIntReferenceTests(String name) {
+ super(name);
+ }
+
+ public void testGetValue() {
+ LocalIntReference br = new LocalIntReference();
+ assertEquals(42, br.getValue());
+ }
+
+ public void testEqualsInt() {
+ LocalIntReference br = new LocalIntReference();
+ assertTrue(br.equals(42));
+ assertFalse(br.equals(0));
+ }
+
+ public void testNotEqual() {
+ LocalIntReference br = new LocalIntReference();
+ assertFalse(br.notEqual(42));
+ assertTrue(br.notEqual(0));
+ }
+
+ public void testIsZero() {
+ LocalIntReference br = new LocalIntReference();
+ assertFalse(br.isZero());
+ }
+
+ public void testIsNotZero() {
+ LocalIntReference br = new LocalIntReference();
+ assertTrue(br.isNotZero());
+ }
+
+ public void testEquals() {
+ LocalIntReference br1 = new LocalIntReference();
+ LocalIntReference br2 = new LocalIntReference();
+ assertTrue(br1.equals(br1));
+ assertFalse(br1.equals(br2));
+ }
+
+ public void testHashCode() {
+ LocalIntReference br = new LocalIntReference();
+ assertEquals(br.hashCode(), br.hashCode());
+ }
+
+ public void testToString() {
+ LocalIntReference br = new LocalIntReference();
+ assertEquals("[42]", br.toString());
+ }
+
+ private class LocalIntReference
+ extends AbstractIntReference
+ {
+ LocalIntReference() {
+ super();
+ }
+ public int getValue() {
+ return 42;
+ }
+ }
+}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/JptCommonUtilityReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/JptCommonUtilityReferenceTests.java
index 719d0195f7..08fbd16f7a 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/JptCommonUtilityReferenceTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/JptCommonUtilityReferenceTests.java
@@ -21,6 +21,7 @@ public class JptCommonUtilityReferenceTests {
TestSuite suite = new TestSuite(JptCommonUtilityReferenceTests.class.getPackage().getName());
suite.addTestSuite(AbstractBooleanReferenceTests.class);
+ suite.addTestSuite(AbstractIntReferenceTests.class);
suite.addTestSuite(SimpleBooleanReferenceTests.class);
suite.addTestSuite(SimpleIntReferenceTests.class);
suite.addTestSuite(SimpleObjectReferenceTests.class);
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SimpleIntReferenceTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SimpleIntReferenceTests.java
index 84fcab0352..f85d7cd6c3 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SimpleIntReferenceTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SimpleIntReferenceTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2010 Oracle. All rights reserved.
+ * Copyright (c) 2007, 2015 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -14,8 +14,9 @@ import org.eclipse.jpt.common.utility.internal.reference.SimpleIntReference;
import org.eclipse.jpt.common.utility.tests.internal.TestTools;
@SuppressWarnings("nls")
-public class SimpleIntReferenceTests extends TestCase {
-
+public class SimpleIntReferenceTests
+ extends TestCase
+{
public SimpleIntReferenceTests(String name) {
super(name);
}
@@ -173,11 +174,11 @@ public class SimpleIntReferenceTests extends TestCase {
SimpleIntReference ir;
ir = new SimpleIntReference(-3);
assertEquals(-3, ir.getValue());
- assertEquals(3, ir.neg());
+ assertEquals(3, ir.negate());
ir.setValue(3);
assertEquals(3, ir.getValue());
- assertEquals(-3, ir.neg());
+ assertEquals(-3, ir.negate());
}
public void testSetZero() {
@@ -264,7 +265,8 @@ public class SimpleIntReferenceTests extends TestCase {
ir = new SimpleIntReference(25);
assertEquals(25, ir.getValue());
assertEquals(3, ir.min(3));
- assertEquals(25, ir.min(33));
+ assertEquals(3, ir.getValue());
+ assertEquals(3, ir.min(33));
}
public void testMaxInt() {
@@ -275,13 +277,6 @@ public class SimpleIntReferenceTests extends TestCase {
assertEquals(30, ir.max(30));
}
- public void testPowInt() {
- SimpleIntReference ir;
- ir = new SimpleIntReference(5);
- assertEquals(5, ir.getValue());
- assertTrue(ir.pow(2) == 25L);
- }
-
public void testCompareToIntReference() {
SimpleIntReference ir1 = new SimpleIntReference(44);
SimpleIntReference ir2 = new SimpleIntReference(44);
@@ -311,5 +306,4 @@ public class SimpleIntReferenceTests extends TestCase {
ir = new SimpleIntReference(5);
assertEquals("[5]", ir.toString());
}
-
}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedIntTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedIntTests.java
index 2d1c825adc..df921544c6 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedIntTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/reference/SynchronizedIntTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2012 Oracle. All rights reserved.
+ * Copyright (c) 2009, 2015 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -161,12 +161,12 @@ public class SynchronizedIntTests
}
public void testNeg() throws Exception {
- assertEquals(0, this.si.neg());
+ assertEquals(0, this.si.negate());
assertEquals(0, this.si.getValue());
this.si.setValue(this.value);
- assertEquals(-this.value, this.si.neg());
+ assertEquals(-this.value, this.si.negate());
this.si.setValue(-this.value);
- assertEquals(this.value, this.si.neg());
+ assertEquals(this.value, this.si.negate());
}
public void testSetZero() throws Exception {

Back to the top