Skip to content

Commit

Permalink
Merge 78b691f into 94b3784
Browse files Browse the repository at this point in the history
  • Loading branch information
nnivruth committed Feb 23, 2020
2 parents 94b3784 + 78b691f commit 469316e
Show file tree
Hide file tree
Showing 2 changed files with 688 additions and 0 deletions.
129 changes: 129 additions & 0 deletions src/main/java/org/apache/commons/lang3/math/NumberUtils.java
Expand Up @@ -21,6 +21,8 @@
import java.math.BigInteger;

import java.math.RoundingMode;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

Expand Down Expand Up @@ -1822,4 +1824,131 @@ public static int compare(final short x, final short y) {
public static int compare(final byte x, final byte y) {
return x - y;
}

/**
* <p>Checks if a {@code Number} value is zero,
* handling {@code null} by returning {@code false}.</p>
*
* @param number the {@code number} to check, {@code null} returns {@code false}
* @return {@code true} only if the input is non-null and equals zero
* @since 3.10
*/
public static boolean isZero(final Number number) {
if (Objects.isNull(number)) {
return false;
} else if (number instanceof Integer) {
return number.intValue() == INTEGER_ZERO;
} else if (number instanceof Long) {
return number.longValue() == LONG_ZERO;
} else if (number instanceof Byte) {
return number.byteValue() == BYTE_ZERO;
} else if (number instanceof Short) {
return number.shortValue() == SHORT_ZERO;
} else if (number instanceof Float) {
return number.floatValue() == FLOAT_ZERO;
} else if (number instanceof Double) {
return number.doubleValue() == DOUBLE_ZERO;
} else {
return number.intValue() == INTEGER_ZERO && number.longValue() == LONG_ZERO &&
number.byteValue() == BYTE_ZERO && number.shortValue() == SHORT_ZERO &&
number.floatValue() == FLOAT_ZERO && number.doubleValue() == DOUBLE_ZERO;
}
}

/**
* <p>Checks if a {@code Number} value is not zero,
* handling {@code null} by returning {@code true}.</p>
*
* @param number the {@code number} to check, {@code null} returns {@code true}
* @return {@code true} if the input is null or not equals zero
* @since 3.10
*/
public static boolean isNotZero(final Number number) {
return !isZero(number);
}

/**
* <p>Checks if a {@code Number} value is positive,
* handling {@code null} by returning {@code false}.</p>
*
* @param number the {@code number} to check, {@code null} returns {@code false}
* @return {@code true} only if the input is non-null and greater than zero
* @since 3.10
*/
public static boolean isPositive(final Number number) {
if (Objects.isNull(number)) {
return false;
} else if (number instanceof Integer) {
return number.intValue() > INTEGER_ZERO;
} else if (number instanceof Long) {
return number.longValue() > LONG_ZERO;
} else if (number instanceof Byte) {
return number.byteValue() > BYTE_ZERO;
} else if (number instanceof Short) {
return number.shortValue() > SHORT_ZERO;
} else if (number instanceof Float) {
return number.floatValue() > FLOAT_ZERO;
} else if (number instanceof Double) {
return number.doubleValue() > DOUBLE_ZERO;
} else {
return number.intValue() > INTEGER_ZERO && number.longValue() > LONG_ZERO &&
number.byteValue() > BYTE_ZERO && number.shortValue() > SHORT_ZERO &&
number.floatValue() > FLOAT_ZERO && number.doubleValue() > DOUBLE_ZERO;
}
}

/**
* <p>Checks if a {@code Number} value is not positive,
* handling {@code null} by returning {@code true}.</p>
*
* @param number the {@code number} to check, {@code null} returns {@code true}
* @return {@code true} if the input is non-null or lesser than zero
* @since 3.10
*/
public static boolean isNotPositive(final Number number) {
return !isPositive(number);
}

/**
* <p>Checks if a {@code Number} value is negative,
* handling {@code null} by returning {@code false}.</p>
*
* @param number the {@code number} to check, {@code null} returns {@code false}
* @return {@code true} only if the input is non-null and lesser than zero
* @since 3.10
*/
public static boolean isNegative(final Number number) {
if (Objects.isNull(number)) {
return false;
} else if (number instanceof Integer) {
return number.intValue() < INTEGER_ZERO;
} else if (number instanceof Long) {
return number.longValue() < LONG_ZERO;
} else if (number instanceof Byte) {
return number.byteValue() < BYTE_ZERO;
} else if (number instanceof Short) {
return number.shortValue() < SHORT_ZERO;
} else if (number instanceof Float) {
return number.floatValue() < FLOAT_ZERO;
} else if (number instanceof Double) {
return number.doubleValue() < DOUBLE_ZERO;
} else {
return number.intValue() < INTEGER_ZERO && number.longValue() < LONG_ZERO &&
number.byteValue() < BYTE_ZERO && number.shortValue() < SHORT_ZERO &&
number.floatValue() < FLOAT_ZERO && number.doubleValue() < DOUBLE_ZERO;
}
}

/**
* <p>Checks if a {@code Number} value is not negative,
* handling {@code null} by returning {@code true}.</p>
*
* @param number the {@code number} to check, {@code null} returns {@code true}
* @return {@code true} if the input is non-null or greater than zero
* @since 3.10
*/
public static boolean isNotNegative(final Number number) {
return !isNegative(number);
}

}

0 comments on commit 469316e

Please sign in to comment.