|
| 1 | +package io.github.kennedykori.utils; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | +import org.checkerframework.dataflow.qual.Pure; |
| 5 | + |
| 6 | +/** |
| 7 | + * Collection of utilities for checking and validating number. |
| 8 | + * |
| 9 | + * <p>The following checks are supported: |
| 10 | + * |
| 11 | + * <ul> |
| 12 | + * <li>inRange checks. |
| 13 | + * <li>inRangeOpen checks. |
| 14 | + * </ul> |
| 15 | + * |
| 16 | + * <p>The following validations are supported: |
| 17 | + * |
| 18 | + * <ul> |
| 19 | + * <li>requireEqual |
| 20 | + * <li>requireGreaterOrEqual |
| 21 | + * <li>requireGreaterThan |
| 22 | + * <li>requireInRange |
| 23 | + * <li>requireInRangeExclusive |
| 24 | + * <li>requireLessOrEqual |
| 25 | + * <li>requireLessThan |
| 26 | + * <li>requireNegative |
| 27 | + * <li>requireNonNegative |
| 28 | + * <li>requireNotEqual |
| 29 | + * </ul> |
| 30 | + * |
| 31 | + * <p>The following number types are supported: |
| 32 | + * |
| 33 | + * <ul> |
| 34 | + * <li>byte |
| 35 | + * <li>int |
| 36 | + * <li>long |
| 37 | + * <li>float |
| 38 | + * <li>double |
| 39 | + * <li>big decimal |
| 40 | + * </ul> |
| 41 | + */ |
| 42 | +public final class Numbers { |
| 43 | + |
| 44 | + /** |
| 45 | + * Checks if a value falls within a given lower and upper bound(inclusive for both). |
| 46 | + * |
| 47 | + * <p>Returns {@code true} if {@code value} is greater than or equal to {@code start} and less |
| 48 | + * than or equal to {@code stop}. |
| 49 | + * |
| 50 | + * @param value the value to check for inclusion in the range. |
| 51 | + * @param start the lower bound(inclusive) of the range. <b>MUST NOT</b> be greater than {@code |
| 52 | + * stop}. |
| 53 | + * @param stop the upper bound(inclusive) of the range. |
| 54 | + * @return {@code true} if {@code value} is greater than or equal to {@code start} and less than |
| 55 | + * or equal to {@code stop}. |
| 56 | + * @throws IllegalArgumentException if {@code start} is greater than {@code stop}. |
| 57 | + */ |
| 58 | + @Pure |
| 59 | + public static boolean inRange(byte value, byte start, byte stop) { |
| 60 | + if (stop < start) { |
| 61 | + throw new IllegalArgumentException("stop MUST be >= start."); |
| 62 | + } |
| 63 | + return start <= value && value <= stop; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Checks if a value falls within a given lower and upper bound(inclusive for both). |
| 68 | + * |
| 69 | + * <p>Returns {@code true} if {@code value} is greater than or equal to {@code start} and less |
| 70 | + * than or equal to {@code stop}. |
| 71 | + * |
| 72 | + * @param value the value to check for inclusion in the range. |
| 73 | + * @param start the lower bound(inclusive) of the range. <b>MUST NOT</b> be greater than {@code |
| 74 | + * stop}. |
| 75 | + * @param stop the upper bound(inclusive) of the range. |
| 76 | + * @return {@code true} if {@code value} is greater than or equal to {@code start} and less than |
| 77 | + * or equal to {@code stop}. |
| 78 | + * @throws IllegalArgumentException if {@code start} is greater than {@code stop}. |
| 79 | + */ |
| 80 | + @Pure |
| 81 | + public static boolean inRange(int value, int start, int stop) { |
| 82 | + if (stop < start) { |
| 83 | + throw new IllegalArgumentException("stop MUST be >= start."); |
| 84 | + } |
| 85 | + return start <= value && value <= stop; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Checks if a value falls within a given lower and upper bound(inclusive for both). |
| 90 | + * |
| 91 | + * <p>Returns {@code true} if {@code value} is greater than or equal to {@code start} and less |
| 92 | + * than or equal to {@code stop}. |
| 93 | + * |
| 94 | + * @param value the value to check for inclusion in the range. |
| 95 | + * @param start the lower bound(inclusive) of the range. <b>MUST NOT</b> be greater than {@code |
| 96 | + * stop}. |
| 97 | + * @param stop the upper bound(inclusive) of the range. |
| 98 | + * @return {@code true} if {@code value} is greater than or equal to {@code start} and less than |
| 99 | + * or equal to {@code stop}. |
| 100 | + * @throws IllegalArgumentException if {@code start} is greater than {@code stop}. |
| 101 | + */ |
| 102 | + @Pure |
| 103 | + public static boolean inRange(long value, long start, long stop) { |
| 104 | + if (stop < start) { |
| 105 | + throw new IllegalArgumentException("stop MUST be >= start."); |
| 106 | + } |
| 107 | + return start <= value && value <= stop; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Checks if a value falls within a given lower and upper bound(inclusive for both). |
| 112 | + * |
| 113 | + * <p>Returns {@code true} if {@code value} is greater than or equal to {@code start} and less |
| 114 | + * than or equal to {@code stop}. |
| 115 | + * |
| 116 | + * @param value the value to check for inclusion in the range. |
| 117 | + * @param start the lower bound(inclusive) of the range. <b>MUST NOT</b> be greater than {@code |
| 118 | + * stop}. |
| 119 | + * @param stop the upper bound(inclusive) of the range. |
| 120 | + * @return {@code true} if {@code value} is greater than or equal to {@code start} and less than |
| 121 | + * or equal to {@code stop}. |
| 122 | + * @throws IllegalArgumentException if {@code start} is greater than {@code stop}. |
| 123 | + */ |
| 124 | + @Pure |
| 125 | + public static boolean inRange(float value, float start, float stop) { |
| 126 | + if (stop < start) { |
| 127 | + throw new IllegalArgumentException("stop MUST be >= start."); |
| 128 | + } |
| 129 | + return Float.compare(start, value) <= 0 && Float.compare(value, stop) <= 0; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Checks if a value falls within a given lower and upper bound(inclusive for both). |
| 134 | + * |
| 135 | + * <p>Returns {@code true} if {@code value} is greater than or equal to {@code start} and less |
| 136 | + * than or equal to {@code stop}. |
| 137 | + * |
| 138 | + * @param value the value to check for inclusion in the range. |
| 139 | + * @param start the lower bound(inclusive) of the range. <b>MUST NOT</b> be greater than {@code |
| 140 | + * stop}. |
| 141 | + * @param stop the upper bound(inclusive) of the range. |
| 142 | + * @return {@code true} if {@code value} is greater than or equal to {@code start} and less than |
| 143 | + * or equal to {@code stop}. |
| 144 | + * @throws IllegalArgumentException if {@code start} is greater than {@code stop}. |
| 145 | + */ |
| 146 | + @Pure |
| 147 | + public static boolean inRange(double value, double start, double stop) { |
| 148 | + if (stop < start) { |
| 149 | + throw new IllegalArgumentException("stop MUST be >= start."); |
| 150 | + } |
| 151 | + return Double.compare(start, value) <= 0 && Double.compare(value, stop) <= 0; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Checks if a value falls within a given lower and upper bound(inclusive for both). |
| 156 | + * |
| 157 | + * <p>Returns {@code true} if {@code value} is greater than or equal to {@code start} and less |
| 158 | + * than or equal to {@code stop}. |
| 159 | + * |
| 160 | + * @param value the value to check for inclusion in the range. |
| 161 | + * @param start the lower bound(inclusive) of the range. <b>MUST NOT</b> be greater than {@code |
| 162 | + * stop}. |
| 163 | + * @param stop the upper bound(inclusive) of the range. |
| 164 | + * @return {@code true} if {@code value} is greater than or equal to {@code start} and less than |
| 165 | + * or equal to {@code stop}. |
| 166 | + * @throws IllegalArgumentException if {@code start} is greater than {@code stop}. |
| 167 | + */ |
| 168 | + @Pure |
| 169 | + public static boolean inRange(BigDecimal value, BigDecimal start, BigDecimal stop) { |
| 170 | + if (stop.compareTo(start) < 0) { |
| 171 | + throw new IllegalArgumentException("stop MUST be >= start."); |
| 172 | + } |
| 173 | + return start.compareTo(value) <= 0 && value.compareTo(stop) <= 0; |
| 174 | + } |
| 175 | +} |
0 commit comments