Skip to content

Commit 9ffd77d

Browse files
committed
Add Args class with static methods for parameter checking (refactor)
1 parent 46a5419 commit 9ffd77d

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

  • src/main/java/com/brunomnsilva/smartgraph/graphview

src/main/java/com/brunomnsilva/smartgraph/graphview/Args.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class Args {
4141
*/
4242
public static void requireNotNull(Object param, String name) {
4343
if (param == null) {
44-
throw new IllegalArgumentException("Null '" + name + "' argument.");
44+
throw new IllegalArgumentException(String.format("Require '%s' to be not null.", name));
4545
}
4646
}
4747

@@ -56,8 +56,7 @@ public static void requireNotNull(Object param, String name) {
5656
*/
5757
public static void requireGreaterThan(double value, String name, double minValue) {
5858
if(value <= minValue) {
59-
throw new IllegalArgumentException(String.format("%s (%f) must be greater than %f",
60-
name, value, minValue));
59+
throw new IllegalArgumentException(String.format("Require '%s' (%f) to be greater than %f.", name, value, minValue));
6160
}
6261
}
6362

@@ -70,7 +69,22 @@ public static void requireGreaterThan(double value, String name, double minValue
7069
*/
7170
public static void requireNonNegative(double value, String name) {
7271
if (value < 0.0) {
73-
throw new IllegalArgumentException("Require '" + name + "' (" + value + ") to be non-negative.");
72+
throw new IllegalArgumentException(String.format("Require '%s' (%f) to be non-negative.", name, value));
73+
}
74+
}
75+
76+
/**
77+
* This method checks if a value falls within a specified range.
78+
* If the value is less than the lower bound or greater than the upper bound, an IllegalArgumentException is thrown.
79+
* @param value the value to check
80+
* @param name the name of the value being checked
81+
* @param lowerBound the lower bound of the range (inclusive)
82+
* @param upperBound the upper bound of the range (inclusive)
83+
* @throws IllegalArgumentException if the value is outside the specified range
84+
*/
85+
public static void requireInRange(double value, String name, double lowerBound, double upperBound) {
86+
if (value < lowerBound || value > upperBound) {
87+
throw new IllegalArgumentException(String.format("Require '%s' (%f) to be in range [%f, %f].", name, value, lowerBound, upperBound));
7488
}
7589
}
7690

@@ -82,7 +96,7 @@ public static void requireNonNegative(double value, String name) {
8296
*/
8397
public static void requireFinite(double value, String name) {
8498
if (!Double.isFinite(value)) {
85-
throw new IllegalArgumentException("Require '" + name + "' (" + value + ") to be finite.");
99+
throw new IllegalArgumentException(String.format("Require '%s' (%f) to be finite.", name, value));
86100
}
87101
}
88102
}

0 commit comments

Comments
 (0)