Add constants for standard numeric values

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137182 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2002-12-15 16:53:28 +00:00
parent 925d554e92
commit 24bc34a7af
2 changed files with 79 additions and 3 deletions

View File

@ -63,12 +63,49 @@
* @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:steve.downey@netfolio.com">Steve Downey</a>
* @version $Id: NumberUtils.java,v 1.4 2002/11/16 10:41:03 scolebourne Exp $
* @version $Id: NumberUtils.java,v 1.5 2002/12/15 16:53:28 scolebourne Exp $
*/
public final class NumberUtils {
/** Reusable Long constant for zero. */
public static final Long LONG_ZERO = new Long(0L);
/** Reusable Long constant for one. */
public static final Long LONG_ONE = new Long(1L);
/** Reusable Long constant for minus one. */
public static final Long LONG_MINUS_ONE = new Long(-1L);
/** Reusable Integer constant for zero. */
public static final Integer INTEGER_ZERO = new Integer(0);
/** Reusable Integer constant for one. */
public static final Integer INTEGER_ONE = new Integer(1);
/** Reusable Integer constant for minus one. */
public static final Integer INTEGER_MINUS_ONE = new Integer(-1);
/** Reusable Short constant for zero. */
public static final Short SHORT_ZERO = new Short((short) 0);
/** Reusable Short constant for one. */
public static final Short SHORT_ONE = new Short((short) 1);
/** Reusable Short constant for minus one. */
public static final Short SHORT_MINUS_ONE = new Short((short) -1);
/** Reusable Byte constant for zero. */
public static final Byte BYTE_ZERO = new Byte((byte) 0);
/** Reusable Byte constant for one. */
public static final Byte BYTE_ONE = new Byte((byte) 1);
/** Reusable Byte constant for minus one. */
public static final Byte BYTE_MINUS_ONE = new Byte((byte) -1);
/** Reusable Double constant for zero. */
public static final Double DOUBLE_ZERO = new Double(0.0d);
/** Reusable Double constant for one. */
public static final Double DOUBLE_ONE = new Double(1.0d);
/** Reusable Double constant for minus one. */
public static final Double DOUBLE_MINUS_ONE = new Double(-1.0d);
/** Reusable Float constant for zero. */
public static final Float FLOAT_ZERO = new Float(0.0f);
/** Reusable Float constant for one. */
public static final Float FLOAT_ONE = new Float(1.0f);
/** Reusable Float constant for minus one. */
public static final Float FLOAT_MINUS_ONE = new Float(-1.0f);
/**
* <p>NumberUtils instances should NOT be constructed in standard programming.
* <p><code>NumberUtils</code> instances should NOT be constructed in standard programming.
* Instead, the class should be used as <code>NumberUtils.stringToInt("6");</code>.</p>
*
* <p>This constructor is public to permit tools that require a JavaBean instance

View File

@ -66,7 +66,7 @@
*
* @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id: NumberUtilsTest.java,v 1.3 2002/11/22 23:30:32 bayard Exp $
* @version $Id: NumberUtilsTest.java,v 1.4 2002/12/15 16:53:28 scolebourne Exp $
*/
public class NumberUtilsTest extends TestCase {
@ -511,4 +511,43 @@ private boolean checkCreateNumber(String val) {
}
}
public void testConstants() {
assertTrue(NumberUtils.LONG_ZERO instanceof Long);
assertTrue(NumberUtils.LONG_ONE instanceof Long);
assertTrue(NumberUtils.LONG_MINUS_ONE instanceof Long);
assertTrue(NumberUtils.INTEGER_ZERO instanceof Integer);
assertTrue(NumberUtils.INTEGER_ONE instanceof Integer);
assertTrue(NumberUtils.INTEGER_MINUS_ONE instanceof Integer);
assertTrue(NumberUtils.SHORT_ZERO instanceof Short);
assertTrue(NumberUtils.SHORT_ONE instanceof Short);
assertTrue(NumberUtils.SHORT_MINUS_ONE instanceof Short);
assertTrue(NumberUtils.BYTE_ZERO instanceof Byte);
assertTrue(NumberUtils.BYTE_ONE instanceof Byte);
assertTrue(NumberUtils.BYTE_MINUS_ONE instanceof Byte);
assertTrue(NumberUtils.DOUBLE_ZERO instanceof Double);
assertTrue(NumberUtils.DOUBLE_ONE instanceof Double);
assertTrue(NumberUtils.DOUBLE_MINUS_ONE instanceof Double);
assertTrue(NumberUtils.FLOAT_ZERO instanceof Float);
assertTrue(NumberUtils.FLOAT_ONE instanceof Float);
assertTrue(NumberUtils.FLOAT_MINUS_ONE instanceof Float);
assertTrue(NumberUtils.LONG_ZERO.longValue() == 0);
assertTrue(NumberUtils.LONG_ONE.longValue() == 1);
assertTrue(NumberUtils.LONG_MINUS_ONE.longValue() == -1);
assertTrue(NumberUtils.INTEGER_ZERO.intValue() == 0);
assertTrue(NumberUtils.INTEGER_ONE.intValue() == 1);
assertTrue(NumberUtils.INTEGER_MINUS_ONE.intValue() == -1);
assertTrue(NumberUtils.SHORT_ZERO.shortValue() == 0);
assertTrue(NumberUtils.SHORT_ONE.shortValue() == 1);
assertTrue(NumberUtils.SHORT_MINUS_ONE.shortValue() == -1);
assertTrue(NumberUtils.BYTE_ZERO.byteValue() == 0);
assertTrue(NumberUtils.BYTE_ONE.byteValue() == 1);
assertTrue(NumberUtils.BYTE_MINUS_ONE.byteValue() == -1);
assertTrue(NumberUtils.DOUBLE_ZERO.doubleValue() == 0.0d);
assertTrue(NumberUtils.DOUBLE_ONE.doubleValue() == 1.0d);
assertTrue(NumberUtils.DOUBLE_MINUS_ONE.doubleValue() == -1.0d);
assertTrue(NumberUtils.FLOAT_ZERO.floatValue() == 0.0f);
assertTrue(NumberUtils.FLOAT_ONE.floatValue() == 1.0f);
assertTrue(NumberUtils.FLOAT_MINUS_ONE.floatValue() == -1.0f);
}
}