|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * JavaFXSmartGraph | Copyright 2024 brunomnsilva@gmail.com |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.brunomnsilva.smartgraph.graphview; |
| 26 | + |
| 27 | +/** |
| 28 | + * The Args class provides a collection of static methods for checking method parameters and throwing |
| 29 | + * IllegalArgumentExceptions if the parameters do not meet the specified requirements. |
| 30 | + * |
| 31 | + * @author brunomnsilva |
| 32 | + */ |
| 33 | +public final class Args { |
| 34 | + |
| 35 | + /** |
| 36 | + * Checks if the specified parameter is null and throws an IllegalArgumentException if it is. |
| 37 | + * |
| 38 | + * @param param the parameter to check for null |
| 39 | + * @param name the name of the parameter being checked |
| 40 | + * @throws IllegalArgumentException if the specified parameter is null |
| 41 | + */ |
| 42 | + public static void requireNotNull(Object param, String name) { |
| 43 | + if (param == null) { |
| 44 | + throw new IllegalArgumentException("Null '" + name + "' argument."); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Checks if a specified double value is greater than a specified minimum value and throws an IllegalArgumentException |
| 50 | + * if it is not. Uses a scaled comparison to avoid floating-point precision errors. |
| 51 | + * |
| 52 | + * @param value the double value to check |
| 53 | + * @param name the name of the double value being checked |
| 54 | + * @param minValue the minimum value that the specified double value must be greater than |
| 55 | + * @throws IllegalArgumentException if the specified double value is less than or equal to the specified minimum value |
| 56 | + */ |
| 57 | + public static void requireGreaterThan(double value, String name, double minValue) { |
| 58 | + if(value <= minValue) { |
| 59 | + throw new IllegalArgumentException(String.format("%s (%f) must be greater than %f", |
| 60 | + name, value, minValue)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Checks if a specified double value is non-negative and throws an IllegalArgumentException if it is not. |
| 66 | + * |
| 67 | + * @param value the double value to check |
| 68 | + * @param name the name of the double value being checked |
| 69 | + * @throws IllegalArgumentException if the specified double value is negative |
| 70 | + */ |
| 71 | + public static void requireNonNegative(double value, String name) { |
| 72 | + if (value < 0.0) { |
| 73 | + throw new IllegalArgumentException("Require '" + name + "' (" + value + ") to be non-negative."); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Checks if the given double value is finite (i.e., not NaN or infinite). |
| 79 | + * @param value the double value to check for finiteness |
| 80 | + * @param name the name of the value being checked |
| 81 | + * @throws IllegalArgumentException if the value is not finite |
| 82 | + */ |
| 83 | + public static void requireFinite(double value, String name) { |
| 84 | + if (!Double.isFinite(value)) { |
| 85 | + throw new IllegalArgumentException("Require '" + name + "' (" + value + ") to be finite."); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments