Skip to content

Commit 3fbb581

Browse files
Add method to check whether a value lies between two limits
1 parent c7b75c8 commit 3fbb581

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/main/java/de/redstoneworld/redutilities/math/Calculation.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,32 @@ public static long getServerTicksFromMillis(long millis) {
2525
public static double roundToX(double input, double x) {
2626
return Math.round(input / x) * x;
2727
}
28-
}
28+
29+
/**
30+
* This method checks whether a specific value is within a range of values.
31+
*
32+
* Two limit values are used as interval limits, whose order (MIN, MAX)
33+
* is irrelevant.
34+
*
35+
* The limit is included ("Closed Interval"), if 'inclusiveLimitValues' is 'true'.
36+
* Otherwise, the limit is exclusive ("Open Interval").
37+
*
38+
* @param value (Double) the target value for the check
39+
* @param limitA (Double) the first interval limit
40+
* @param limitB (Double) the second interval limit
41+
* @param inclusiveLimitValues (Boolean) 'true', if the interval limits are included in the range
42+
* @return (Boolean) 'true', if the target values is a part of the specified interval
43+
*/
44+
public static boolean isBetween(double value, double limitA, double limitB, boolean inclusiveLimitValues) {
45+
46+
double min = Math.min(limitA, limitB);
47+
double max = Math.max(limitA, limitB);
48+
49+
if (inclusiveLimitValues) {
50+
if ((value == min) || (value == max)) return true;
51+
}
52+
53+
return ((value > min) && (value < max));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)