diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d1785b7d0..c35e734ae 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -15,6 +15,30 @@ See the License for the specific language governing permissions and limitations under the License. --> + + + + Apache Commons Lang Changes @@ -22,6 +46,7 @@ + Add getAndIncrement/getAndDecrement/getAndAdd/incrementAndGet/decrementAndGet/addAndGet in Mutable* classes Optimize BitField constructor implementation Improve CharSetUtils.squeeze() performance Add RandomStringUtils#randomGraph and #randomPrint which match corresponding regular expression class diff --git a/src/main/java/org/apache/commons/lang3/mutable/MutableByte.java b/src/main/java/org/apache/commons/lang3/mutable/MutableByte.java index 80edf3a01..b6674d85b 100644 --- a/src/main/java/org/apache/commons/lang3/mutable/MutableByte.java +++ b/src/main/java/org/apache/commons/lang3/mutable/MutableByte.java @@ -119,6 +119,29 @@ public void increment() { value++; } + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the increment operation. This method is not thread safe. + * + * @return the value associated with the instance before it was incremented + */ + public byte getAndIncrement() { + byte last = value; + value++; + return last; + } + + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately after the increment operation. This method is not thread safe. + * + * @return the value associated with the instance after it is incremented + */ + public byte incrementAndGet() { + value++; + return value; + } + /** * Decrements the value. * @@ -128,6 +151,29 @@ public void decrement() { value--; } + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance before it was decremented + */ + public byte getAndDecrement() { + byte last = value; + value--; + return last; + } + + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately after the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance after it is decremented + */ + public byte decrementAndGet() { + value--; + return value; + } + //----------------------------------------------------------------------- /** * Adds a value to the value of this instance. @@ -171,6 +217,58 @@ public void subtract(final Number operand) { this.value -= operand.byteValue(); } + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance after adding the operand + */ + public byte addAndGet(final byte operand) { + this.value += operand; + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance after adding the operand + */ + public byte addAndGet(final Number operand) { + this.value += operand.byteValue(); + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance immediately before the operand was added + */ + public byte getAndAdd(final byte operand) { + byte last = value; + this.value += operand; + return last; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance immediately before the operand was added + */ + public byte getAndAdd(final Number operand) { + byte last = value; + this.value += operand.byteValue(); + return last; + } + //----------------------------------------------------------------------- // shortValue relies on Number implementation /** diff --git a/src/main/java/org/apache/commons/lang3/mutable/MutableDouble.java b/src/main/java/org/apache/commons/lang3/mutable/MutableDouble.java index 855b04cca..633b8fb66 100644 --- a/src/main/java/org/apache/commons/lang3/mutable/MutableDouble.java +++ b/src/main/java/org/apache/commons/lang3/mutable/MutableDouble.java @@ -136,6 +136,29 @@ public void increment() { value++; } + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the increment operation. This method is not thread safe. + * + * @return the value associated with the instance before it was incremented + */ + public double getAndIncrement() { + double last = value; + value++; + return last; + } + + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately after the increment operation. This method is not thread safe. + * + * @return the value associated with the instance after it is incremented + */ + public double incrementAndGet() { + value++; + return value; + } + /** * Decrements the value. * @@ -145,6 +168,29 @@ public void decrement() { value--; } + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance before it was decremented + */ + public double getAndDecrement() { + double last = value; + value--; + return last; + } + + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately after the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance after it is decremented + */ + public double decrementAndGet() { + value--; + return value; + } + //----------------------------------------------------------------------- /** * Adds a value to the value of this instance. @@ -188,6 +234,58 @@ public void subtract(final Number operand) { this.value -= operand.doubleValue(); } + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance after adding the operand + */ + public double addAndGet(final double operand) { + this.value += operand; + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance after adding the operand + */ + public double addAndGet(final Number operand) { + this.value += operand.doubleValue(); + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance immediately before the operand was added + */ + public double getAndAdd(final double operand) { + double last = value; + this.value += operand; + return last; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance immediately before the operand was added + */ + public double getAndAdd(final Number operand) { + double last = value; + this.value += operand.doubleValue(); + return last; + } + //----------------------------------------------------------------------- // shortValue and byteValue rely on Number implementation /** diff --git a/src/main/java/org/apache/commons/lang3/mutable/MutableFloat.java b/src/main/java/org/apache/commons/lang3/mutable/MutableFloat.java index 7f798f7ec..9fc295561 100644 --- a/src/main/java/org/apache/commons/lang3/mutable/MutableFloat.java +++ b/src/main/java/org/apache/commons/lang3/mutable/MutableFloat.java @@ -136,6 +136,29 @@ public void increment() { value++; } + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the increment operation. This method is not thread safe. + * + * @return the value associated with the instance before it was incremented + */ + public float getAndIncrement() { + float last = value; + value++; + return last; + } + + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately after the increment operation. This method is not thread safe. + * + * @return the value associated with the instance after it is incremented + */ + public float incrementAndGet() { + value++; + return value; + } + /** * Decrements the value. * @@ -145,6 +168,29 @@ public void decrement() { value--; } + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance before it was decremented + */ + public float getAndDecrement() { + float last = value; + value--; + return last; + } + + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately after the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance after it is decremented + */ + public float decrementAndGet() { + value--; + return value; + } + //----------------------------------------------------------------------- /** * Adds a value to the value of this instance. @@ -188,6 +234,58 @@ public void subtract(final Number operand) { this.value -= operand.floatValue(); } + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance after adding the operand + */ + public float addAndGet(final float operand) { + this.value += operand; + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance after adding the operand + */ + public float addAndGet(final Number operand) { + this.value += operand.floatValue(); + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance immediately before the operand was added + */ + public float getAndAdd(final float operand) { + float last = value; + this.value += operand; + return last; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance immediately before the operand was added + */ + public float getAndAdd(final Number operand) { + float last = value; + this.value += operand.floatValue(); + return last; + } + //----------------------------------------------------------------------- // shortValue and byteValue rely on Number implementation /** diff --git a/src/main/java/org/apache/commons/lang3/mutable/MutableInt.java b/src/main/java/org/apache/commons/lang3/mutable/MutableInt.java index 1b86adede..dd490cd93 100644 --- a/src/main/java/org/apache/commons/lang3/mutable/MutableInt.java +++ b/src/main/java/org/apache/commons/lang3/mutable/MutableInt.java @@ -119,6 +119,29 @@ public void increment() { value++; } + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the increment operation. This method is not thread safe. + * + * @return the value associated with the instance before it was incremented + */ + public int getAndIncrement() { + int last = value; + value++; + return last; + } + + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately after the increment operation. This method is not thread safe. + * + * @return the value associated with the instance after it is incremented + */ + public int incrementAndGet() { + value++; + return value; + } + /** * Decrements the value. * @@ -128,6 +151,29 @@ public void decrement() { value--; } + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance before it was decremented + */ + public int getAndDecrement() { + int last = value; + value--; + return last; + } + + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately after the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance after it is decremented + */ + public int decrementAndGet() { + value--; + return value; + } + //----------------------------------------------------------------------- /** * Adds a value to the value of this instance. @@ -171,6 +217,58 @@ public void subtract(final Number operand) { this.value -= operand.intValue(); } + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance after adding the operand + */ + public int addAndGet(final int operand) { + this.value += operand; + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance after adding the operand + */ + public int addAndGet(final Number operand) { + this.value += operand.intValue(); + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance immediately before the operand was added + */ + public int getAndAdd(final int operand) { + int last = value; + this.value += operand; + return last; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance immediately before the operand was added + */ + public int getAndAdd(final Number operand) { + int last = value; + this.value += operand.intValue(); + return last; + } + //----------------------------------------------------------------------- // shortValue and byteValue rely on Number implementation /** diff --git a/src/main/java/org/apache/commons/lang3/mutable/MutableLong.java b/src/main/java/org/apache/commons/lang3/mutable/MutableLong.java index d9391c4a3..01bbef62c 100644 --- a/src/main/java/org/apache/commons/lang3/mutable/MutableLong.java +++ b/src/main/java/org/apache/commons/lang3/mutable/MutableLong.java @@ -119,6 +119,29 @@ public void increment() { value++; } + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the increment operation. This method is not thread safe. + * + * @return the value associated with the instance before it was incremented + */ + public long getAndIncrement() { + long last = value; + value++; + return last; + } + + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately after the increment operation. This method is not thread safe. + * + * @return the value associated with the instance after it is incremented + */ + public long incrementAndGet() { + value++; + return value; + } + /** * Decrements the value. * @@ -128,6 +151,29 @@ public void decrement() { value--; } + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance before it was decremented + */ + public long getAndDecrement() { + long last = value; + value--; + return last; + } + + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately after the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance after it is decremented + */ + public long decrementAndGet() { + value--; + return value; + } + //----------------------------------------------------------------------- /** * Adds a value to the value of this instance. @@ -171,6 +217,58 @@ public void subtract(final Number operand) { this.value -= operand.longValue(); } + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance after adding the operand + */ + public long addAndGet(final long operand) { + this.value += operand; + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance after adding the operand + */ + public long addAndGet(final Number operand) { + this.value += operand.longValue(); + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance immediately before the operand was added + */ + public long getAndAdd(final long operand) { + long last = value; + this.value += operand; + return last; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance immediately before the operand was added + */ + public long getAndAdd(final Number operand) { + long last = value; + this.value += operand.longValue(); + return last; + } + //----------------------------------------------------------------------- // shortValue and byteValue rely on Number implementation /** diff --git a/src/main/java/org/apache/commons/lang3/mutable/MutableShort.java b/src/main/java/org/apache/commons/lang3/mutable/MutableShort.java index 531324506..2125f444a 100644 --- a/src/main/java/org/apache/commons/lang3/mutable/MutableShort.java +++ b/src/main/java/org/apache/commons/lang3/mutable/MutableShort.java @@ -119,6 +119,29 @@ public void increment() { value++; } + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the increment operation. This method is not thread safe. + * + * @return the value associated with the instance before it was incremented + */ + public short getAndIncrement() { + short last = value; + value++; + return last; + } + + /** + * Increments this instance's value by 1; this method returns the value associated with the instance + * immediately after the increment operation. This method is not thread safe. + * + * @return the value associated with the instance after it is incremented + */ + public short incrementAndGet() { + value++; + return value; + } + /** * Decrements the value. * @@ -128,6 +151,29 @@ public void decrement() { value--; } + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately prior to the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance before it was decremented + */ + public short getAndDecrement() { + short last = value; + value--; + return last; + } + + /** + * Decrements this instance's value by 1; this method returns the value associated with the instance + * immediately after the decrement operation. This method is not thread safe. + * + * @return the value associated with the instance after it is decremented + */ + public short decrementAndGet() { + value--; + return value; + } + //----------------------------------------------------------------------- /** * Adds a value to the value of this instance. @@ -171,6 +217,58 @@ public void subtract(final Number operand) { this.value -= operand.shortValue(); } + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance after adding the operand + */ + public short addAndGet(final short operand) { + this.value += operand; + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately after the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance after adding the operand + */ + public short addAndGet(final Number operand) { + this.value += operand.shortValue(); + return value; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @return the value associated with this instance immediately before the operand was added + */ + public short getAndAdd(final short operand) { + short last = value; + this.value += operand; + return last; + } + + /** + * Increments this instance's value by {@code operand}; this method returns the value associated with the instance + * immediately prior to the addition operation. This method is not thread safe. + * + * @param operand the quantity to add, not null + * @throws NullPointerException if {@code operand} is null + * @return the value associated with this instance immediately before the operand was added + */ + public short getAndAdd(final Number operand) { + short last = value; + this.value += operand.shortValue(); + return last; + } + //----------------------------------------------------------------------- // byteValue relies on Number implementation /** diff --git a/src/test/java/org/apache/commons/lang3/mutable/MutableByteTest.java b/src/test/java/org/apache/commons/lang3/mutable/MutableByteTest.java index 37bddfa6c..4903730f2 100644 --- a/src/test/java/org/apache/commons/lang3/mutable/MutableByteTest.java +++ b/src/test/java/org/apache/commons/lang3/mutable/MutableByteTest.java @@ -142,6 +142,26 @@ public void testIncrement() { assertEquals(2L, mutNum.longValue()); } + @Test + public void testIncrementAndGet() { + final MutableByte mutNum = new MutableByte((byte) 1); + byte result = mutNum.incrementAndGet(); + + assertEquals(2, result); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + + @Test + public void testGetAndIncrement() { + final MutableByte mutNum = new MutableByte((byte) 1); + byte result = mutNum.getAndIncrement(); + + assertEquals(1, result); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + @Test public void testDecrement() { final MutableByte mutNum = new MutableByte((byte) 1); @@ -151,6 +171,26 @@ public void testDecrement() { assertEquals(0L, mutNum.longValue()); } + @Test + public void testDecrementAndGet() { + final MutableByte mutNum = new MutableByte((byte) 1); + byte result = mutNum.decrementAndGet(); + + assertEquals(0, result); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + + @Test + public void testGetAndDecrement() { + final MutableByte mutNum = new MutableByte((byte) 1); + byte result = mutNum.getAndDecrement(); + + assertEquals(1, result); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + @Test public void testAddValuePrimitive() { final MutableByte mutNum = new MutableByte((byte) 1); @@ -167,6 +207,42 @@ public void testAddValueObject() { assertEquals((byte) 2, mutNum.byteValue()); } + @Test + public void testGetAndAddValuePrimitive() { + final MutableByte mutableByte = new MutableByte((byte)0); + byte result = mutableByte.getAndAdd((byte) 1); + + assertEquals((byte) 0, result); + assertEquals((byte) 1, mutableByte.byteValue()); + } + + @Test + public void testGetAndAddValueObject() { + final MutableByte mutableByte = new MutableByte((byte)0); + byte result = mutableByte.getAndAdd(Byte.valueOf((byte) 1)); + + assertEquals((byte) 0, result); + assertEquals((byte) 1, mutableByte.byteValue()); + } + + @Test + public void testAddAndGetValuePrimitive() { + final MutableByte mutableByte = new MutableByte((byte)0); + byte result = mutableByte.addAndGet((byte) 1); + + assertEquals((byte) 1, result); + assertEquals((byte) 1, mutableByte.byteValue()); + } + + @Test + public void testAddAndGetValueObject() { + final MutableByte mutableByte = new MutableByte((byte)0); + byte result = mutableByte.addAndGet(Byte.valueOf((byte) 1)); + + assertEquals((byte) 1, result); + assertEquals((byte) 1, mutableByte.byteValue()); + } + @Test public void testSubtractValuePrimitive() { final MutableByte mutNum = new MutableByte((byte) 1); diff --git a/src/test/java/org/apache/commons/lang3/mutable/MutableDoubleTest.java b/src/test/java/org/apache/commons/lang3/mutable/MutableDoubleTest.java index af071fc3c..95dab5990 100644 --- a/src/test/java/org/apache/commons/lang3/mutable/MutableDoubleTest.java +++ b/src/test/java/org/apache/commons/lang3/mutable/MutableDoubleTest.java @@ -154,6 +154,26 @@ public void testIncrement() { assertEquals(2L, mutNum.longValue()); } + @Test + public void testIncrementAndGet() { + final MutableDouble mutNum = new MutableDouble(1d); + double result = mutNum.incrementAndGet(); + + assertEquals(2d, result, 0.01d); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + + @Test + public void testGetAndIncrement() { + final MutableDouble mutNum = new MutableDouble(1d); + double result = mutNum.getAndIncrement(); + + assertEquals(1d, result, 0.01d); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + @Test public void testDecrement() { final MutableDouble mutNum = new MutableDouble(1); @@ -163,6 +183,26 @@ public void testDecrement() { assertEquals(0L, mutNum.longValue()); } + @Test + public void testDecrementAndGet() { + final MutableDouble mutNum = new MutableDouble(1d); + double result = mutNum.decrementAndGet(); + + assertEquals(0d, result, 0.01d); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + + @Test + public void testGetAndDecrement() { + final MutableDouble mutNum = new MutableDouble(1d); + double result = mutNum.getAndDecrement(); + + assertEquals(1d, result, 0.01d); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + @Test public void testAddValuePrimitive() { final MutableDouble mutNum = new MutableDouble(1); @@ -179,6 +219,42 @@ public void testAddValueObject() { assertEquals(2.1d, mutNum.doubleValue(), 0.01d); } + @Test + public void testGetAndAddValuePrimitive() { + final MutableDouble mutableDouble = new MutableDouble(0.5d); + double result = mutableDouble.getAndAdd(1d); + + assertEquals(0.5d, result, 0.01d); + assertEquals(1.5d, mutableDouble.doubleValue(), 0.01d); + } + + @Test + public void testGetAndAddValueObject() { + final MutableDouble mutableDouble = new MutableDouble(0.5d); + double result = mutableDouble.getAndAdd(Double.valueOf(2d)); + + assertEquals(0.5d, result, 0.01d); + assertEquals(2.5d, mutableDouble.doubleValue(), 0.01d); + } + + @Test + public void testAddAndGetValuePrimitive() { + final MutableDouble mutableDouble = new MutableDouble(10.5d); + double result = mutableDouble.addAndGet(-0.5d); + + assertEquals(10d, result, 0.01d); + assertEquals(10d, mutableDouble.doubleValue(), 0.01d); + } + + @Test + public void testAddAndGetValueObject() { + final MutableDouble mutableDouble = new MutableDouble(7.5d); + double result = mutableDouble.addAndGet(Double.valueOf(-2.5d)); + + assertEquals(5d, result, 0.01d); + assertEquals(5d, mutableDouble.doubleValue(), 0.01d); + } + @Test public void testSubtractValuePrimitive() { final MutableDouble mutNum = new MutableDouble(1); diff --git a/src/test/java/org/apache/commons/lang3/mutable/MutableFloatTest.java b/src/test/java/org/apache/commons/lang3/mutable/MutableFloatTest.java index 2f67c138b..f507cb457 100644 --- a/src/test/java/org/apache/commons/lang3/mutable/MutableFloatTest.java +++ b/src/test/java/org/apache/commons/lang3/mutable/MutableFloatTest.java @@ -154,6 +154,26 @@ public void testIncrement() { assertEquals(2L, mutNum.longValue()); } + @Test + public void testIncrementAndGet() { + final MutableFloat mutNum = new MutableFloat(1f); + float result = mutNum.incrementAndGet(); + + assertEquals(2f, result, 0.01f); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + + @Test + public void testGetAndIncrement() { + final MutableFloat mutNum = new MutableFloat(1f); + float result = mutNum.getAndIncrement(); + + assertEquals(1f, result, 0.01f); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + @Test public void testDecrement() { final MutableFloat mutNum = new MutableFloat(1); @@ -163,6 +183,26 @@ public void testDecrement() { assertEquals(0L, mutNum.longValue()); } + @Test + public void testDecrementAndGet() { + final MutableFloat mutNum = new MutableFloat(1f); + float result = mutNum.decrementAndGet(); + + assertEquals(0f, result, 0.01f); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + + @Test + public void testGetAndDecrement() { + final MutableFloat mutNum = new MutableFloat(1f); + float result = mutNum.getAndDecrement(); + + assertEquals(1f, result, 0.01f); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + @Test public void testAddValuePrimitive() { final MutableFloat mutNum = new MutableFloat(1); @@ -179,6 +219,42 @@ public void testAddValueObject() { assertEquals(2.1f, mutNum.floatValue(), 0.01f); } + @Test + public void testGetAndAddValuePrimitive() { + final MutableFloat mutableFloat = new MutableFloat(1.25f); + float result = mutableFloat.getAndAdd(0.75f); + + assertEquals(1.25f, result, 0.01f); + assertEquals(2f, mutableFloat.floatValue(), 0.01f); + } + + @Test + public void testGetAndAddValueObject() { + final MutableFloat mutableFloat = new MutableFloat(7.75f); + float result = mutableFloat.getAndAdd(Float.valueOf(2.25f)); + + assertEquals(7.75f, result, 0.01f); + assertEquals(10f, mutableFloat.floatValue(), 0.01f); + } + + @Test + public void testAddAndGetValuePrimitive() { + final MutableFloat mutableFloat = new MutableFloat(0.5f); + float result = mutableFloat.addAndGet(1f); + + assertEquals(1.5f, result, 0.01f); + assertEquals(1.5f, mutableFloat.floatValue(), 0.01f); + } + + @Test + public void testAddAndGetValueObject() { + final MutableFloat mutableFloat = new MutableFloat(5f); + float result = mutableFloat.addAndGet(Float.valueOf(2.5f)); + + assertEquals(7.5f, result, 0.01f); + assertEquals(7.5f, mutableFloat.floatValue(), 0.01f); + } + @Test public void testSubtractValuePrimitive() { final MutableFloat mutNum = new MutableFloat(1); diff --git a/src/test/java/org/apache/commons/lang3/mutable/MutableIntTest.java b/src/test/java/org/apache/commons/lang3/mutable/MutableIntTest.java index e18fd5fc7..d21c7a3aa 100644 --- a/src/test/java/org/apache/commons/lang3/mutable/MutableIntTest.java +++ b/src/test/java/org/apache/commons/lang3/mutable/MutableIntTest.java @@ -148,6 +148,26 @@ public void testIncrement() { assertEquals(2L, mutNum.longValue()); } + @Test + public void testIncrementAndGet() { + final MutableInt mutNum = new MutableInt((int) 1); + int result = mutNum.incrementAndGet(); + + assertEquals(2, result); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + + @Test + public void testGetAndIncrement() { + final MutableInt mutNum = new MutableInt((int) 1); + int result = mutNum.getAndIncrement(); + + assertEquals(1, result); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + @Test public void testDecrement() { final MutableInt mutNum = new MutableInt(1); @@ -157,6 +177,26 @@ public void testDecrement() { assertEquals(0L, mutNum.longValue()); } + @Test + public void testDecrementAndGet() { + final MutableInt mutNum = new MutableInt((int) 1); + int result = mutNum.decrementAndGet(); + + assertEquals(0, result); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + + @Test + public void testGetAndDecrement() { + final MutableInt mutNum = new MutableInt((int) 1); + int result = mutNum.getAndDecrement(); + + assertEquals(1, result); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + @Test public void testAddValuePrimitive() { final MutableInt mutNum = new MutableInt(1); @@ -175,6 +215,42 @@ public void testAddValueObject() { assertEquals(2L, mutNum.longValue()); } + @Test + public void testGetAndAddValuePrimitive() { + final MutableInt mutableInteger = new MutableInt((int)0); + int result = mutableInteger.getAndAdd((int) 1); + + assertEquals((int) 0, result); + assertEquals((int) 1, mutableInteger.intValue()); + } + + @Test + public void testGetAndAddValueObject() { + final MutableInt mutableInteger = new MutableInt((int)0); + int result = mutableInteger.getAndAdd(Integer.valueOf((int) 1)); + + assertEquals((int) 0, result); + assertEquals((int) 1, mutableInteger.intValue()); + } + + @Test + public void testAddAndGetValuePrimitive() { + final MutableInt mutableInteger = new MutableInt((int)0); + int result = mutableInteger.addAndGet((int) 1); + + assertEquals((int) 1, result); + assertEquals((int) 1, mutableInteger.intValue()); + } + + @Test + public void testAddAndGetValueObject() { + final MutableInt mutableInteger = new MutableInt((int)0); + int result = mutableInteger.addAndGet(Integer.valueOf((int) 1)); + + assertEquals((int) 1, result); + assertEquals((int) 1, mutableInteger.intValue()); + } + @Test public void testSubtractValuePrimitive() { final MutableInt mutNum = new MutableInt(1); diff --git a/src/test/java/org/apache/commons/lang3/mutable/MutableLongTest.java b/src/test/java/org/apache/commons/lang3/mutable/MutableLongTest.java index 936c4ef30..c2e66a2c7 100644 --- a/src/test/java/org/apache/commons/lang3/mutable/MutableLongTest.java +++ b/src/test/java/org/apache/commons/lang3/mutable/MutableLongTest.java @@ -142,6 +142,26 @@ public void testIncrement() { assertEquals(2L, mutNum.longValue()); } + @Test + public void testIncrementAndGet() { + final MutableLong mutNum = new MutableLong((long) 1); + long result = mutNum.incrementAndGet(); + + assertEquals(2, result); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + + @Test + public void testGetAndIncrement() { + final MutableLong mutNum = new MutableLong((long) 1); + long result = mutNum.getAndIncrement(); + + assertEquals(1, result); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + @Test public void testDecrement() { final MutableLong mutNum = new MutableLong(1); @@ -151,6 +171,26 @@ public void testDecrement() { assertEquals(0L, mutNum.longValue()); } + @Test + public void testDecrementAndGet() { + final MutableLong mutNum = new MutableLong((long) 1); + long result = mutNum.decrementAndGet(); + + assertEquals(0, result); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + + @Test + public void testGetAndDecrement() { + final MutableLong mutNum = new MutableLong((long) 1); + long result = mutNum.getAndDecrement(); + + assertEquals(1, result); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + @Test public void testAddValuePrimitive() { final MutableLong mutNum = new MutableLong(1); @@ -169,6 +209,42 @@ public void testAddValueObject() { assertEquals(2L, mutNum.longValue()); } + @Test + public void testGetAndAddValuePrimitive() { + final MutableLong mutableLong = new MutableLong((long)0); + long result = mutableLong.getAndAdd((long) 1); + + assertEquals((long) 0, result); + assertEquals((long) 1, mutableLong.longValue()); + } + + @Test + public void testGetAndAddValueObject() { + final MutableLong mutableLong = new MutableLong((long)0); + long result = mutableLong.getAndAdd(Long.valueOf((long) 1)); + + assertEquals((long) 0, result); + assertEquals((long) 1, mutableLong.longValue()); + } + + @Test + public void testAddAndGetValuePrimitive() { + final MutableLong mutableLong = new MutableLong((long)0); + long result = mutableLong.addAndGet((long) 1); + + assertEquals((long) 1, result); + assertEquals((long) 1, mutableLong.longValue()); + } + + @Test + public void testAddAndGetValueObject() { + final MutableLong mutableLong = new MutableLong((long)0); + long result = mutableLong.addAndGet(Long.valueOf((long) 1)); + + assertEquals((long) 1, result); + assertEquals((long) 1, mutableLong.longValue()); + } + @Test public void testSubtractValuePrimitive() { final MutableLong mutNum = new MutableLong(1); diff --git a/src/test/java/org/apache/commons/lang3/mutable/MutableShortTest.java b/src/test/java/org/apache/commons/lang3/mutable/MutableShortTest.java index a177185ef..5cd597d9f 100644 --- a/src/test/java/org/apache/commons/lang3/mutable/MutableShortTest.java +++ b/src/test/java/org/apache/commons/lang3/mutable/MutableShortTest.java @@ -137,6 +137,26 @@ public void testIncrement() { assertEquals(2L, mutNum.longValue()); } + @Test + public void testIncrementAndGet() { + final MutableShort mutNum = new MutableShort((short) 1); + short result = mutNum.incrementAndGet(); + + assertEquals(2, result); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + + @Test + public void testGetAndIncrement() { + final MutableShort mutNum = new MutableShort((short) 1); + short result = mutNum.getAndIncrement(); + + assertEquals(1, result); + assertEquals(2, mutNum.intValue()); + assertEquals(2L, mutNum.longValue()); + } + @Test public void testDecrement() { final MutableShort mutNum = new MutableShort((short) 1); @@ -146,6 +166,26 @@ public void testDecrement() { assertEquals(0L, mutNum.longValue()); } + @Test + public void testDecrementAndGet() { + final MutableShort mutNum = new MutableShort((short) 1); + short result = mutNum.decrementAndGet(); + + assertEquals(0, result); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + + @Test + public void testGetAndDecrement() { + final MutableShort mutNum = new MutableShort((short) 1); + short result = mutNum.getAndDecrement(); + + assertEquals(1, result); + assertEquals(0, mutNum.intValue()); + assertEquals(0L, mutNum.longValue()); + } + @Test public void testAddValuePrimitive() { final MutableShort mutNum = new MutableShort((short) 1); @@ -162,6 +202,42 @@ public void testAddValueObject() { assertEquals((short) 2, mutNum.shortValue()); } + @Test + public void testGetAndAddValuePrimitive() { + final MutableShort mutableShort = new MutableShort((short)0); + short result = mutableShort.getAndAdd((short) 1); + + assertEquals((short) 0, result); + assertEquals((short) 1, mutableShort.shortValue()); + } + + @Test + public void testGetAndAddValueObject() { + final MutableShort mutableShort = new MutableShort((short)0); + short result = mutableShort.getAndAdd(Short.valueOf((short) 1)); + + assertEquals((short) 0, result); + assertEquals((short) 1, mutableShort.shortValue()); + } + + @Test + public void testAddAndGetValuePrimitive() { + final MutableShort mutableShort = new MutableShort((short) 0); + short result = mutableShort.addAndGet((short) 1); + + assertEquals((short) 1, result); + assertEquals((short) 1, mutableShort.shortValue()); + } + + @Test + public void testAddAndGetValueObject() { + final MutableShort mutableShort = new MutableShort((short) 0); + short result = mutableShort.addAndGet(Short.valueOf((short) 1)); + + assertEquals((short) 1, result); + assertEquals((short) 1, mutableShort.shortValue()); + } + @Test public void testSubtractValuePrimitive() { final MutableShort mutNum = new MutableShort((short) 1);