Skip to content

Commit 5c982c0

Browse files
committed
refactor: Enhance docs, add tests in GenericArrayListQueue
1 parent 0f1dcbe commit 5c982c0

File tree

2 files changed

+74
-30
lines changed

2 files changed

+74
-30
lines changed

src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,46 @@
44
import java.util.List;
55

66
/**
7-
* This class implements a GenericArrayListQueue.
7+
* This class implements a GenericArrayListQueue, a queue data structure that
8+
* holds elements of any type specified at runtime, allowing flexibility in the type
9+
* of elements it stores.
810
*
9-
* A GenericArrayListQueue data structure functions the same as any
10-
* specific-typed queue. The GenericArrayListQueue holds elements of types
11-
* to-be-specified at runtime. The elements that are added first are the first
12-
* to be removed (FIFO). New elements are added to the back/rear of the queue.
11+
* <p>The GenericArrayListQueue operates on a First-In-First-Out (FIFO) basis, where
12+
* elements added first are the first to be removed. New elements are added to the back
13+
* (or rear) of the queue, while removal of elements occurs from the front.
14+
*
15+
* @param <T> The type of elements held in this queue.
1316
*/
1417
public class GenericArrayListQueue<T> {
1518

1619
/**
17-
* The generic List for the queue. T is the generic element type.
20+
* A list that stores the queue's elements in insertion order.
1821
*/
1922
private final List<T> elementList = new ArrayList<>();
2023

2124
/**
2225
* Checks if the queue is empty.
2326
*
24-
* @return True if the queue is empty, false otherwise.
27+
* @return {@code true} if the queue has no elements; {@code false} otherwise.
2528
*/
26-
private boolean isEmpty() {
29+
public boolean isEmpty() {
2730
return elementList.isEmpty();
2831
}
2932

3033
/**
31-
* Returns the element at the front of the queue without removing it.
34+
* Retrieves, but does not remove, the element at the front of the queue.
3235
*
33-
* @return The element at the front of the queue, or null if the queue is empty.
36+
* @return The element at the front of the queue, or {@code null} if the queue is empty.
3437
*/
3538
public T peek() {
3639
return isEmpty() ? null : elementList.getFirst();
3740
}
3841

3942
/**
40-
* Inserts an element of type T to the back of the queue.
43+
* Inserts an element at the back of the queue.
4144
*
42-
* @param element the element to be added to the queue.
43-
* @return True if the element was added successfully.
45+
* @param element The element to be added to the queue.
46+
* @return {@code true} if the element was successfully added.
4447
*/
4548
public boolean add(T element) {
4649
return elementList.add(element);
@@ -49,7 +52,7 @@ public boolean add(T element) {
4952
/**
5053
* Retrieves and removes the element at the front of the queue.
5154
*
52-
* @return The element removed from the front of the queue, or null if the queue is empty.
55+
* @return The element removed from the front of the queue, or {@code null} if the queue is empty.
5356
*/
5457
public T poll() {
5558
return isEmpty() ? null : elementList.removeFirst();
Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.thealgorithms.datastructures.queues;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertNull;
5-
import static org.junit.jupiter.api.Assertions.assertTrue;
6-
3+
import static org.junit.jupiter.api.Assertions.*;
74
import org.junit.jupiter.api.Test;
85

96
class GenericArrayListQueueTest {
@@ -13,43 +10,87 @@ void testAdd() {
1310
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
1411
assertTrue(queue.add(10));
1512
assertTrue(queue.add(20));
13+
assertEquals(10, queue.peek()); // Ensure the first added element is at the front
1614
}
1715

1816
@Test
1917
void testPeek() {
2018
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
21-
assertNull(queue.peek());
19+
assertNull(queue.peek(), "Peek should return null for an empty queue");
2220

2321
queue.add(10);
2422
queue.add(20);
2523

26-
assertEquals(10, queue.peek());
24+
assertEquals(10, queue.peek(), "Peek should return the first element (10)");
2725
queue.poll();
28-
assertEquals(20, queue.peek());
26+
assertEquals(20, queue.peek(), "Peek should return the next element (20) after poll");
2927
}
3028

3129
@Test
3230
void testPoll() {
3331
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
34-
assertNull(queue.poll());
32+
assertNull(queue.poll(), "Poll should return null for an empty queue");
3533

3634
queue.add(10);
3735
queue.add(20);
3836

39-
assertEquals(10, queue.poll());
40-
assertEquals(20, queue.poll());
41-
assertNull(queue.poll());
37+
assertEquals(10, queue.poll(), "Poll should return and remove the first element (10)");
38+
assertEquals(20, queue.poll(), "Poll should return and remove the next element (20)");
39+
assertNull(queue.poll(), "Poll should return null when queue is empty after removals");
4240
}
4341

4442
@Test
4543
void testIsEmpty() {
4644
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
47-
assertNull(queue.peek());
48-
assertNull(queue.poll());
45+
assertTrue(queue.isEmpty(), "Queue should initially be empty");
4946

5047
queue.add(30);
51-
assertEquals(30, queue.peek());
52-
assertEquals(30, queue.poll());
53-
assertNull(queue.peek());
48+
assertFalse(queue.isEmpty(), "Queue should not be empty after adding an element");
49+
queue.poll();
50+
assertTrue(queue.isEmpty(), "Queue should be empty after removing the only element");
51+
}
52+
53+
@Test
54+
void testClearQueueAndReuse() {
55+
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
56+
queue.add(5);
57+
queue.add(10);
58+
queue.poll();
59+
queue.poll(); // Remove all elements
60+
61+
assertTrue(queue.isEmpty(), "Queue should be empty after all elements are removed");
62+
assertNull(queue.peek(), "Peek should return null on an empty queue after clear");
63+
assertTrue(queue.add(15), "Queue should be reusable after being emptied");
64+
assertEquals(15, queue.peek(), "Newly added element should be accessible in the empty queue");
65+
}
66+
67+
@Test
68+
void testOrderMaintained() {
69+
GenericArrayListQueue<String> queue = new GenericArrayListQueue<>();
70+
queue.add("First");
71+
queue.add("Second");
72+
queue.add("Third");
73+
74+
assertEquals("First", queue.poll(), "Order should be maintained; expected 'First'");
75+
assertEquals("Second", queue.poll(), "Order should be maintained; expected 'Second'");
76+
assertEquals("Third", queue.poll(), "Order should be maintained; expected 'Third'");
77+
}
78+
79+
@Test
80+
void testVariousDataTypes() {
81+
GenericArrayListQueue<Double> queue = new GenericArrayListQueue<>();
82+
queue.add(1.1);
83+
queue.add(2.2);
84+
85+
assertEquals(1.1, queue.peek(), "Queue should handle Double data type correctly");
86+
assertEquals(1.1, queue.poll(), "Poll should return correct Double value");
87+
assertEquals(2.2, queue.peek(), "Peek should show next Double value in the queue");
88+
}
89+
90+
@Test
91+
void testEmptyPollAndPeekBehavior() {
92+
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
93+
assertNull(queue.peek(), "Peek on an empty queue should return null");
94+
assertNull(queue.poll(), "Poll on an empty queue should return null");
5495
}
5596
}

0 commit comments

Comments
 (0)