From 39ecf708578c7c08d7c3773a43649cc6694bc362 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Wed, 21 Aug 2024 15:55:36 +0200 Subject: [PATCH] refactor: `GenericArrayListQueue` (#5355) --- .../queues/GenericArrayListQueue.java | 64 +++++-------------- .../queues/GenericArrayListQueueTest.java | 55 ++++++++++++++++ 2 files changed, 72 insertions(+), 47 deletions(-) create mode 100644 src/test/java/com/thealgorithms/datastructures/queues/GenericArrayListQueueTest.java 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 { /** - * The generic ArrayList for the queue T is the generic element + * The generic List for the queue. T is the generic element type. */ - ArrayList elementList = new ArrayList<>(); + private final List 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 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(); } } 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 queue = new GenericArrayListQueue<>(); + assertTrue(queue.add(10)); + assertTrue(queue.add(20)); + } + + @Test + void testPeek() { + GenericArrayListQueue 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 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 queue = new GenericArrayListQueue<>(); + assertNull(queue.peek()); + assertNull(queue.poll()); + + queue.add(30); + assertEquals(30, queue.peek()); + assertEquals(30, queue.poll()); + assertNull(queue.peek()); + } +}