Skip to content

Commit

Permalink
move the IllegalArgumentException throwing to outside try-catch, as c…
Browse files Browse the repository at this point in the history
…onverting the string value to double may fail with exceptions
  • Loading branch information
bowenliang123 committed Dec 1, 2023
1 parent fe2fc80 commit fab49e1
Showing 1 changed file with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,40 @@ object MathUtils {
/**
* Check if the given object represents a positive number in value
*/
def isPositiveNumber(o: Any): Boolean = o match {
case intValue: Int => intValue > 0
case longValue: Long => longValue > 0L
case floatValue: Float => floatValue > 0.0f
case doubleValue: Double => doubleValue > 0.0d
case byteValue: Byte => byteValue > 0
case shortValue: Short => shortValue > 0
case charValue: Char => charValue > 0
case stringValue: String => stringValue.toDouble >= 0.0d
case _ => throw new IllegalArgumentException(s"Cannot check if $o is a positive number.")
}
def isPositiveNumber(o: Any): Boolean =
try {
o match {
case intValue: Int => intValue > 0
case longValue: Long => longValue > 0L
case floatValue: Float => floatValue > 0.0f
case doubleValue: Double => doubleValue > 0.0d
case byteValue: Byte => byteValue > 0
case shortValue: Short => shortValue > 0
case charValue: Char => charValue > 0
case stringValue: String => stringValue.toDouble >= 0.0d
}
} catch {
case t: Throwable =>
throw new IllegalArgumentException(s"Cannot check if $o is a positive number.", t)
}

/**
* Check if the given object represents a non-negative number in value
*/
def isNonNegativeNumber(o: Any): Boolean = o match {
case intValue: Int => intValue >= 0
case longValue: Long => longValue >= 0L
case floatValue: Float => floatValue >= 0.0f
case doubleValue: Double => doubleValue >= 0.0d
case byteValue: Byte => byteValue >= 0
case shortValue: Short => shortValue >= 0
case charValue: Char => charValue >= 0
case stringValue: String => stringValue.toDouble >= 0.0d
case _ => throw new IllegalArgumentException(s"Cannot check if $o is a non-negative number.")
}
def isNonNegativeNumber(o: Any): Boolean =
try {
o match {
case intValue: Int => intValue >= 0
case longValue: Long => longValue >= 0L
case floatValue: Float => floatValue >= 0.0f
case doubleValue: Double => doubleValue >= 0.0d
case byteValue: Byte => byteValue >= 0
case shortValue: Short => shortValue >= 0
case charValue: Char => charValue >= 0
case stringValue: String => stringValue.toDouble >= 0.0d
}
} catch {
case t: Throwable =>
throw new IllegalArgumentException(s"Cannot check if $o is a non-negative number.", t)
}
}

0 comments on commit fab49e1

Please sign in to comment.