Repository: TheAlgorithms/Java
Pull request #6016 (2024-10-26 04:13:34)

refactor: Enhance docs, add tests in `GenericArrayListQueue`
<!--
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/6016?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 69.09%. Comparing base [(`0f1dcbe`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/0f1dcbe47930d9a30f1beaee359c183669b4d1fb?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`3b49ecb`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/3b49ecbd9ee3b721ae3adcfe1e254ac6aab5be62?dropdown=coverage&el=desc&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    #6016      +/-   ##
============================================
+ Coverage     69.07%   69.09%   +0.01%     
- Complexity     4665     4667       +2     
============================================
  Files           624      624              
  Lines         17166    17166              
  Branches       3311     3311              
============================================
+ Hits          11857    11860       +3     
+ Misses         4855     4854       -1     
+ Partials        454      452       -2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6016?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 2a3a5a2c..865da7bf 100644
--- a/src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java
+++ b/src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java
@@ -4,43 +4,46 @@ import java.util.ArrayList;
 import java.util.List;
 
 /**
- * This class implements a GenericArrayListQueue.
+ * This class implements a GenericArrayListQueue, a queue data structure that
+ * holds elements of any type specified at runtime, allowing flexibility in the type
+ * of elements it stores.
  *
- * A GenericArrayListQueue data structure functions the same as any
- * specific-typed queue. The GenericArrayListQueue holds elements of types
- * to-be-specified at runtime. The elements that are added first are the first
- * to be removed (FIFO). New elements are added to the back/rear of the queue.
+ * <p>The GenericArrayListQueue operates on a First-In-First-Out (FIFO) basis, where
+ * elements added first are the first to be removed. New elements are added to the back
+ * (or rear) of the queue, while removal of elements occurs from the front.
+ *
+ * @param <T> The type of elements held in this queue.
  */
 public class GenericArrayListQueue<T> {
 
     /**
-     * The generic List for the queue. T is the generic element type.
+     * A list that stores the queue's elements in insertion order.
      */
     private final List<T> elementList = new ArrayList<>();
 
     /**
      * Checks if the queue is empty.
      *
-     * @return True if the queue is empty, false otherwise.
+     * @return {@code true} if the queue has no elements; {@code false} otherwise.
      */
-    private boolean isEmpty() {
+    public boolean isEmpty() {
         return elementList.isEmpty();
     }
 
     /**
-     * Returns the element at the front of the queue without removing it.
+     * Retrieves, but does not remove, the element at the front of the queue.
      *
-     * @return The element at the front of the queue, or null if the queue is empty.
+     * @return The element at the front of the queue, or {@code null} if the queue is empty.
      */
     public T peek() {
         return isEmpty() ? null : elementList.getFirst();
     }
 
     /**
-     * Inserts an element of type T to the back of the queue.
+     * Inserts an element at the back of the queue.
      *
-     * @param element the element to be added to the queue.
-     * @return True if the element was added successfully.
+     * @param element The element to be added to the queue.
+     * @return {@code true} if the element was successfully added.
      */
     public boolean add(T element) {
         return elementList.add(element);
@@ -49,7 +52,7 @@ public class GenericArrayListQueue<T> {
     /**
      * Retrieves and removes the element at the front of the queue.
      *
-     * @return The element removed from the front of the queue, or null if the queue is empty.
+     * @return The element removed from the front of the queue, or {@code null} if the queue is empty.
      */
     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
index bb76b831..b19513e1 100644
--- a/src/test/java/com/thealgorithms/datastructures/queues/GenericArrayListQueueTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/queues/GenericArrayListQueueTest.java
@@ -1,6 +1,7 @@
 package com.thealgorithms.datastructures.queues;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -13,43 +14,87 @@ class GenericArrayListQueueTest {
         GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
         assertTrue(queue.add(10));
         assertTrue(queue.add(20));
+        assertEquals(10, queue.peek()); // Ensure the first added element is at the front
     }
 
     @Test
     void testPeek() {
         GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
-        assertNull(queue.peek());
+        assertNull(queue.peek(), "Peek should return null for an empty queue");
 
         queue.add(10);
         queue.add(20);
 
-        assertEquals(10, queue.peek());
+        assertEquals(10, queue.peek(), "Peek should return the first element (10)");
         queue.poll();
-        assertEquals(20, queue.peek());
+        assertEquals(20, queue.peek(), "Peek should return the next element (20) after poll");
     }
 
     @Test
     void testPoll() {
         GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
-        assertNull(queue.poll());
+        assertNull(queue.poll(), "Poll should return null for an empty queue");
 
         queue.add(10);
         queue.add(20);
 
-        assertEquals(10, queue.poll());
-        assertEquals(20, queue.poll());
-        assertNull(queue.poll());
+        assertEquals(10, queue.poll(), "Poll should return and remove the first element (10)");
+        assertEquals(20, queue.poll(), "Poll should return and remove the next element (20)");
+        assertNull(queue.poll(), "Poll should return null when queue is empty after removals");
     }
 
     @Test
     void testIsEmpty() {
         GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
-        assertNull(queue.peek());
-        assertNull(queue.poll());
+        assertTrue(queue.isEmpty(), "Queue should initially be empty");
 
         queue.add(30);
-        assertEquals(30, queue.peek());
-        assertEquals(30, queue.poll());
-        assertNull(queue.peek());
+        assertFalse(queue.isEmpty(), "Queue should not be empty after adding an element");
+        queue.poll();
+        assertTrue(queue.isEmpty(), "Queue should be empty after removing the only element");
+    }
+
+    @Test
+    void testClearQueueAndReuse() {
+        GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
+        queue.add(5);
+        queue.add(10);
+        queue.poll();
+        queue.poll(); // Remove all elements
+
+        assertTrue(queue.isEmpty(), "Queue should be empty after all elements are removed");
+        assertNull(queue.peek(), "Peek should return null on an empty queue after clear");
+        assertTrue(queue.add(15), "Queue should be reusable after being emptied");
+        assertEquals(15, queue.peek(), "Newly added element should be accessible in the empty queue");
+    }
+
+    @Test
+    void testOrderMaintained() {
+        GenericArrayListQueue<String> queue = new GenericArrayListQueue<>();
+        queue.add("First");
+        queue.add("Second");
+        queue.add("Third");
+
+        assertEquals("First", queue.poll(), "Order should be maintained; expected 'First'");
+        assertEquals("Second", queue.poll(), "Order should be maintained; expected 'Second'");
+        assertEquals("Third", queue.poll(), "Order should be maintained; expected 'Third'");
+    }
+
+    @Test
+    void testVariousDataTypes() {
+        GenericArrayListQueue<Double> queue = new GenericArrayListQueue<>();
+        queue.add(1.1);
+        queue.add(2.2);
+
+        assertEquals(1.1, queue.peek(), "Queue should handle Double data type correctly");
+        assertEquals(1.1, queue.poll(), "Poll should return correct Double value");
+        assertEquals(2.2, queue.peek(), "Peek should show next Double value in the queue");
+    }
+
+    @Test
+    void testEmptyPollAndPeekBehavior() {
+        GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
+        assertNull(queue.peek(), "Peek on an empty queue should return null");
+        assertNull(queue.poll(), "Poll on an empty queue should return null");
     }
 }