1717
1818import org .strongback .function .DoubleToDoubleFunction ;
1919
20-
2120/**
2221 * Utility class for working with values.
2322 *
@@ -75,10 +74,12 @@ private static double calcTolerance(int bits) {
7574 * @throws IllegalArgumentException if the tolerance is negative
7675 */
7776 public static int fuzzyCompare (double a , double b , double tolerance ) {
78- if ( tolerance < 0.0 ) throw new IllegalArgumentException ("The tolerance may not be negative" );
77+ if (tolerance < 0.0 ) throw new IllegalArgumentException ("The tolerance may not be negative" );
7978 double difference = a - b ;
80- return (Math .abs (difference ) <= tolerance ? 0 : // the two values are within the tolerance of each other
81- (difference > 0 ? 1 : // the first is greater than the second
79+ return (Math .abs (difference ) <= tolerance ? 0
80+ : // the two values are within the tolerance of each other
81+ (difference > 0 ? 1
82+ : // the first is greater than the second
8283 -1 )); // the first is less than the second
8384 }
8485
@@ -92,7 +93,8 @@ public static int fuzzyCompare(double a, double b, double tolerance) {
9293 * @throws IllegalArgumentException if the minimum value is greater than the maximum value
9394 */
9495 public static double limit (double minimum , double num , double maximum ) {
95- if ( maximum < minimum ) throw new IllegalArgumentException ("The minimum value must be less than or equal to the maximum value" );
96+ if (maximum < minimum ) throw new IllegalArgumentException (
97+ "The minimum value must be less than or equal to the maximum value" );
9698 if (num > maximum ) {
9799 return maximum ;
98100 }
@@ -112,7 +114,8 @@ public static double limit(double minimum, double num, double maximum) {
112114 * @throws IllegalArgumentException if the minimum value is greater than the maximum value
113115 */
114116 public static DoubleToDoubleFunction limiter (double minimum , double maximum ) {
115- if ( maximum < minimum ) throw new IllegalArgumentException ("The minimum value must be less than or equal to the maximum value" );
117+ if (maximum < minimum ) throw new IllegalArgumentException (
118+ "The minimum value must be less than or equal to the maximum value" );
116119 return new DoubleToDoubleFunction () {
117120 @ Override
118121 public double applyAsDouble (double value ) {
@@ -138,9 +141,10 @@ public double applyAsDouble(double value) {
138141 * @throws IllegalArgumentException if the minimum and maximum values are invalid
139142 */
140143 public static double symmetricLimit (double minimum , double num , double maximum ) {
141- if ( minimum < 0 ) throw new IllegalArgumentException ("The minimum value may not be negative" );
142- if ( maximum < 0 ) throw new IllegalArgumentException ("The maximum value may not be negative" );
143- if ( maximum < minimum ) throw new IllegalArgumentException ("The minimum value must be less than or equal to the maximum value" );
144+ if (minimum < 0 ) throw new IllegalArgumentException ("The minimum value may not be negative" );
145+ if (maximum < 0 ) throw new IllegalArgumentException ("The maximum value may not be negative" );
146+ if (maximum < minimum ) throw new IllegalArgumentException (
147+ "The minimum value must be less than or equal to the maximum value" );
144148 if (num > maximum ) {
145149 return maximum ;
146150 }
@@ -160,9 +164,10 @@ public static double symmetricLimit(double minimum, double num, double maximum)
160164 * @return the function that limits to the maximum and minimum values; never null
161165 */
162166 public static DoubleToDoubleFunction symmetricLimiter (double minimum , double maximum ) {
163- if ( minimum < 0 ) throw new IllegalArgumentException ("The minimum value may not be negative" );
164- if ( maximum < 0 ) throw new IllegalArgumentException ("The maximum value may not be negative" );
165- if ( maximum < minimum ) throw new IllegalArgumentException ("The minimum value must be less than or equal to the maximum value" );
167+ if (minimum < 0 ) throw new IllegalArgumentException ("The minimum value may not be negative" );
168+ if (maximum < 0 ) throw new IllegalArgumentException ("The maximum value may not be negative" );
169+ if (maximum < minimum ) throw new IllegalArgumentException (
170+ "The minimum value must be less than or equal to the maximum value" );
166171 return new DoubleToDoubleFunction () {
167172 @ Override
168173 public double applyAsDouble (double num ) {
@@ -178,4 +183,63 @@ public double applyAsDouble(double num) {
178183 };
179184 }
180185
186+ public static interface RangeMaker {
187+ DoubleToDoubleFunction toRange (double minOutputValue , double maxOutputValue );
188+ }
189+
190+ /**
191+ * Begin to create a function that maps from the specified range of input values. To obtain a mapping function, call the
192+ * {@link RangeMaker#toRange(double, double)} method on the resulting {@link RangeMaker} instance.
193+ * <p>
194+ * This is equivalent to calling {@link #mapRange(double, double, double, double)} with the input and output range limits.
195+ * For example:
196+ *
197+ * <pre>
198+ * Values.mapRange(-1.0,1.0).toRange(0.0,1.0);
199+ * </pre>
200+ *
201+ * is equivalent to:
202+ *
203+ * <pre>
204+ * Values.mapRange(-1.0,1.0,0.0,1.0);
205+ * </pre>
206+ *
207+ * @param minInputValue the minimum value of the range of input values to the function
208+ * @param maxInputValue the maximum value of the range of input values to the function
209+ * @return the range maker that should be used to complete the function; never null
210+ * @see #mapRange(double, double, double, double)
211+ */
212+ public static RangeMaker mapRange (double minInputValue , double maxInputValue ) {
213+ return (minOutput , maxOutput ) -> {
214+ return mapRange (minInputValue , maxInputValue , minOutput , maxOutput );
215+ };
216+ }
217+
218+ /**
219+ * Create a function that maps from the specified range of input values
220+ *
221+ * @param minInputValue the minimum value of the range of input values to the function
222+ * @param maxInputValue the maximum value of the range of input values to the function
223+ * @param minOutputValue the minimum value for the output of the function
224+ * @param maxOutputValue the maximum value for the output of the function
225+ * @return the mapping function; never null
226+ * @see #mapRange(double, double)
227+ */
228+ public static DoubleToDoubleFunction mapRange (double minInputValue , double maxInputValue , double minOutputValue ,
229+ double maxOutputValue ) {
230+ double factor = (maxOutputValue - minOutputValue ) / (maxInputValue - minInputValue );
231+ return new DoubleToDoubleFunction () {
232+ @ Override
233+ public double applyAsDouble (double num ) {
234+ if (num <= minInputValue ) return minOutputValue ;
235+ if (num >= maxInputValue ) return maxOutputValue ;
236+ double output = minOutputValue + ((num - minInputValue ) * factor );
237+ if (output < minOutputValue )
238+ output = minOutputValue ;
239+ else if (output > maxOutputValue ) output = maxOutputValue ;
240+ return output ;
241+ }
242+ };
243+ }
244+
181245}
0 commit comments