Repository: TheAlgorithms/Java
Pull request #3022 (2022-04-19 23:01:30)

Code refactor for AbsoluteMin improvements
### **Describe your change:**

- [ ] Add an algorithm?
- [x] Fix a bug or typo in an existing algorithm?
- [x] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

See: https://github.com/TheAlgorithms/Java/issues/3021

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All functions and variable names follow Java naming conventions.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/AbsoluteMin.java b/src/main/java/com/thealgorithms/maths/AbsoluteMin.java
index bdb8eb1a..9b6f0e74 100644
--- a/src/main/java/com/thealgorithms/maths/AbsoluteMin.java
+++ b/src/main/java/com/thealgorithms/maths/AbsoluteMin.java
@@ -2,35 +2,31 @@ package com.thealgorithms.maths;
 
 import java.util.Arrays;
 
-/**
- * description:
- *
- * <p>
- * absMin([0, 5, 1, 11]) = 0, absMin([3 , -10, -2]) = -2
- */
 public class AbsoluteMin {
 
-    public static void main(String[] args) {
-        int[] testnums = {4, 0, 16};
-        assert absMin(testnums) == 0;
-
-        int[] numbers = {3, -10, -2};
-        System.out.println("absMin(" + Arrays.toString(numbers) + ") = " + absMin(numbers));
-    }
-
     /**
-     * get the value, returns the absolute min value min
+     * Compares the numbers given as arguments to get the absolute min value.
      *
-     * @param numbers contains elements
-     * @return the absolute min value
+     * @param numbers The numbers to compare
+     * @return The absolute min value
      */
-    public static int absMin(int[] numbers) {
-        int absMinValue = numbers[0];
-        for (int i = 1, length = numbers.length; i < length; ++i) {
-            if (Math.abs(numbers[i]) < Math.abs(absMinValue)) {
-                absMinValue = numbers[i];
-            }
+    public static int getMinValue(int... numbers) {
+        if (numbers.length == 0) {
+            throw new IllegalArgumentException("Numbers array cannot be empty");
         }
-        return absMinValue;
+
+        var absMinWrapper = new Object() {
+            int value = numbers[0];
+        };
+
+        Arrays.stream(numbers)
+                .skip(1)
+                .forEach(number -> {
+                    if (Math.abs(number) < Math.abs(absMinWrapper.value)) {
+                        absMinWrapper.value = number;
+                    }
+                });
+
+        return absMinWrapper.value;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java b/src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java
new file mode 100644
index 00000000..571b080c
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java
@@ -0,0 +1,21 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class AbsoluteMinTest {
+
+    @Test
+    void testGetMinValue() {
+        assertEquals(0, AbsoluteMin.getMinValue(4, 0, 16));
+        assertEquals(-2, AbsoluteMin.getMinValue(3, -10, -2));
+    }
+
+    @Test
+    void testGetMinValueWithNoArguments() {
+        Exception exception = assertThrows(IllegalArgumentException.class, () -> AbsoluteMin.getMinValue());
+        assertEquals("Numbers array cannot be empty", exception.getMessage());
+    }
+}