From bd65b2153e491810b3883211ccf89863db1d1ad6 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 15 Sep 2002 10:26:42 +0000 Subject: [PATCH] Added compare methods for double and float Added min/max methods for long Added public constructor with comment git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137023 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/NumberUtils.java | 188 +++++++++++++++++- 1 file changed, 184 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/commons/lang/NumberUtils.java b/src/java/org/apache/commons/lang/NumberUtils.java index 122d155cb..4bb7a6e8a 100644 --- a/src/java/org/apache/commons/lang/NumberUtils.java +++ b/src/java/org/apache/commons/lang/NumberUtils.java @@ -57,15 +57,27 @@ import java.math.BigInteger; import java.math.BigDecimal; /** - * Provides extra functionality for java Number classes. + * Provides extra functionality for Java Number classes. * * @author Henri Yandell * @author Rand McNeely * @author Stephen Colebourne - * @version $Id: NumberUtils.java,v 1.1 2002/07/19 03:35:54 bayard Exp $ + * @author Steve Downey + * @version $Id: NumberUtils.java,v 1.2 2002/09/15 10:26:42 scolebourne Exp $ */ public final class NumberUtils { + /** + * NumberUtils instances should NOT be constructed in standard programming. + * Instead, the class should be used as NumberUtils.stringToInt("6");. + * This constructor is public to permit tools that require a JavaBean instance + * to operate. + */ + public NumberUtils() { + } + + //-------------------------------------------------------------------- + /** * Convert a String to an int, returning zero if the conversion fails * @@ -92,6 +104,8 @@ public static int stringToInt(String str, int defaultValue) { } } + //-------------------------------------------------------------------- + // must handle Long, Float, Integer, Float, Short, // BigDecimal, BigInteger and Byte // useful methods: @@ -312,6 +326,8 @@ private static boolean isAllZeros(String s) { return s.length() > 0; } + //-------------------------------------------------------------------- + /** * Convert a String to a Float * @@ -382,8 +398,33 @@ public static BigDecimal createBigDecimal(String val) { return bd; } + //-------------------------------------------------------------------- + /** - * Get the minimum of three values. + * Gets the minimum of three long values. + * + * @param a value 1 + * @param b value 2 + * @param c value 3 + * @return the largest of the values + */ + public static long minimum(long a, long b, long c) { + if (b < a) { + a = b; + } + if (c < a) { + a = c; + } + return a; + } + + /** + * Gets the minimum of three int values. + * + * @param a value 1 + * @param b value 2 + * @param c value 3 + * @return the largest of the values */ public static int minimum(int a, int b, int c) { if (b < a) { @@ -396,7 +437,30 @@ public static int minimum(int a, int b, int c) { } /** - * Get the maximum of three values. + * Gets the maximum of three long values. + * + * @param a value 1 + * @param b value 2 + * @param c value 3 + * @return the largest of the values + */ + public static long maximum(long a, long b, long c) { + if (b > a) { + a = b; + } + if (c > a) { + a = c; + } + return a; + } + + /** + * Gets the maximum of three int values. + * + * @param a value 1 + * @param b value 2 + * @param c value 3 + * @return the largest of the values */ public static int maximum(int a, int b, int c) { if (b > a) { @@ -408,6 +472,122 @@ public static int maximum(int a, int b, int c) { return a; } + //-------------------------------------------------------------------- + + /** + * Compares two doubles for order. + *

+ * This method is more comprhensive than the standard Java greater than, + * less than and equals operators. + * It returns -1 if the first value is less than the second. + * It returns +1 if the first value is greater than the second. + * It returns 0 if the values are equal. + *

+ * The ordering is as follows, largest to smallest: + *

+ * Comparing NaN with NaN will return 0. + * + * @param lhs the first double + * @param rhs the second double + * @return -1 if lhs is less, +1 if greater, 0 if equal to rhs + */ + public static int compare(double lhs, double rhs) { + if (lhs < rhs) { + return -1; + } + if (lhs > rhs) { + return +1; + } + // Need to compare bits to handle 0.0 == -0.0 being true + // compare should put -0.0 < +0.0 + // Two NaNs are also == for compare purposes + // where NaN == NaN is false + long lhsBits = Double.doubleToLongBits(lhs); + long rhsBits = Double.doubleToLongBits(rhs); + if (lhsBits == rhsBits) { + return 0; + } + // Something exotic! A comparison to NaN or 0.0 vs -0.0 + // Fortunately NaN's long is > than everything else + // Also negzeros bits < poszero + // NAN: 9221120237041090560 + // MAX: 9218868437227405311 + // NEGZERO: -9223372036854775808 + if (lhsBits < rhsBits) { + return -1; + } else { + return +1; + } + } + + /** + * Compares two floats for order. + *

+ * This method is more comprhensive than the standard Java greater than, + * less than and equals operators. + * It returns -1 if the first value is less than the second. + * It returns +1 if the first value is greater than the second. + * It returns 0 if the values are equal. + *

+ * The ordering is as follows, largest to smallest: + *

+ * Comparing NaN with NaN will return 0. + * + * @param lhs the first float + * @param rhs the second float + * @return -1 if lhs is less, +1 if greater, 0 if equal to rhs + */ + public static int compare(float lhs, float rhs) { + if (lhs < rhs) { + return -1; + } + if (lhs > rhs) { + return +1; + } + //Need to compare bits to handle 0.0 == -0.0 being true + // compare should put -0.0 < +0.0 + // Two NaNs are also == for compare purposes + // where NaN == NaN is false + int lhsBits = Float.floatToIntBits(lhs); + int rhsBits = Float.floatToIntBits(rhs); + if (lhsBits == rhsBits) { + return 0; + } + //Something exotic! A comparison to NaN or 0.0 vs -0.0 + //Fortunately NaN's int is > than everything else + //Also negzeros bits < poszero + //NAN: 2143289344 + //MAX: 2139095039 + //NEGZERO: -2147483648 + if (lhsBits < rhsBits) { + return -1; + } else { + return +1; + } + } + + //-------------------------------------------------------------------- + /** * Checks whether the String contains only digit characters. * Null and blank string will return false.