Update mutable package for release 2.1

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137873 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-07-07 23:50:28 +00:00
parent 43eba0aec4
commit fbeb9d58da
18 changed files with 1408 additions and 832 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,21 +15,33 @@
*/
package org.apache.commons.lang.mutable;
import java.io.Serializable;
/**
* A mutable <code>Byte</code>.
* A mutable <code>byte</code>.
*
* @since 2.1
* @version $Id: MutableByte.java,v 1.3 2004/06/24 04:20:46 bayard Exp $
* @version $Id: MutableByte.java,v 1.4 2004/07/07 23:50:28 scolebourne Exp $
*/
public class MutableByte extends MutableNumber {
public class MutableByte extends Number
implements Comparable, Mutable, Serializable {
/** Serialization lock. */
private static final long serialVersionUID = -1585823265L;
/** The mutable value. */
private byte value;
/**
* Internal value.
* Constructs a new MutableByte with the default value of zero.
*/
private byte value;
public MutableByte() {
super();
}
/**
* Instantiates with the specified value
* Constructs a new MutableByte with the specified value.
*
* @param value a value.
*/
public MutableByte(byte value) {
@ -37,34 +49,112 @@ public MutableByte(byte value) {
this.value = value;
}
public void setValue(byte value) {
this.value = value;
}
public long longValue() {
return this.value;
}
public double doubleValue() {
return this.value;
}
public int intValue() {
return this.value;
}
/**
* @return a <code>Byte</code>
* Constructs a new MutableByte with the specified value.
*
* @param value a value.
* @throws NullPointerException if the object is null
*/
public MutableByte(Number value) {
super();
this.value = value.byteValue();
}
//-----------------------------------------------------------------------
/**
* Gets the value as a Byte instance.
*
* @return the value as a Byte
*/
public Object getValue() {
return new Byte(this.value);
}
/**
* @param value a <code>Byte</code>
* Sets the value.
*
* @param value the value to set
*/
public void setValue(byte value) {
this.value = value;
}
/**
* Sets the value from any Number instance.
*
* @param value the value to set
* @throws NullPointerException if the object is null
* @throws ClassCastException if the type is invalid
*/
public void setValue(Object value) {
setValue(((Number)value).byteValue());
setValue(((Number) value).byteValue());
}
//-----------------------------------------------------------------------
public byte byteValue() {
return value;
}
public int intValue() {
return (int) value;
}
public long longValue() {
return (long) value;
}
public float floatValue() {
return (float) value;
}
public double doubleValue() {
return (double) value;
}
//-----------------------------------------------------------------------
/**
* Checks if this object equals the specified object.
* <p>
* The object must be a MutableByte with the same value to be equal.
*
* @param obj the object to compare to
* @return true if equal
*/
public boolean equals(Object obj) {
if (obj instanceof MutableByte) {
return (value == ((MutableByte) obj).value);
}
return false;
}
/**
* Returns a suitable hashcode for this mutable.
*
* @return a suitable hashcode
*/
public int hashCode() {
return (int) value;
}
/**
* Compares this mutable to another in ascending order.
*
* @param obj the mutable to compare to
* @return negative if this is less, zero if equal, positive if greater
*/
public int compareTo(Object obj) {
MutableByte other = (MutableByte) obj;
byte anotherVal = other.value;
return (value < anotherVal ? -1 : (value == anotherVal ? 0 : 1));
}
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
public String toString() {
return String.valueOf((int) value);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,21 +15,35 @@
*/
package org.apache.commons.lang.mutable;
import java.io.Serializable;
import org.apache.commons.lang.math.NumberUtils;
/**
* A mutable <code>Double</code>
* A mutable <code>double</code>.
*
* @since 2.1
* @version $Id: MutableDouble.java,v 1.3 2004/06/24 04:20:46 bayard Exp $
* @version $Id: MutableDouble.java,v 1.4 2004/07/07 23:50:28 scolebourne Exp $
*/
public class MutableDouble extends MutableNumber {
public class MutableDouble extends Number
implements Comparable, Mutable, Serializable {
/**
* Internal value.
*/
/** Serialization lock. */
private static final long serialVersionUID = 1587163916L;
/** The mutable value. */
private double value;
/**
* Instantiates with the specified value
* Constructs a new MutableDouble with the default value of zero.
*/
public MutableDouble() {
super();
}
/**
* Constructs a new MutableDouble with the specified value.
*
* @param value a value.
*/
public MutableDouble(double value) {
@ -37,28 +51,128 @@ public MutableDouble(double value) {
this.value = value;
}
public void setValue(double value) {
this.value = value;
}
public double doubleValue() {
return this.value;
}
public long longValue() {
return (long)this.value;
}
public int intValue() {
return (int)this.value;
/**
* Constructs a new MutableDouble with the specified value.
*
* @param value a value.
* @throws NullPointerException if the object is null
*/
public MutableDouble(Number value) {
super();
this.value = value.doubleValue();
}
//-----------------------------------------------------------------------
/**
* Gets the value as a Double instance.
*
* @return the value as a Double
*/
public Object getValue() {
return new Double(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(double value) {
this.value = value;
}
/**
* Sets the value from any Number instance.
*
* @param value the value to set
* @throws NullPointerException if the object is null
* @throws ClassCastException if the type is invalid
*/
public void setValue(Object value) {
setValue(((Number)value).doubleValue());
setValue(((Number) value).doubleValue());
}
//-----------------------------------------------------------------------
public int intValue() {
return (int) value;
}
public long longValue() {
return (long) value;
}
public float floatValue() {
return (float) value;
}
public double doubleValue() {
return value;
}
/**
* Checks whether the double value is the special NaN value.
*
* @return true if NaN
*/
public boolean isNaN() {
return Double.isNaN(value);
}
/**
* Checks whether the double value is infinite.
*
* @return true if infinite
*/
public boolean isInfinite() {
return Double.isInfinite(value);
}
//-----------------------------------------------------------------------
/**
* Checks if this object equals the specified object.
* <p>
* The object must be a MutableDouble with the same value to be equal.
*
* @param obj the object to compare to
* @return true if equal
*/
public boolean equals(Object obj) {
if (obj instanceof MutableDouble) {
double other = ((MutableDouble) obj).value;
return (Double.doubleToLongBits(other) == Double.doubleToLongBits(value));
}
return false;
}
/**
* Returns a suitable hashcode for this mutable.
*
* @return a suitable hashcode
*/
public int hashCode() {
long bits = Double.doubleToLongBits(value);
return (int)(bits ^ (bits >>> 32));
}
/**
* Compares this mutable to another in ascending order.
*
* @param obj the mutable to compare to
* @return negative if this is less, zero if equal, positive if greater
*/
public int compareTo(Object obj) {
MutableDouble other = (MutableDouble) obj;
double anotherVal = other.value;
return NumberUtils.compare(value, anotherVal);
}
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
public String toString() {
return String.valueOf(value);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,21 +15,34 @@
*/
package org.apache.commons.lang.mutable;
import java.io.Serializable;
import org.apache.commons.lang.math.NumberUtils;
/**
* A mutable <code>Float</code>
* A mutable <code>float</code>.
*
* @since 2.1
* @version $Id: MutableFloat.java,v 1.3 2004/06/24 04:20:46 bayard Exp $
* @version $Id: MutableFloat.java,v 1.4 2004/07/07 23:50:28 scolebourne Exp $
*/
public class MutableFloat extends MutableNumber {
public class MutableFloat extends Number
implements Comparable, Mutable, Serializable {
/**
* Internal value.
*/
/** Serialization lock. */
private static final long serialVersionUID = 5787169186L;
/** The mutable value. */
private float value;
/**
* Instantiates with the specified value
* Constructs a new MutableFloat with the default value of zero.
*/
public MutableFloat() {
super();
}
/**
* Constructs a new MutableFloat with the specified value.
*
* @param value a value.
*/
@ -38,28 +51,127 @@ public MutableFloat(float value) {
this.value = value;
}
public void setValue(float value) {
this.value = value;
}
public double doubleValue() {
return this.value;
}
public int intValue() {
return (int)this.value;
}
public long longValue() {
return (long)this.value;
/**
* Constructs a new MutableFloat with the specified value.
*
* @param value a value.
* @throws NullPointerException if the object is null
*/
public MutableFloat(Number value) {
super();
this.value = value.floatValue();
}
//-----------------------------------------------------------------------
/**
* Gets the value as a Float instance.
*
* @return the value as a Float
*/
public Object getValue() {
return new Float(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(float value) {
this.value = value;
}
/**
* Sets the value from any Number instance.
*
* @param value the value to set
* @throws NullPointerException if the object is null
* @throws ClassCastException if the type is invalid
*/
public void setValue(Object value) {
setValue(((Number)value).floatValue());
setValue(((Number) value).floatValue());
}
//-----------------------------------------------------------------------
public int intValue() {
return (int) value;
}
public long longValue() {
return (long) value;
}
public float floatValue() {
return value;
}
public double doubleValue() {
return (double) value;
}
/**
* Checks whether the float value is the special NaN value.
*
* @return true if NaN
*/
public boolean isNaN() {
return Float.isNaN(value);
}
/**
* Checks whether the float value is infinite.
*
* @return true if infinite
*/
public boolean isInfinite() {
return Float.isInfinite(value);
}
//-----------------------------------------------------------------------
/**
* Checks if this object equals the specified object.
* <p>
* The object must be a MutableFloat with the same value to be equal.
*
* @param obj the object to compare to
* @return true if equal
*/
public boolean equals(Object obj) {
if (obj instanceof MutableFloat) {
float other = ((MutableFloat) obj).value;
return (Float.floatToIntBits(other) == Float.floatToIntBits(value));
}
return false;
}
/**
* Returns a suitable hashcode for this mutable.
*
* @return a suitable hashcode
*/
public int hashCode() {
return Float.floatToIntBits(value);
}
/**
* Compares this mutable to another in ascending order.
*
* @param obj the mutable to compare to
* @return negative if this is less, zero if equal, positive if greater
*/
public int compareTo(Object obj) {
MutableFloat other = (MutableFloat) obj;
float anotherVal = other.value;
return NumberUtils.compare(value, anotherVal);
}
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
public String toString() {
return String.valueOf(value);
}
}

View File

@ -21,7 +21,7 @@
* A mutable <code>int</code>.
*
* @since 2.1
* @version $Id: MutableInt.java,v 1.1 2004/07/05 22:15:33 scolebourne Exp $
* @version $Id: MutableInt.java,v 1.2 2004/07/07 23:50:28 scolebourne Exp $
*/
public class MutableInt extends Number
implements Comparable, Mutable, Serializable {
@ -49,15 +49,43 @@ public MutableInt(int value) {
this.value = value;
}
/**
* Constructs a new MutableInt with the specified value.
*
* @param value a value.
* @throws NullPointerException if the object is null
*/
public MutableInt(Number value) {
super();
this.value = value.intValue();
}
//-----------------------------------------------------------------------
/**
* Gets the value as a Integer instance.
*
* @return the value as a Integer
*/
public Object getValue() {
return new Integer(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}
/**
* Sets the value from any Number instance.
*
* @param value the value to set
* @throws NullPointerException if the object is null
* @throws ClassCastException if the type is invalid
*/
public void setValue(Object value) {
setValue(((Number) value).intValue());
}

View File

@ -1,65 +0,0 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
/**
* A mutable <code>Integer</code>.
*
* @since 2.1
* @version $Id: MutableInteger.java,v 1.4 2004/06/27 05:23:47 bayard Exp $
*/
public class MutableInteger extends MutableNumber {
/**
* Internal value.
*/
private int value;
/**
* Instantiates with the specified value
*
* @param value a value.
*/
public MutableInteger(int value) {
super();
this.value = value;
}
public void setValue(int value) {
this.value = value;
}
public double doubleValue() {
return this.value;
}
public long longValue() {
return this.value;
}
public int intValue() {
return this.value;
}
public Object getValue() {
return new Integer(this.value);
}
public void setValue(Object value) {
setValue(((Number)value).intValue());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,50 +15,142 @@
*/
package org.apache.commons.lang.mutable;
import java.io.Serializable;
/**
* A mutable <code>Long</code>
* A mutable <code>long</code>.
*
* @since 2.1
* @version $Id: MutableLong.java,v 1.3 2004/06/24 04:20:46 bayard Exp $
* @version $Id: MutableLong.java,v 1.4 2004/07/07 23:50:28 scolebourne Exp $
*/
public class MutableLong extends MutableNumber {
public class MutableLong extends Number
implements Comparable, Mutable, Serializable {
/**
* Internal value.
*/
/** Serialization lock. */
private static final long serialVersionUID = 62986528375L;
/** The mutable value. */
private long value;
/**
* Instantiates with the specified value
* Constructs a new MutableLong with the default value of zero.
*/
public MutableLong() {
super();
}
/**
* Constructs a new MutableLong with the specified value.
*
* @param value a value.
*/
public MutableLong(long value) {
super();
setValue(value);
}
public void setValue(long value) {
this.value = value;
}
public double doubleValue() {
return this.value;
}
public long longValue() {
return this.value;
}
public int intValue() {
return (int)this.value;
/**
* Constructs a new MutableLong with the specified value.
*
* @param value a value.
* @throws NullPointerException if the object is null
*/
public MutableLong(Number value) {
super();
this.value = value.longValue();
}
//-----------------------------------------------------------------------
/**
* Gets the value as a Long instance.
*
* @return the value as a Long
*/
public Object getValue() {
return new Long(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(long value) {
this.value = value;
}
/**
* Sets the value from any Number instance.
*
* @param value the value to set
* @throws NullPointerException if the object is null
* @throws ClassCastException if the type is invalid
*/
public void setValue(Object value) {
setValue(((Number)value).longValue());
setValue(((Number) value).longValue());
}
//-----------------------------------------------------------------------
public int intValue() {
return (int) value;
}
public long longValue() {
return value;
}
public float floatValue() {
return (float) value;
}
public double doubleValue() {
return (double) value;
}
//-----------------------------------------------------------------------
/**
* Checks if this object equals the specified object.
* <p>
* The object must be a MutableLong with the same value to be equal.
*
* @param obj the object to compare to
* @return true if equal
*/
public boolean equals(Object obj) {
if (obj instanceof MutableLong) {
return (value == ((MutableLong) obj).value);
}
return false;
}
/**
* Returns a suitable hashcode for this mutable.
*
* @return a suitable hashcode
*/
public int hashCode() {
return (int)(value ^ (value >>> 32));
}
/**
* Compares this mutable to another in ascending order.
*
* @param obj the mutable to compare to
* @return negative if this is less, zero if equal, positive if greater
*/
public int compareTo(Object obj) {
MutableLong other = (MutableLong) obj;
long anotherVal = other.value;
return (value < anotherVal ? -1 : (value == anotherVal ? 0 : 1));
}
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
public String toString() {
return String.valueOf(value);
}
}

View File

@ -1,85 +0,0 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
import java.io.Serializable;
/**
* A mutable <code>Number</code>.
*
* @since 2.1
* @version $Id: MutableNumber.java,v 1.3 2004/06/24 04:20:46 bayard Exp $
*/
public abstract class MutableNumber
extends Number
implements Comparable, Mutable, Serializable {
MutableNumber() {
super();
}
// ----------------------------------------------------------------
// Number overrides
// ----------------------------------------------------------------
public float floatValue() {
return (float)doubleValue();
}
// ----------------------------------------------------------------
// Object overrides
// ----------------------------------------------------------------
public String toString() {
return String.valueOf(doubleValue()).intern();
}
public int hashCode() {
return super.hashCode();
}
/**
* Compares <code>this</code> to another object.
*
* @param obj an object to compare to
* @return <code>true</code> if <code>this</code> is equal to
* <code>obj</code>.
* @see #compareTo(Object)
*/
public boolean equals(Object obj) {
return super.equals(obj);
}
// ----------------------------------------------------------------
// Comparable overrides
// ----------------------------------------------------------------
/**
* Compares to another object
*
* @param o an object to compare to
* @return -1 if <code>this < o</code>, 0 if <code>this.equals(o)</code>,
* 1 if <code>this > o<code>
* @throws ClassCastException if <code>o</code> is not a
* <code>Number</code>.
*/
public int compareTo(Object o) {
final double d = ((Number)o).doubleValue();
return (doubleValue() < d) ? -1 : (doubleValue() > d) ? 1 : 0;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,21 +15,33 @@
*/
package org.apache.commons.lang.mutable;
import java.io.Serializable;
/**
* A mutable <code>Short</code>
* A mutable <code>short</code>.
*
* @since 2.1
* @version $Id: MutableShort.java,v 1.3 2004/06/24 04:20:46 bayard Exp $
* @version $Id: MutableShort.java,v 1.4 2004/07/07 23:50:28 scolebourne Exp $
*/
public class MutableShort extends MutableNumber {
public class MutableShort extends Number
implements Comparable, Mutable, Serializable {
/**
* Internal value.
*/
/** Serialization lock. */
private static final long serialVersionUID = -2135791679L;
/** The mutable value. */
private short value;
/**
* Instantiates with the specified value
* Constructs a new MutableShort with the default value of zero.
*/
public MutableShort() {
super();
}
/**
* Constructs a new MutableShort with the specified value.
*
* @param value a value.
*/
public MutableShort(short value) {
@ -37,28 +49,112 @@ public MutableShort(short value) {
this.value = value;
}
public void setValue(short value) {
this.value = value;
}
public double doubleValue() {
return this.value;
}
public int intValue() {
return this.value;
}
public long longValue() {
return this.value;
/**
* Constructs a new MutableShort with the specified value.
*
* @param value a value.
* @throws NullPointerException if the object is null
*/
public MutableShort(Number value) {
super();
this.value = value.shortValue();
}
//-----------------------------------------------------------------------
/**
* Gets the value as a Short instance.
*
* @return the value as a Short
*/
public Object getValue() {
return new Short(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(short value) {
this.value = value;
}
/**
* Sets the value from any Number instance.
*
* @param value the value to set
* @throws NullPointerException if the object is null
* @throws ClassCastException if the type is invalid
*/
public void setValue(Object value) {
setValue(((Number)value).shortValue());
setValue(((Number) value).shortValue());
}
//-----------------------------------------------------------------------
public short shortValue() {
return value;
}
public int intValue() {
return (int) value;
}
public long longValue() {
return (long) value;
}
public float floatValue() {
return (float) value;
}
public double doubleValue() {
return (double) value;
}
//-----------------------------------------------------------------------
/**
* Checks if this object equals the specified object.
* <p>
* The object must be a MutableShort with the same value to be equal.
*
* @param obj the object to compare to
* @return true if equal
*/
public boolean equals(Object obj) {
if (obj instanceof MutableShort) {
return (value == ((MutableShort) obj).value);
}
return false;
}
/**
* Returns a suitable hashcode for this mutable.
*
* @return a suitable hashcode
*/
public int hashCode() {
return (int) value;
}
/**
* Compares this mutable to another in ascending order.
*
* @param obj the mutable to compare to
* @return negative if this is less, zero if equal, positive if greater
*/
public int compareTo(Object obj) {
MutableShort other = (MutableShort) obj;
short anotherVal = other.value;
return (value < anotherVal ? -1 : (value == anotherVal ? 0 : 1));
}
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
public String toString() {
return String.valueOf((int) value);
}
}

View File

@ -19,17 +19,19 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.commons.lang.builder.BuilderTestSuite;
import org.apache.commons.lang.enums.EnumTestSuite;
import org.apache.commons.lang.exception.ExceptionTestSuite;
import org.apache.commons.lang.math.MathTestSuite;
import org.apache.commons.lang.mutable.MutableTestSuite;
import org.apache.commons.lang.time.TimeTestSuite;
/**
* Test suite for [lang].
*
* @author Stephen Colebourne
* @version $Id: AllLangTestSuite.java,v 1.6 2004/06/01 20:55:28 scolebourne Exp $
* @version $Id: AllLangTestSuite.java,v 1.7 2004/07/07 23:50:28 scolebourne Exp $
*/
public class AllLangTestSuite extends TestCase {
@ -59,6 +61,7 @@ public static Test suite() {
suite.addTest(org.apache.commons.lang.enum.EnumTestSuite.suite());
suite.addTest(ExceptionTestSuite.suite());
suite.addTest(MathTestSuite.suite());
suite.addTest(MutableTestSuite.suite());
suite.addTest(TimeTestSuite.suite());
return suite;
}

View File

@ -16,59 +16,122 @@
package org.apache.commons.lang.mutable;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Id: MutableByteTest.java,v 1.1 2004/06/11 02:26:32 matth Exp $
* @version $Id: MutableByteTest.java,v 1.2 2004/07/07 23:50:28 scolebourne Exp $
* @see MutableByte
*/
public class MutableByteTest extends MutableNumberTest {
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(MutableByteTest.class);
}
public class MutableByteTest extends TestCase {
public MutableByteTest(String testName) {
super(testName);
}
public MutableNumber getMutableNumber(double value) {
return new MutableByte((byte)value);
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(MutableByteTest.class);
}
// ----------------------------------------------------------------
// Converters
// ----------------------------------------------------------------
public byte byteValue(double value) {
return (byte)value;
public void testConstructors() {
assertEquals((byte) 0, new MutableByte().byteValue());
assertEquals((byte) 1, new MutableByte((byte) 1).byteValue());
assertEquals((byte) 2, new MutableByte(new Byte((byte) 2)).byteValue());
assertEquals((byte) 3, new MutableByte(new MutableByte((byte) 3)).byteValue());
try {
new MutableByte(null);
fail();
} catch (NullPointerException ex) {}
}
public short shortValue(double value) {
return (byte)value;
public void testGetSet() {
final MutableByte mutNum = new MutableByte((byte) 0);
assertEquals((byte) 0, new MutableByte().byteValue());
assertEquals(new Byte((byte) 0), new MutableByte().getValue());
mutNum.setValue((byte) 1);
assertEquals((byte) 1, mutNum.byteValue());
assertEquals(new Byte((byte) 1), mutNum.getValue());
mutNum.setValue(new Byte((byte) 2));
assertEquals((byte) 2, mutNum.byteValue());
assertEquals(new Byte((byte) 2), mutNum.getValue());
mutNum.setValue(new MutableByte((byte) 3));
assertEquals((byte) 3, mutNum.byteValue());
assertEquals(new Byte((byte) 3), mutNum.getValue());
try {
mutNum.setValue(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.setValue("0");
fail();
} catch (ClassCastException ex) {}
}
public int intValue(double value) {
return (byte)value;
public void testEquals() {
final MutableByte mutNumA = new MutableByte((byte) 0);
final MutableByte mutNumB = new MutableByte((byte) 0);
final MutableByte mutNumC = new MutableByte((byte) 1);
assertEquals(true, mutNumA.equals(mutNumA));
assertEquals(true, mutNumA.equals(mutNumB));
assertEquals(true, mutNumB.equals(mutNumA));
assertEquals(true, mutNumB.equals(mutNumB));
assertEquals(false, mutNumA.equals(mutNumC));
assertEquals(false, mutNumB.equals(mutNumC));
assertEquals(true, mutNumC.equals(mutNumC));
assertEquals(false, mutNumA.equals(null));
assertEquals(false, mutNumA.equals(new Byte((byte) 0)));
assertEquals(false, mutNumA.equals("0"));
}
public long longValue(double value) {
return (byte)value;
public void testHashCode() {
final MutableByte mutNumA = new MutableByte((byte) 0);
final MutableByte mutNumB = new MutableByte((byte) 0);
final MutableByte mutNumC = new MutableByte((byte) 1);
assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
assertEquals(true, mutNumA.hashCode() == new Byte((byte) 0).hashCode());
}
public float floatValue(double value) {
return (byte)value;
public void testCompareTo() {
final MutableByte mutNum = new MutableByte((byte) 0);
assertEquals((byte) 0, mutNum.compareTo(new MutableByte((byte) 0)));
assertEquals((byte) +1, mutNum.compareTo(new MutableByte((byte) -1)));
assertEquals((byte) -1, mutNum.compareTo(new MutableByte((byte) 1)));
try {
mutNum.compareTo(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.compareTo(new Byte((byte) 0));
fail();
} catch (ClassCastException ex) {}
try {
mutNum.compareTo("0");
fail();
} catch (ClassCastException ex) {}
}
public double doubleValue(double value) {
return (byte)value;
public void testToString() {
assertEquals("0", new MutableByte((byte) 0).toString());
assertEquals("10", new MutableByte((byte) 10).toString());
assertEquals("-123", new MutableByte((byte) -123).toString());
}
} // MutableByteTest
}

View File

@ -1,73 +1,148 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Id: MutableDoubleTest.java,v 1.1 2004/06/11 02:26:32 matth Exp $
* @see MutableDouble
*/
public class MutableDoubleTest extends MutableNumberTest {
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(MutableDoubleTest.class);
}
public MutableDoubleTest(String testName) {
super(testName);
}
public MutableNumber getMutableNumber(double value) {
return new MutableDouble(value);
}
// Converters
// ----------------------------------------------------------------
public byte byteValue(double value) {
return (byte)value;
}
public short shortValue(double value) {
return (short)value;
}
public int intValue(double value) {
return (int)value;
}
public long longValue(double value) {
return (long)value;
}
public float floatValue(double value) {
return (float)value;
}
public double doubleValue(double value) {
return value;
}
}
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Id: MutableDoubleTest.java,v 1.2 2004/07/07 23:50:28 scolebourne Exp $
* @see MutableDouble
*/
public class MutableDoubleTest extends TestCase {
public MutableDoubleTest(String testName) {
super(testName);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(MutableDoubleTest.class);
}
// ----------------------------------------------------------------
public void testConstructors() {
assertEquals(0d, new MutableDouble().doubleValue(), 0.0001d);
assertEquals(1d, new MutableDouble(1d).doubleValue(), 0.0001d);
assertEquals(2d, new MutableDouble(new Double(2d)).doubleValue(), 0.0001d);
assertEquals(3d, new MutableDouble(new MutableDouble(3d)).doubleValue(), 0.0001d);
try {
new MutableDouble(null);
fail();
} catch (NullPointerException ex) {}
}
public void testGetSet() {
final MutableDouble mutNum = new MutableDouble(0d);
assertEquals(0d, new MutableDouble().doubleValue(), 0.0001d);
assertEquals(new Double(0), new MutableDouble().getValue());
mutNum.setValue(1);
assertEquals(1d, mutNum.doubleValue(), 0.0001d);
assertEquals(new Double(1d), mutNum.getValue());
mutNum.setValue(new Double(2d));
assertEquals(2d, mutNum.doubleValue(), 0.0001d);
assertEquals(new Double(2d), mutNum.getValue());
mutNum.setValue(new MutableDouble(3d));
assertEquals(3d, mutNum.doubleValue(), 0.0001d);
assertEquals(new Double(3d), mutNum.getValue());
try {
mutNum.setValue(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.setValue("0");
fail();
} catch (ClassCastException ex) {}
}
public void testNanInfinite() {
MutableDouble mutNum = new MutableDouble(Double.NaN);
assertEquals(true, mutNum.isNaN());
mutNum = new MutableDouble(Double.POSITIVE_INFINITY);
assertEquals(true, mutNum.isInfinite());
mutNum = new MutableDouble(Double.NEGATIVE_INFINITY);
assertEquals(true, mutNum.isInfinite());
}
public void testEquals() {
final MutableDouble mutNumA = new MutableDouble(0d);
final MutableDouble mutNumB = new MutableDouble(0d);
final MutableDouble mutNumC = new MutableDouble(1d);
assertEquals(true, mutNumA.equals(mutNumA));
assertEquals(true, mutNumA.equals(mutNumB));
assertEquals(true, mutNumB.equals(mutNumA));
assertEquals(true, mutNumB.equals(mutNumB));
assertEquals(false, mutNumA.equals(mutNumC));
assertEquals(false, mutNumB.equals(mutNumC));
assertEquals(true, mutNumC.equals(mutNumC));
assertEquals(false, mutNumA.equals(null));
assertEquals(false, mutNumA.equals(new Double(0d)));
assertEquals(false, mutNumA.equals("0"));
}
public void testHashCode() {
final MutableDouble mutNumA = new MutableDouble(0d);
final MutableDouble mutNumB = new MutableDouble(0d);
final MutableDouble mutNumC = new MutableDouble(1d);
assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
assertEquals(true, mutNumA.hashCode() == new Double(0d).hashCode());
}
public void testCompareTo() {
final MutableDouble mutNum = new MutableDouble(0d);
assertEquals(0, mutNum.compareTo(new MutableDouble(0d)));
assertEquals(+1, mutNum.compareTo(new MutableDouble(-1d)));
assertEquals(-1, mutNum.compareTo(new MutableDouble(1d)));
try {
mutNum.compareTo(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.compareTo(new Double(0d));
fail();
} catch (ClassCastException ex) {}
try {
mutNum.compareTo("0");
fail();
} catch (ClassCastException ex) {}
}
public void testToString() {
assertEquals("0.0", new MutableDouble(0d).toString());
assertEquals("10.0", new MutableDouble(10d).toString());
assertEquals("-123.0", new MutableDouble(-123d).toString());
}
}

View File

@ -1,75 +1,148 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Id: MutableFloatTest.java,v 1.1 2004/06/11 02:26:32 matth Exp $
* @see MutableFloat
*/
public class MutableFloatTest extends MutableNumberTest {
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(MutableFloatTest.class);
}
/**
* @param testName
*/
public MutableFloatTest(String testName) {
super(testName);
}
public MutableNumber getMutableNumber(double value) {
return new MutableFloat((float)value);
}
// Converters
// ----------------------------------------------------------------
public byte byteValue(double value) {
return (byte)(float)value;
}
public short shortValue(double value) {
return (short)(float)value;
}
public int intValue(double value) {
return (int)(float)value;
}
public long longValue(double value) {
return (long)(float)value;
}
public float floatValue(double value) {
return (float)value;
}
public double doubleValue(double value) {
return (float)value;
}
}
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Id: MutableFloatTest.java,v 1.2 2004/07/07 23:50:28 scolebourne Exp $
* @see MutableFloat
*/
public class MutableFloatTest extends TestCase {
public MutableFloatTest(String testName) {
super(testName);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(MutableFloatTest.class);
}
// ----------------------------------------------------------------
public void testConstructors() {
assertEquals(0f, new MutableFloat().floatValue(), 0.0001f);
assertEquals(1f, new MutableFloat(1f).floatValue(), 0.0001f);
assertEquals(2f, new MutableFloat(new Float(2f)).floatValue(), 0.0001f);
assertEquals(3f, new MutableFloat(new MutableFloat(3f)).floatValue(), 0.0001f);
try {
new MutableFloat(null);
fail();
} catch (NullPointerException ex) {}
}
public void testGetSet() {
final MutableFloat mutNum = new MutableFloat(0f);
assertEquals(0f, new MutableFloat().floatValue(), 0.0001f);
assertEquals(new Float(0), new MutableFloat().getValue());
mutNum.setValue(1);
assertEquals(1f, mutNum.floatValue(), 0.0001f);
assertEquals(new Float(1f), mutNum.getValue());
mutNum.setValue(new Float(2f));
assertEquals(2f, mutNum.floatValue(), 0.0001f);
assertEquals(new Float(2f), mutNum.getValue());
mutNum.setValue(new MutableFloat(3f));
assertEquals(3f, mutNum.floatValue(), 0.0001f);
assertEquals(new Float(3f), mutNum.getValue());
try {
mutNum.setValue(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.setValue("0");
fail();
} catch (ClassCastException ex) {}
}
public void testNanInfinite() {
MutableFloat mutNum = new MutableFloat(Float.NaN);
assertEquals(true, mutNum.isNaN());
mutNum = new MutableFloat(Float.POSITIVE_INFINITY);
assertEquals(true, mutNum.isInfinite());
mutNum = new MutableFloat(Float.NEGATIVE_INFINITY);
assertEquals(true, mutNum.isInfinite());
}
public void testEquals() {
final MutableFloat mutNumA = new MutableFloat(0f);
final MutableFloat mutNumB = new MutableFloat(0f);
final MutableFloat mutNumC = new MutableFloat(1f);
assertEquals(true, mutNumA.equals(mutNumA));
assertEquals(true, mutNumA.equals(mutNumB));
assertEquals(true, mutNumB.equals(mutNumA));
assertEquals(true, mutNumB.equals(mutNumB));
assertEquals(false, mutNumA.equals(mutNumC));
assertEquals(false, mutNumB.equals(mutNumC));
assertEquals(true, mutNumC.equals(mutNumC));
assertEquals(false, mutNumA.equals(null));
assertEquals(false, mutNumA.equals(new Float(0f)));
assertEquals(false, mutNumA.equals("0"));
}
public void testHashCode() {
final MutableFloat mutNumA = new MutableFloat(0f);
final MutableFloat mutNumB = new MutableFloat(0f);
final MutableFloat mutNumC = new MutableFloat(1f);
assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
assertEquals(true, mutNumA.hashCode() == new Float(0f).hashCode());
}
public void testCompareTo() {
final MutableFloat mutNum = new MutableFloat(0f);
assertEquals(0, mutNum.compareTo(new MutableFloat(0f)));
assertEquals(+1, mutNum.compareTo(new MutableFloat(-1f)));
assertEquals(-1, mutNum.compareTo(new MutableFloat(1f)));
try {
mutNum.compareTo(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.compareTo(new Float(0f));
fail();
} catch (ClassCastException ex) {}
try {
mutNum.compareTo("0");
fail();
} catch (ClassCastException ex) {}
}
public void testToString() {
assertEquals("0.0", new MutableFloat(0f).toString());
assertEquals("10.0", new MutableFloat(10f).toString());
assertEquals("-123.0", new MutableFloat(-123f).toString());
}
}

View File

@ -0,0 +1,137 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Id: MutableIntTest.java,v 1.1 2004/07/07 23:50:28 scolebourne Exp $
* @see MutableInt
*/
public class MutableIntTest extends TestCase {
public MutableIntTest(String testName) {
super(testName);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(MutableIntTest.class);
}
// ----------------------------------------------------------------
public void testConstructors() {
assertEquals(0, new MutableInt().intValue());
assertEquals(1, new MutableInt(1).intValue());
assertEquals(2, new MutableInt(new Integer(2)).intValue());
assertEquals(3, new MutableInt(new MutableLong(3)).intValue());
try {
new MutableInt(null);
fail();
} catch (NullPointerException ex) {}
}
public void testGetSet() {
final MutableInt mutNum = new MutableInt(0);
assertEquals(0, new MutableInt().intValue());
assertEquals(new Integer(0), new MutableInt().getValue());
mutNum.setValue(1);
assertEquals(1, mutNum.intValue());
assertEquals(new Integer(1), mutNum.getValue());
mutNum.setValue(new Integer(2));
assertEquals(2, mutNum.intValue());
assertEquals(new Integer(2), mutNum.getValue());
mutNum.setValue(new MutableLong(3));
assertEquals(3, mutNum.intValue());
assertEquals(new Integer(3), mutNum.getValue());
try {
mutNum.setValue(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.setValue("0");
fail();
} catch (ClassCastException ex) {}
}
public void testEquals() {
final MutableInt mutNumA = new MutableInt(0);
final MutableInt mutNumB = new MutableInt(0);
final MutableInt mutNumC = new MutableInt(1);
assertEquals(true, mutNumA.equals(mutNumA));
assertEquals(true, mutNumA.equals(mutNumB));
assertEquals(true, mutNumB.equals(mutNumA));
assertEquals(true, mutNumB.equals(mutNumB));
assertEquals(false, mutNumA.equals(mutNumC));
assertEquals(false, mutNumB.equals(mutNumC));
assertEquals(true, mutNumC.equals(mutNumC));
assertEquals(false, mutNumA.equals(null));
assertEquals(false, mutNumA.equals(new Integer(0)));
assertEquals(false, mutNumA.equals("0"));
}
public void testHashCode() {
final MutableInt mutNumA = new MutableInt(0);
final MutableInt mutNumB = new MutableInt(0);
final MutableInt mutNumC = new MutableInt(1);
assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
assertEquals(true, mutNumA.hashCode() == new Integer(0).hashCode());
}
public void testCompareTo() {
final MutableInt mutNum = new MutableInt(0);
assertEquals(0, mutNum.compareTo(new MutableInt(0)));
assertEquals(+1, mutNum.compareTo(new MutableInt(-1)));
assertEquals(-1, mutNum.compareTo(new MutableInt(1)));
try {
mutNum.compareTo(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.compareTo(new Integer(0));
fail();
} catch (ClassCastException ex) {}
try {
mutNum.compareTo("0");
fail();
} catch (ClassCastException ex) {}
}
public void testToString() {
assertEquals("0", new MutableInt(0).toString());
assertEquals("10", new MutableInt(10).toString());
assertEquals("-123", new MutableInt(-123).toString());
}
}

View File

@ -1,74 +0,0 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Id: MutableIntegerTest.java,v 1.1 2004/06/11 02:26:32 matth Exp $
* @see MutableInteger
*/
public class MutableIntegerTest extends MutableNumberTest {
public MutableIntegerTest(String testName) {
super(testName);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(MutableIntegerTest.class);
}
public MutableNumber getMutableNumber(double value) {
return new MutableInteger((int)value);
}
// ----------------------------------------------------------------
// Converters
// ----------------------------------------------------------------
public byte byteValue(double value) {
return (byte)(int)value;
}
public double doubleValue(double value) {
return (int)value;
}
public float floatValue(double value) {
return (int)value;
}
public int intValue(double value) {
return (int)value;
}
public long longValue(double value) {
return (int)value;
}
public short shortValue(double value) {
return (short)(int)value;
}
}

View File

@ -1,74 +1,137 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Id: MutableLongTest.java,v 1.1 2004/06/11 02:26:32 matth Exp $
* @see MutableLong
*/
public class MutableLongTest extends MutableNumberTest {
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(MutableLongTest.class);
}
public MutableLongTest(String testName) {
super(testName);
}
public MutableNumber getMutableNumber(double value) {
return new MutableLong((long)value);
}
// ----------------------------------------------------------------
// Converters
// ----------------------------------------------------------------
public byte byteValue(double value) {
return (byte)(long)value;
}
public short shortValue(double value) {
return (short)(long)value;
}
public int intValue(double value) {
return (int)(long)value;
}
public long longValue(double value) {
return (long)value;
}
public float floatValue(double value) {
return (long)value;
}
public double doubleValue(double value) {
return (long)value;
}
}
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Id: MutableLongTest.java,v 1.2 2004/07/07 23:50:28 scolebourne Exp $
* @see MutableLong
*/
public class MutableLongTest extends TestCase {
public MutableLongTest(String testName) {
super(testName);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(MutableLongTest.class);
}
// ----------------------------------------------------------------
public void testConstructors() {
assertEquals(0, new MutableLong().longValue());
assertEquals(1, new MutableLong(1).longValue());
assertEquals(2, new MutableLong(new Long(2)).longValue());
assertEquals(3, new MutableLong(new MutableLong(3)).longValue());
try {
new MutableLong(null);
fail();
} catch (NullPointerException ex) {}
}
public void testGetSet() {
final MutableLong mutNum = new MutableLong(0);
assertEquals(0, new MutableLong().longValue());
assertEquals(new Long(0), new MutableLong().getValue());
mutNum.setValue(1);
assertEquals(1, mutNum.longValue());
assertEquals(new Long(1), mutNum.getValue());
mutNum.setValue(new Long(2));
assertEquals(2, mutNum.longValue());
assertEquals(new Long(2), mutNum.getValue());
mutNum.setValue(new MutableLong(3));
assertEquals(3, mutNum.longValue());
assertEquals(new Long(3), mutNum.getValue());
try {
mutNum.setValue(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.setValue("0");
fail();
} catch (ClassCastException ex) {}
}
public void testEquals() {
final MutableLong mutNumA = new MutableLong(0);
final MutableLong mutNumB = new MutableLong(0);
final MutableLong mutNumC = new MutableLong(1);
assertEquals(true, mutNumA.equals(mutNumA));
assertEquals(true, mutNumA.equals(mutNumB));
assertEquals(true, mutNumB.equals(mutNumA));
assertEquals(true, mutNumB.equals(mutNumB));
assertEquals(false, mutNumA.equals(mutNumC));
assertEquals(false, mutNumB.equals(mutNumC));
assertEquals(true, mutNumC.equals(mutNumC));
assertEquals(false, mutNumA.equals(null));
assertEquals(false, mutNumA.equals(new Long(0)));
assertEquals(false, mutNumA.equals("0"));
}
public void testHashCode() {
final MutableLong mutNumA = new MutableLong(0);
final MutableLong mutNumB = new MutableLong(0);
final MutableLong mutNumC = new MutableLong(1);
assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
assertEquals(true, mutNumA.hashCode() == new Long(0).hashCode());
}
public void testCompareTo() {
final MutableLong mutNum = new MutableLong(0);
assertEquals(0, mutNum.compareTo(new MutableLong(0)));
assertEquals(+1, mutNum.compareTo(new MutableLong(-1)));
assertEquals(-1, mutNum.compareTo(new MutableLong(1)));
try {
mutNum.compareTo(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.compareTo(new Long(0));
fail();
} catch (ClassCastException ex) {}
try {
mutNum.compareTo("0");
fail();
} catch (ClassCastException ex) {}
}
public void testToString() {
assertEquals("0", new MutableLong(0).toString());
assertEquals("10", new MutableLong(10).toString());
assertEquals("-123", new MutableLong(-123).toString());
}
}

View File

@ -1,208 +0,0 @@
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.mutable;
import junit.framework.TestCase;
/**
* JUnit tests.
*
* @version $Id: MutableNumberTest.java,v 1.1 2004/06/11 02:26:32 matth Exp $
* @see MutableNumber
*/
public abstract class MutableNumberTest extends TestCase {
public MutableNumberTest(String testName) {
super(testName);
}
/**
* Gets an instance to test.
* @param value the value of the number.
* @return a <code>MutableNumber</code>
*/
public abstract MutableNumber getMutableNumber(double value);
// ----------------------------------------------------------------
// Converters
// ----------------------------------------------------------------
public abstract byte byteValue(double value);
public abstract short shortValue(double value);
public abstract int intValue(double value);
public abstract long longValue(double value);
public abstract float floatValue(double value);
public abstract double doubleValue(double value);
// ----------------------------------------------------------------
// Tests
// ----------------------------------------------------------------
public void testCompareTo() {
final double num = 0;
final MutableNumber mutNum = getMutableNumber(num);
assertEquals("Equality", 0, mutNum.compareTo(new Double(num)));
assertEquals(
"Less than",
-1,
mutNum.compareTo(new Double(Double.POSITIVE_INFINITY)));
assertEquals(
"Greater than",
1,
mutNum.compareTo(new Double(Double.NEGATIVE_INFINITY)));
}
public void testPrimitiveAccessors() {
testPrimitiveAccessors(0);
testPrimitiveAccessors(Double.MAX_VALUE);
testPrimitiveAccessors(-Double.MAX_VALUE);
testPrimitiveAccessors(Float.MAX_VALUE);
testPrimitiveAccessors(-Float.MAX_VALUE);
testPrimitiveAccessors(Long.MAX_VALUE);
testPrimitiveAccessors(Long.MIN_VALUE);
testPrimitiveAccessors(Integer.MAX_VALUE);
testPrimitiveAccessors(Integer.MIN_VALUE);
testPrimitiveAccessors(Short.MAX_VALUE);
testPrimitiveAccessors(Short.MIN_VALUE);
testPrimitiveAccessors(Byte.MAX_VALUE);
testPrimitiveAccessors(Byte.MIN_VALUE);
}
public void XtestObjectAccessors() {
testObjectAccessors(0);
testObjectAccessors(Double.MAX_VALUE);
testObjectAccessors(-Double.MAX_VALUE);
testObjectAccessors(Float.MAX_VALUE);
testObjectAccessors(-Float.MAX_VALUE);
testObjectAccessors(Long.MAX_VALUE);
testObjectAccessors(Long.MIN_VALUE);
testObjectAccessors(Integer.MAX_VALUE);
testObjectAccessors(Integer.MIN_VALUE);
testObjectAccessors(Short.MAX_VALUE);
testObjectAccessors(Short.MIN_VALUE);
testObjectAccessors(Byte.MAX_VALUE);
testObjectAccessors(Byte.MIN_VALUE);
}
public void testSetValue() {
setValueAndTestAccessors(Double.MAX_VALUE);
setValueAndTestAccessors(-Double.MAX_VALUE);
setValueAndTestAccessors(Float.MAX_VALUE);
setValueAndTestAccessors(-Float.MAX_VALUE);
setValueAndTestAccessors(Long.MAX_VALUE);
setValueAndTestAccessors(Long.MIN_VALUE);
setValueAndTestAccessors(Integer.MAX_VALUE);
setValueAndTestAccessors(Integer.MIN_VALUE);
setValueAndTestAccessors(Short.MAX_VALUE);
setValueAndTestAccessors(Short.MIN_VALUE);
setValueAndTestAccessors(Byte.MAX_VALUE);
setValueAndTestAccessors(Byte.MIN_VALUE);
}
// ----------------------------------------------------------------
// Private methods
// ----------------------------------------------------------------
private void setValueAndTestAccessors(double num) {
final MutableNumber mutNum = getMutableNumber(0);
mutNum.setValue(new Double(num));
testPrimitiveAccessors(mutNum, num);
//testObjectAccessors(mutNum, num);
}
private void testPrimitiveAccessors(double num) {
testPrimitiveAccessors(getMutableNumber(num), num);
}
private void testPrimitiveAccessors(MutableNumber mutNum, double num) {
assertEquals("byte comparison", byteValue(num), mutNum.byteValue());
assertEquals("short comparison", shortValue(num), mutNum.shortValue());
assertEquals("int comparison", intValue(num), mutNum.intValue());
assertEquals("long comparison", longValue(num), mutNum.longValue());
assertEquals(
"float comparison",
floatValue(num),
mutNum.floatValue(),
0);
assertEquals(
"double comparison",
doubleValue(num),
mutNum.doubleValue(),
0);
}
private void testObjectAccessors(double num) {
testObjectAccessors(getMutableNumber(num), num);
}
private void testObjectAccessors(MutableNumber mutNum, double num) {
assertEquals(
"byte comparison",
new Byte(byteValue(num)),
mutNum.getValue());
assertEquals(
"short comparison",
new Short(shortValue(num)),
mutNum.getValue());
assertEquals(
"int comparison",
new Integer(intValue(num)),
mutNum.getValue());
assertEquals(
"long comparison",
new Long(longValue(num)),
mutNum.getValue());
assertEquals(
"float comparison",
new Float(floatValue(num)),
mutNum.getValue());
assertEquals(
"double comparison",
new Double(doubleValue(num)),
mutNum.getValue());
}
}

View File

@ -16,16 +16,21 @@
package org.apache.commons.lang.mutable;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests.
*
* @version $Id: MutableShortTest.java,v 1.1 2004/06/11 02:26:32 matth Exp $
* @version $Id: MutableShortTest.java,v 1.2 2004/07/07 23:50:28 scolebourne Exp $
* @see MutableShort
*/
public class MutableShortTest extends MutableNumberTest {
public class MutableShortTest extends TestCase {
public MutableShortTest(String testName) {
super(testName);
}
public static void main(String[] args) {
TestRunner.run(suite());
@ -35,41 +40,98 @@ public static Test suite() {
return new TestSuite(MutableShortTest.class);
}
public MutableShortTest(String testName) {
super(testName);
}
// ----------------------------------------------------------------
// Converters
// ----------------------------------------------------------------
public MutableNumber getMutableNumber(double value) {
return new MutableShort((short)value);
public void testConstructors() {
assertEquals((short) 0, new MutableShort().shortValue());
assertEquals((short) 1, new MutableShort((short) 1).shortValue());
assertEquals((short) 2, new MutableShort(new Short((short) 2)).shortValue());
assertEquals((short) 3, new MutableShort(new MutableShort((short) 3)).shortValue());
try {
new MutableShort(null);
fail();
} catch (NullPointerException ex) {}
}
public byte byteValue(double value) {
return (byte)(short)value;
public void testGetSet() {
final MutableShort mutNum = new MutableShort((short) 0);
assertEquals((short) 0, new MutableShort().shortValue());
assertEquals(new Short((short) 0), new MutableShort().getValue());
mutNum.setValue((short) 1);
assertEquals((short) 1, mutNum.shortValue());
assertEquals(new Short((short) 1), mutNum.getValue());
mutNum.setValue(new Short((short) 2));
assertEquals((short) 2, mutNum.shortValue());
assertEquals(new Short((short) 2), mutNum.getValue());
mutNum.setValue(new MutableShort((short) 3));
assertEquals((short) 3, mutNum.shortValue());
assertEquals(new Short((short) 3), mutNum.getValue());
try {
mutNum.setValue(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.setValue("0");
fail();
} catch (ClassCastException ex) {}
}
public short shortValue(double value) {
return (short)value;
public void testEquals() {
final MutableShort mutNumA = new MutableShort((short) 0);
final MutableShort mutNumB = new MutableShort((short) 0);
final MutableShort mutNumC = new MutableShort((short) 1);
assertEquals(true, mutNumA.equals(mutNumA));
assertEquals(true, mutNumA.equals(mutNumB));
assertEquals(true, mutNumB.equals(mutNumA));
assertEquals(true, mutNumB.equals(mutNumB));
assertEquals(false, mutNumA.equals(mutNumC));
assertEquals(false, mutNumB.equals(mutNumC));
assertEquals(true, mutNumC.equals(mutNumC));
assertEquals(false, mutNumA.equals(null));
assertEquals(false, mutNumA.equals(new Short((short) 0)));
assertEquals(false, mutNumA.equals("0"));
}
public int intValue(double value) {
return (short)value;
public void testHashCode() {
final MutableShort mutNumA = new MutableShort((short) 0);
final MutableShort mutNumB = new MutableShort((short) 0);
final MutableShort mutNumC = new MutableShort((short) 1);
assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
assertEquals(true, mutNumA.hashCode() == new Short((short) 0).hashCode());
}
public long longValue(double value) {
return (short)value;
public void testCompareTo() {
final MutableShort mutNum = new MutableShort((short) 0);
assertEquals((short) 0, mutNum.compareTo(new MutableShort((short) 0)));
assertEquals((short) +1, mutNum.compareTo(new MutableShort((short) -1)));
assertEquals((short) -1, mutNum.compareTo(new MutableShort((short) 1)));
try {
mutNum.compareTo(null);
fail();
} catch (NullPointerException ex) {}
try {
mutNum.compareTo(new Short((short) 0));
fail();
} catch (ClassCastException ex) {}
try {
mutNum.compareTo("0");
fail();
} catch (ClassCastException ex) {}
}
public float floatValue(double value) {
return (short)value;
}
public double doubleValue(double value) {
return (short)value;
public void testToString() {
assertEquals("0", new MutableShort((short) 0).toString());
assertEquals("10", new MutableShort((short) 10).toString());
assertEquals("-123", new MutableShort((short) -123).toString());
}
}

View File

@ -22,9 +22,9 @@
/**
* JUnit tests.
*
* @version $Id: MutableTestSuite.java,v 1.2 2004/06/27 06:28:32 bayard Exp $
* @version $Id: MutableTestSuite.java,v 1.3 2004/07/07 23:50:28 scolebourne Exp $
*/
public final class MutableTestSuite {
public class MutableTestSuite {
public static void main(String[] args) {
TestRunner.run(suite());
@ -35,7 +35,7 @@ public static Test suite() {
suite.addTest(MutableByteTest.suite());
suite.addTest(MutableShortTest.suite());
suite.addTest(MutableIntegerTest.suite());
suite.addTest(MutableIntTest.suite());
suite.addTest(MutableLongTest.suite());
suite.addTest(MutableFloatTest.suite());
suite.addTest(MutableDoubleTest.suite());