BAEL2526 queue interface code (#6115)
* BAEL2526 queue interface code * renamed test class to end with 'UnitTest', removed camel case from package name
This commit is contained in:
parent
502fdc0134
commit
44490a052f
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.queueinterface;
|
||||
|
||||
import java.util.AbstractQueue;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class CustomBaeldungQueue<T> extends AbstractQueue<T> {
|
||||
|
||||
private LinkedList<T> elements;
|
||||
|
||||
public CustomBaeldungQueue() {
|
||||
this.elements = new LinkedList<T>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return elements.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return elements.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean offer(T t) {
|
||||
if(t == null) return false;
|
||||
elements.add(t);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T poll() {
|
||||
|
||||
Iterator<T> iter = elements.iterator();
|
||||
T t = iter.next();
|
||||
if(t != null){
|
||||
iter.remove();
|
||||
return t;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T peek() {
|
||||
return elements.getFirst();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.queueinterface;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class PriorityQueueUnitTest {
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void givenIntegerQueue_whenIntegersOutOfOrder_checkRetrievalOrderIsNatural() {
|
||||
|
||||
PriorityQueue<Integer> integerQueue = new PriorityQueue<>();
|
||||
|
||||
integerQueue.add(9);
|
||||
integerQueue.add(2);
|
||||
integerQueue.add(4);
|
||||
|
||||
int first = integerQueue.poll();
|
||||
int second = integerQueue.poll();
|
||||
int third = integerQueue.poll();
|
||||
|
||||
assertEquals(2, first);
|
||||
assertEquals(4, second);
|
||||
assertEquals(9, third);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringQueue_whenStringsAddedOutOfNaturalOrder_checkRetrievalOrderNatural() {
|
||||
|
||||
PriorityQueue<String> stringQueue = new PriorityQueue<>();
|
||||
|
||||
stringQueue.add("banana");
|
||||
stringQueue.add("apple");
|
||||
stringQueue.add("cherry");
|
||||
|
||||
String first = stringQueue.poll();
|
||||
String second = stringQueue.poll();
|
||||
String third = stringQueue.poll();
|
||||
|
||||
assertEquals("apple", first);
|
||||
assertEquals("banana", second);
|
||||
assertEquals("cherry", third);
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.queueinterface;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CustomBaeldungQueueUnitTest {
|
||||
|
||||
private CustomBaeldungQueue<Integer> customQueue;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
customQueue = new CustomBaeldungQueue<>();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueueWithTwoElements_whenElementsRetrieved_checkRetrievalCorrect() {
|
||||
|
||||
customQueue.add(7);
|
||||
customQueue.add(5);
|
||||
|
||||
int first = customQueue.poll();
|
||||
int second = customQueue.poll();
|
||||
|
||||
assertEquals(7, first);
|
||||
assertEquals(5, second);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue