Repository: TheAlgorithms/Java
Pull request #5355 (2024-08-20 22:23:30)

refactor: `GenericArrayListQueue`
Cleanup GenericArrayListQueue.
Readability improvements.
Added tests.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- 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 it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5355?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `50.00000%` with `4 lines` in your changes missing coverage. Please review.
> Project coverage is 44.42%. Comparing base [(`f5c0314`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/f5c0314111f5973748862e21d6e7ccbb9e53a3ee?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`9794ccd`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9794ccd91605c86cab954807ed75bd9528ce47fb?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5355?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...s/datastructures/queues/GenericArrayListQueue.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5355?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fdatastructures%2Fqueues%2FGenericArrayListQueue.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9kYXRhc3RydWN0dXJlcy9xdWV1ZXMvR2VuZXJpY0FycmF5TGlzdFF1ZXVlLmphdmE=) | 50.00% | [4 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5355?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5355      +/-   ##
============================================
+ Coverage     44.38%   44.42%   +0.04%     
- Complexity     2752     2759       +7     
============================================
  Files           522      522              
  Lines         15419    15413       -6     
  Branches       2942     2940       -2     
============================================
+ Hits           6843     6847       +4     
+ Misses         8282     8271      -11     
- Partials        294      295       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5355?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java b/src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java
index ec1e15e4..2a3a5a2c 100644
--- a/src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java
+++ b/src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java
@@ -1,6 +1,7 @@
 package com.thealgorithms.datastructures.queues;
 
 import java.util.ArrayList;
+import java.util.List;
 
 /**
  * This class implements a GenericArrayListQueue.
@@ -13,75 +14,44 @@ import java.util.ArrayList;
 public class GenericArrayListQueue<T> {
 
     /**
-     * The generic ArrayList for the queue T is the generic element
+     * The generic List for the queue. T is the generic element type.
      */
-    ArrayList<T> elementList = new ArrayList<>();
+    private final List<T> elementList = new ArrayList<>();
 
     /**
-     * Checks if the queue has elements (not empty).
+     * Checks if the queue is empty.
      *
-     * @return True if the queue has elements. False otherwise.
+     * @return True if the queue is empty, false otherwise.
      */
-    private boolean hasElements() {
-        return !elementList.isEmpty();
+    private boolean isEmpty() {
+        return elementList.isEmpty();
     }
 
     /**
-     * Checks what's at the front of the queue.
+     * Returns the element at the front of the queue without removing it.
      *
-     * @return If queue is not empty, element at the front of the queue.
-     * Otherwise, null
+     * @return The element at the front of the queue, or null if the queue is empty.
      */
     public T peek() {
-        T result = null;
-        if (this.hasElements()) {
-            result = elementList.get(0);
-        }
-        return result;
+        return isEmpty() ? null : elementList.getFirst();
     }
 
     /**
-     * Inserts an element of type T to the queue.
+     * Inserts an element of type T to the back of the queue.
      *
-     * @param element of type T to be added
-     * @return True if the element was added successfully
+     * @param element the element to be added to the queue.
+     * @return True if the element was added successfully.
      */
     public boolean add(T element) {
         return elementList.add(element);
     }
 
     /**
-     * Retrieve what's at the front of the queue
+     * Retrieves and removes the element at the front of the queue.
      *
-     * @return If queue is not empty, element retrieved. Otherwise, null
+     * @return The element removed from the front of the queue, or null if the queue is empty.
      */
-    public T pull() {
-        T result = null;
-        if (this.hasElements()) {
-            result = elementList.remove(0);
-        }
-        return result;
-    }
-
-    /**
-     * Main method
-     *
-     * @param args Command line arguments
-     */
-    public static void main(String[] args) {
-        GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
-        System.out.println("Running...");
-        assert queue.peek() == null;
-        assert queue.pull() == null;
-        assert queue.add(1);
-        assert queue.peek() == 1;
-        assert queue.add(2);
-        assert queue.peek() == 1;
-        assert queue.pull() == 1;
-        assert queue.peek() == 2;
-        assert queue.pull() == 2;
-        assert queue.peek() == null;
-        assert queue.pull() == null;
-        System.out.println("Finished.");
+    public T poll() {
+        return isEmpty() ? null : elementList.removeFirst();
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/queues/GenericArrayListQueueTest.java b/src/test/java/com/thealgorithms/datastructures/queues/GenericArrayListQueueTest.java
new file mode 100644
index 00000000..bb76b831
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/queues/GenericArrayListQueueTest.java
@@ -0,0 +1,55 @@
+package com.thealgorithms.datastructures.queues;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+class GenericArrayListQueueTest {
+
+    @Test
+    void testAdd() {
+        GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
+        assertTrue(queue.add(10));
+        assertTrue(queue.add(20));
+    }
+
+    @Test
+    void testPeek() {
+        GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
+        assertNull(queue.peek());
+
+        queue.add(10);
+        queue.add(20);
+
+        assertEquals(10, queue.peek());
+        queue.poll();
+        assertEquals(20, queue.peek());
+    }
+
+    @Test
+    void testPoll() {
+        GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
+        assertNull(queue.poll());
+
+        queue.add(10);
+        queue.add(20);
+
+        assertEquals(10, queue.poll());
+        assertEquals(20, queue.poll());
+        assertNull(queue.poll());
+    }
+
+    @Test
+    void testIsEmpty() {
+        GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
+        assertNull(queue.peek());
+        assertNull(queue.poll());
+
+        queue.add(30);
+        assertEquals(30, queue.peek());
+        assertEquals(30, queue.poll());
+        assertNull(queue.peek());
+    }
+}