Making changes into method as per Oracle specifications

This commit is contained in:
asri71 2019-03-05 17:05:11 +05:30
parent c8ee96d9d5
commit 320b5de0fe

View File

@ -4,6 +4,7 @@ import src.main.java.com.types.Queue;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.NoSuchElementException;
/** /**
* linkedList based implementation of queue. * linkedList based implementation of queue.
@ -24,18 +25,23 @@ public class GeneralQueue<T> implements Queue<T> {
public boolean add(T t) { public boolean add(T t) {
if(queue == null) { if(queue == null) {
throw new NullPointerException(); throw new IllegalStateException();
}
if(t == null){
throw new NullPointerException();
} }
queue.add(t); queue.add(t);
return true; return true;
} }
@Override @Override
public boolean remove(T t) { public boolean remove(T t) {
if(null == queue || queue.size() == 0){ if(null == queue){
throw new NullPointerException(); throw new NullPointerException();
} }
if(queue.isEmpty()) {
throw new NoSuchElementException();
}
queue.remove(t); queue.remove(t);
return true; return true;
} }
@ -65,7 +71,9 @@ public class GeneralQueue<T> implements Queue<T> {
if(null == queue) { if(null == queue) {
return false; return false;
} }
if(t == null){
throw new NullPointerException();
}
queue.add(t); queue.add(t);
return true; return true;
} }
@ -84,7 +92,7 @@ public class GeneralQueue<T> implements Queue<T> {
public T element() { public T element() {
if(queue == null || queue.isEmpty()) { if(queue == null || queue.isEmpty()) {
throw new NullPointerException(); throw new NoSuchElementException();
} }
return queue.peekFirst(); return queue.peekFirst();