package useQueues; import queues.*; import java.util.NoSuchElementException; import java.util.Date; public class TimedQueue<E> implements Queue<E>{ /* Pair represents an element of the queue with its timestamp */ private class Pair { E data; Date time; Pair(E data) { this.data = data; this.time = new Date(); } public String toString() { return '(' + time.toString() + " , " + data.toString() + ')'; } } /* Iterator for TimedQueue */ private class TimedQueueIterator implements Iterator<E> { Iterator<Pair> it = body.iterator(); public boolean hasNext() { return it.hasNext(); } public E next() { return it.next().data; } public void remove() { throw new UnsupportedOperationException(); } } /* Before Iterator for TimedQueue */ private class BeforeIterator implements Iterator<E> { Iterator<Pair> it = body.iterator(); Pair p = null; Date time; public BeforeIterator(Date time) { this.time = time; } public boolean hasNext() { if(p == null) { if(it.hasNext()) p = it.next(); else return false; } return p.time.compareTo(time) <= 0; } public E next() { if(!hasNext()) throw new NoSuchElementException(); E result = p.data; p = null; return result; } public void remove() { throw new UnsupportedOperationException(); } } /* After Iterator for TimedQueue */ private class AfterIterator implements Iterator<E> { Iterator<Pair> it = body.iterator(); Pair p = null; Date time; public AfterIterator(Date time) { this.time = time; } public boolean hasNext() { if(p == null) { while(it.hasNext() && (p = it.next()).time.before(time)); if(p != null && !p.time.before(time)) { return true; } else return false; } return p.time.compareTo(time) >= 0; } public E next() { if(!hasNext()) throw new NoSuchElementException(); E result = p.data; p = null; return result; } public void remove() { throw new UnsupportedOperationException(); } } /* Between Iterator for TimedQueue */ private class BetweenIterator implements Iterator<E> { Iterator<Pair> it = body.iterator(); Pair p = null; Date from; Date to; public BetweenIterator(Date from, Date to) { this.from = from; this.to = to; } public boolean hasNext() { if(p == null) { while(it.hasNext() && (p = it.next()).time.before(from)); if(p != null && !p.time.before(from) && !p.time.after(to)) { return true; } else return false; } return p.time.compareTo(from) >= 0 && p.time.compareTo(to) <= 0; } public E next() { if(!hasNext()) throw new NoSuchElementException(); E result = p.data; p = null; return result; } public void remove() { throw new UnsupportedOperationException(); } } /* An ArrayQueue object holds the elements of TimedQueue */ ArrayQueue<Pair> body; public TimedQueue(int capacity) { body = new ArrayQueue<Pair>(capacity); } public boolean add(E e) { //throws IllegalStateException, NullPointerException throw new UnsupportedOperationException(); } public boolean offer(E e) { //throws NullPointerException return body.offer(new Pair(e)); } public E remove() { //throws NoSuchElementException throw new UnsupportedOperationException(); } public E poll() { return body.poll().data; } public E element() { //throws NoSuchElementException throw new UnsupportedOperationException(); } public E peek() { return body.peek().data; } public boolean contains(Object o) { throw new UnsupportedOperationException(); } public boolean remove(Object e) { //throws NoSuchElementException, UnsupportedOperationException throw new UnsupportedOperationException(); } public Iterator<E> iterator() { return new TimedQueueIterator(); } public Iterator<E> iterateAfter(Date time) { return new AfterIterator(time); } public Iterator<E> iterateBetween(Date from, Date to) { return new BetweenIterator(from, to); } public Iterator<E> iterateBefore(Date time) { return new BeforeIterator(time); } public int size() { return body.size(); } public boolean isEmpty() { return body.isEmpty(); } public void clear() { //throws UnsupportedOperationException throw new UnsupportedOperationException(); } public String toString() { StringBuffer stb = new StringBuffer("[ "); if(body.size() > 0) { Iterator<Pair> it = body.iterator(); while(it.hasNext()) stb.append(it.next().toString() + " ; "); stb.delete(stb.length()-2, stb.length()-1); stb.append("]"); } return stb.toString(); } public <T> T[] toArray(T[] a) { throw new UnsupportedOperationException(); } }