Skip to content

Commit 44490a0

Browse files
sjmillingtonpivovarit
authored andcommitted
BAEL2526 queue interface code (eugenp#6115)
* BAEL2526 queue interface code * renamed test class to end with 'UnitTest', removed camel case from package name
1 parent 502fdc0 commit 44490a0

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.queueinterface;
2+
3+
import java.util.AbstractQueue;
4+
import java.util.Iterator;
5+
import java.util.LinkedList;
6+
7+
public class CustomBaeldungQueue<T> extends AbstractQueue<T> {
8+
9+
private LinkedList<T> elements;
10+
11+
public CustomBaeldungQueue() {
12+
this.elements = new LinkedList<T>();
13+
}
14+
15+
@Override
16+
public Iterator<T> iterator() {
17+
return elements.iterator();
18+
}
19+
20+
@Override
21+
public int size() {
22+
return elements.size();
23+
}
24+
25+
@Override
26+
public boolean offer(T t) {
27+
if(t == null) return false;
28+
elements.add(t);
29+
return true;
30+
}
31+
32+
@Override
33+
public T poll() {
34+
35+
Iterator<T> iter = elements.iterator();
36+
T t = iter.next();
37+
if(t != null){
38+
iter.remove();
39+
return t;
40+
}
41+
return null;
42+
}
43+
44+
@Override
45+
public T peek() {
46+
return elements.getFirst();
47+
}
48+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.queueinterface;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.PriorityQueue;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class PriorityQueueUnitTest {
11+
12+
13+
14+
@Test
15+
public void givenIntegerQueue_whenIntegersOutOfOrder_checkRetrievalOrderIsNatural() {
16+
17+
PriorityQueue<Integer> integerQueue = new PriorityQueue<>();
18+
19+
integerQueue.add(9);
20+
integerQueue.add(2);
21+
integerQueue.add(4);
22+
23+
int first = integerQueue.poll();
24+
int second = integerQueue.poll();
25+
int third = integerQueue.poll();
26+
27+
assertEquals(2, first);
28+
assertEquals(4, second);
29+
assertEquals(9, third);
30+
31+
32+
}
33+
34+
@Test
35+
public void givenStringQueue_whenStringsAddedOutOfNaturalOrder_checkRetrievalOrderNatural() {
36+
37+
PriorityQueue<String> stringQueue = new PriorityQueue<>();
38+
39+
stringQueue.add("banana");
40+
stringQueue.add("apple");
41+
stringQueue.add("cherry");
42+
43+
String first = stringQueue.poll();
44+
String second = stringQueue.poll();
45+
String third = stringQueue.poll();
46+
47+
assertEquals("apple", first);
48+
assertEquals("banana", second);
49+
assertEquals("cherry", third);
50+
51+
52+
}
53+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.queueinterface;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class CustomBaeldungQueueUnitTest {
9+
10+
private CustomBaeldungQueue<Integer> customQueue;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
customQueue = new CustomBaeldungQueue<>();
15+
}
16+
17+
@Test
18+
public void givenQueueWithTwoElements_whenElementsRetrieved_checkRetrievalCorrect() {
19+
20+
customQueue.add(7);
21+
customQueue.add(5);
22+
23+
int first = customQueue.poll();
24+
int second = customQueue.poll();
25+
26+
assertEquals(7, first);
27+
assertEquals(5, second);
28+
29+
}
30+
}

0 commit comments

Comments
 (0)