From c0f14e226ec2a08fde8f0eef1e2fae30c056ba04 Mon Sep 17 00:00:00 2001 From: Matthew Hawthorne Date: Fri, 11 Jun 2004 02:26:32 +0000 Subject: [PATCH] Adding initial crack at mutables. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137849 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang/mutable/Mutable.java | 41 ++++ .../commons/lang/mutable/MutableByte.java | 70 ++++++ .../commons/lang/mutable/MutableDouble.java | 64 ++++++ .../commons/lang/mutable/MutableFloat.java | 65 ++++++ .../commons/lang/mutable/MutableInteger.java | 65 ++++++ .../commons/lang/mutable/MutableLong.java | 64 ++++++ .../commons/lang/mutable/MutableNumber.java | 85 +++++++ .../commons/lang/mutable/MutableShort.java | 64 ++++++ .../commons/lang/mutable/MutableByteTest.java | 74 +++++++ .../lang/mutable/MutableDoubleTest.java | 73 ++++++ .../lang/mutable/MutableFloatTest.java | 75 +++++++ .../lang/mutable/MutableIntegerTest.java | 74 +++++++ .../commons/lang/mutable/MutableLongTest.java | 74 +++++++ .../lang/mutable/MutableNumberTest.java | 208 ++++++++++++++++++ .../lang/mutable/MutableShortTest.java | 75 +++++++ .../lang/mutable/MutableTestSuite.java | 48 ++++ 16 files changed, 1219 insertions(+) create mode 100755 src/java/org/apache/commons/lang/mutable/Mutable.java create mode 100755 src/java/org/apache/commons/lang/mutable/MutableByte.java create mode 100755 src/java/org/apache/commons/lang/mutable/MutableDouble.java create mode 100755 src/java/org/apache/commons/lang/mutable/MutableFloat.java create mode 100755 src/java/org/apache/commons/lang/mutable/MutableInteger.java create mode 100755 src/java/org/apache/commons/lang/mutable/MutableLong.java create mode 100755 src/java/org/apache/commons/lang/mutable/MutableNumber.java create mode 100755 src/java/org/apache/commons/lang/mutable/MutableShort.java create mode 100755 src/test/org/apache/commons/lang/mutable/MutableByteTest.java create mode 100755 src/test/org/apache/commons/lang/mutable/MutableDoubleTest.java create mode 100755 src/test/org/apache/commons/lang/mutable/MutableFloatTest.java create mode 100755 src/test/org/apache/commons/lang/mutable/MutableIntegerTest.java create mode 100755 src/test/org/apache/commons/lang/mutable/MutableLongTest.java create mode 100755 src/test/org/apache/commons/lang/mutable/MutableNumberTest.java create mode 100755 src/test/org/apache/commons/lang/mutable/MutableShortTest.java create mode 100755 src/test/org/apache/commons/lang/mutable/MutableTestSuite.java diff --git a/src/java/org/apache/commons/lang/mutable/Mutable.java b/src/java/org/apache/commons/lang/mutable/Mutable.java new file mode 100755 index 000000000..8e0a2280d --- /dev/null +++ b/src/java/org/apache/commons/lang/mutable/Mutable.java @@ -0,0 +1,41 @@ +/* + * 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 object. + * + * @author Matthew Hawthorne + * @since 2.1 + * @version $Id: Mutable.java,v 1.1 2004/06/11 02:26:32 matth Exp $ + */ +public interface Mutable { + + /** + * Sets the value of this object. + * + * @param value the value of this object. + */ + public void setValue(Object value); + + /** + * Gets the value of this object. + * + * @return a value. + */ + public Object getValue(); + +} diff --git a/src/java/org/apache/commons/lang/mutable/MutableByte.java b/src/java/org/apache/commons/lang/mutable/MutableByte.java new file mode 100755 index 000000000..a807b509f --- /dev/null +++ b/src/java/org/apache/commons/lang/mutable/MutableByte.java @@ -0,0 +1,70 @@ +/* + * 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 Byte. + * + * @since 2.1 + * @version $Id: MutableByte.java,v 1.1 2004/06/11 02:26:32 matth Exp $ + */ +public class MutableByte extends MutableNumber { + + /** + * Internal value. + */ + private byte value; + + /** + * Instantiates with the specified value + * @param value a value. + */ + public MutableByte(byte value) { + super(); + 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 Byte + */ + public Object getValue() { + return new Byte(this.value); + } + + /** + * @param value a Byte + */ + public void setValue(Object value) { + setValue(((Number)value).byteValue()); + } + +} diff --git a/src/java/org/apache/commons/lang/mutable/MutableDouble.java b/src/java/org/apache/commons/lang/mutable/MutableDouble.java new file mode 100755 index 000000000..b25861256 --- /dev/null +++ b/src/java/org/apache/commons/lang/mutable/MutableDouble.java @@ -0,0 +1,64 @@ +/* + * 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 Double + * + * @since 2.1 + * @version $Id: MutableDouble.java,v 1.1 2004/06/11 02:26:32 matth Exp $ + */ +public class MutableDouble extends MutableNumber { + + /** + * Internal value. + */ + private double value; + + /** + * Instantiates with the specified value + * @param value a value. + */ + public MutableDouble(double value) { + super(); + 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; + } + + public Object getValue() { + return new Double(this.value); + } + + public void setValue(Object value) { + setValue(((Number)value).doubleValue()); + } + +} diff --git a/src/java/org/apache/commons/lang/mutable/MutableFloat.java b/src/java/org/apache/commons/lang/mutable/MutableFloat.java new file mode 100755 index 000000000..6da74422b --- /dev/null +++ b/src/java/org/apache/commons/lang/mutable/MutableFloat.java @@ -0,0 +1,65 @@ +/* + * 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 Float + * + * @since 2.1 + * @version $Id: MutableFloat.java,v 1.1 2004/06/11 02:26:32 matth Exp $ + */ +public class MutableFloat extends MutableNumber { + + /** + * Internal value. + */ + private float value; + + /** + * Instantiates with the specified value + * + * @param value a value. + */ + public MutableFloat(float value) { + super(); + 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; + } + + public Object getValue() { + return new Float(this.value); + } + + public void setValue(Object value) { + setValue(((Number)value).floatValue()); + } + +} diff --git a/src/java/org/apache/commons/lang/mutable/MutableInteger.java b/src/java/org/apache/commons/lang/mutable/MutableInteger.java new file mode 100755 index 000000000..baac3078e --- /dev/null +++ b/src/java/org/apache/commons/lang/mutable/MutableInteger.java @@ -0,0 +1,65 @@ +/* + * 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 Integer. + * + * @since 2.1 + * @version $Id: MutableInteger.java,v 1.1 2004/06/11 02:26:32 matth 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 Float(this.value); + } + + public void setValue(Object value) { + setValue(((Number)value).intValue()); + } + +} diff --git a/src/java/org/apache/commons/lang/mutable/MutableLong.java b/src/java/org/apache/commons/lang/mutable/MutableLong.java new file mode 100755 index 000000000..9cc737468 --- /dev/null +++ b/src/java/org/apache/commons/lang/mutable/MutableLong.java @@ -0,0 +1,64 @@ +/* + * 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 Long + * + * @since 2.1 + * @version $Id: MutableLong.java,v 1.1 2004/06/11 02:26:32 matth Exp $ + */ +public class MutableLong extends MutableNumber { + + /** + * Internal value. + */ + private long value; + + /** + * Instantiates 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; + } + + public Object getValue() { + return new Long(this.value); + } + + public void setValue(Object value) { + setValue(((Number)value).longValue()); + } + +} diff --git a/src/java/org/apache/commons/lang/mutable/MutableNumber.java b/src/java/org/apache/commons/lang/mutable/MutableNumber.java new file mode 100755 index 000000000..9f72314d1 --- /dev/null +++ b/src/java/org/apache/commons/lang/mutable/MutableNumber.java @@ -0,0 +1,85 @@ +/* + * 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 Number. + * + * @since 2.1 + * @version $Id: MutableNumber.java,v 1.1 2004/06/11 02:26:32 matth 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 this to another object. + * + * @param obj an object to compare to + * @return true if this is equal to + * obj. + * @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 this < o, 0 if this.equals(o), + * 1 if this > o + * @throws ClassCastException if o is not a + * Number. + */ + public int compareTo(Object o) { + final double d = ((Number)o).doubleValue(); + return (doubleValue() < d) ? -1 : (doubleValue() > d) ? 1 : 0; + } + +} diff --git a/src/java/org/apache/commons/lang/mutable/MutableShort.java b/src/java/org/apache/commons/lang/mutable/MutableShort.java new file mode 100755 index 000000000..940d63d49 --- /dev/null +++ b/src/java/org/apache/commons/lang/mutable/MutableShort.java @@ -0,0 +1,64 @@ +/* + * 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 Short + * + * @since 2.1 + * @version $Id: MutableShort.java,v 1.1 2004/06/11 02:26:32 matth Exp $ + */ +public class MutableShort extends MutableNumber { + + /** + * Internal value. + */ + private short value; + + /** + * Instantiates with the specified value + * @param value a value. + */ + public MutableShort(short value) { + super(); + 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; + } + + public Object getValue() { + return new Short(this.value); + } + + public void setValue(Object value) { + setValue(((Number)value).shortValue()); + } + +} diff --git a/src/test/org/apache/commons/lang/mutable/MutableByteTest.java b/src/test/org/apache/commons/lang/mutable/MutableByteTest.java new file mode 100755 index 000000000..ff35b7297 --- /dev/null +++ b/src/test/org/apache/commons/lang/mutable/MutableByteTest.java @@ -0,0 +1,74 @@ +/* + * 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: MutableByteTest.java,v 1.1 2004/06/11 02:26:32 matth 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 MutableByteTest(String testName) { + super(testName); + } + + public MutableNumber getMutableNumber(double value) { + return new MutableByte((byte)value); + } + + // ---------------------------------------------------------------- + // Converters + // ---------------------------------------------------------------- + + public byte byteValue(double value) { + return (byte)value; + } + + public short shortValue(double value) { + return (byte)value; + } + + public int intValue(double value) { + return (byte)value; + } + + public long longValue(double value) { + return (byte)value; + } + + public float floatValue(double value) { + return (byte)value; + } + + public double doubleValue(double value) { + return (byte)value; + } + +} // MutableByteTest diff --git a/src/test/org/apache/commons/lang/mutable/MutableDoubleTest.java b/src/test/org/apache/commons/lang/mutable/MutableDoubleTest.java new file mode 100755 index 000000000..fa6dfeec7 --- /dev/null +++ b/src/test/org/apache/commons/lang/mutable/MutableDoubleTest.java @@ -0,0 +1,73 @@ +/* + * 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; + } + +} diff --git a/src/test/org/apache/commons/lang/mutable/MutableFloatTest.java b/src/test/org/apache/commons/lang/mutable/MutableFloatTest.java new file mode 100755 index 000000000..69bbe1f02 --- /dev/null +++ b/src/test/org/apache/commons/lang/mutable/MutableFloatTest.java @@ -0,0 +1,75 @@ +/* + * 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; + } + +} diff --git a/src/test/org/apache/commons/lang/mutable/MutableIntegerTest.java b/src/test/org/apache/commons/lang/mutable/MutableIntegerTest.java new file mode 100755 index 000000000..838d7a1e7 --- /dev/null +++ b/src/test/org/apache/commons/lang/mutable/MutableIntegerTest.java @@ -0,0 +1,74 @@ +/* + * 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; + } + +} diff --git a/src/test/org/apache/commons/lang/mutable/MutableLongTest.java b/src/test/org/apache/commons/lang/mutable/MutableLongTest.java new file mode 100755 index 000000000..2014a9fe6 --- /dev/null +++ b/src/test/org/apache/commons/lang/mutable/MutableLongTest.java @@ -0,0 +1,74 @@ +/* + * 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; + } + +} diff --git a/src/test/org/apache/commons/lang/mutable/MutableNumberTest.java b/src/test/org/apache/commons/lang/mutable/MutableNumberTest.java new file mode 100755 index 000000000..93cd8a1c4 --- /dev/null +++ b/src/test/org/apache/commons/lang/mutable/MutableNumberTest.java @@ -0,0 +1,208 @@ +/* + * 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 MutableNumber + */ + 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()); + } + +} diff --git a/src/test/org/apache/commons/lang/mutable/MutableShortTest.java b/src/test/org/apache/commons/lang/mutable/MutableShortTest.java new file mode 100755 index 000000000..c461c300c --- /dev/null +++ b/src/test/org/apache/commons/lang/mutable/MutableShortTest.java @@ -0,0 +1,75 @@ +/* + * 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: MutableShortTest.java,v 1.1 2004/06/11 02:26:32 matth Exp $ + * @see MutableShort + */ +public class MutableShortTest extends MutableNumberTest { + + public static void main(String[] args) { + TestRunner.run(suite()); + } + + 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 byte byteValue(double value) { + return (byte)(short)value; + } + + public short shortValue(double value) { + return (short)value; + } + + public int intValue(double value) { + return (short)value; + } + + public long longValue(double value) { + return (short)value; + } + + public float floatValue(double value) { + return (short)value; + } + + public double doubleValue(double value) { + return (short)value; + } + +} + diff --git a/src/test/org/apache/commons/lang/mutable/MutableTestSuite.java b/src/test/org/apache/commons/lang/mutable/MutableTestSuite.java new file mode 100755 index 000000000..e46b58044 --- /dev/null +++ b/src/test/org/apache/commons/lang/mutable/MutableTestSuite.java @@ -0,0 +1,48 @@ +/* + * 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: MutableTestSuite.java,v 1.1 2004/06/11 02:26:32 matth Exp $ + */ +public final class MutableTestSuite { + + public static void main(String[] args) { + TestRunner.run(suite()); + } + + public static Test suite() { + final TestSuite suite = new TestSuite(); + + suite.addTest(MutableByteTest.suite()); + suite.addTest(MutableShortTest.suite()); + suite.addTest(MutableIntegerTest.suite()); + suite.addTest(MutableLongTest.suite()); + suite.addTest(MutableFloatTest.suite()); + suite.addTest(MutableDoubleTest.suite()); + + return suite; + } + + private MutableTestSuite() {} + +} \ No newline at end of file