United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6301085 (coll) Add Collections.asLifoQueue(Deque)
JDK-6301085 : (coll) Add Collections.asLifoQueue(Deque)

Details
Type:
Enhancement
Submit Date:
2005-07-24
Status:
Closed
Updated Date:
2012-10-08
Project Name:
JDK
Resolved Date:
2005-09-04
Component:
core-libs
OS:
generic
Sub-Component:
java.util:collections
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports

Sub Tasks

Description
Add Collections.asLifoQueue(Deque)

This method was inadvertently omitted from the CCC for
6192552: Add BlockingDeque to java.util.concurrent

                                    

Comments
EVALUATION

A fine idea
                                     
2005-07-24
SUGGESTED FIX

/**
     * Returns a view of a {@link Deque} as a Last-in-first-out (Lifo)
     * {@link Queue}. Method <tt>add</tt> is mapped to <tt>push</tt>,
     * <tt>remove</tt> is mapped to <tt>pop</tt> and so on. This
     * view can be useful when you would like to use a method
     * requiring a <tt>Queue</tt> but you need Lifo ordering.
     * @param deque the deque
     * @return the queue
     * @since  1.6
     */
    public static <T> Queue<T> asLifoQueue(Deque<T> deque) {
        return new AsLIFOQueue<T>(deque);
    }

    static class AsLIFOQueue<E> extends AbstractQueue<E>
        implements Queue<E>, Serializable {
	private static final long serialVersionUID = 1802017725587941708L;
        private final Deque<E> q;
        AsLIFOQueue(Deque<E> q)            { this.q = q; }
        public boolean offer(E e)          { return q.offerFirst(e); }
        public E poll()                    { return q.pollFirst(); }
        public E remove()                  { return q.removeFirst(); }
        public E peek()                    { return q.peekFirst(); }
        public E element()                 { return q.getFirst(); }
        public int size()                  { return q.size(); }
        public boolean isEmpty()           { return q.isEmpty(); }
        public boolean contains(Object o)  { return q.contains(o); }
        public Iterator<E> iterator()      { return q.iterator(); }
        public Object[] toArray()          { return q.toArray(); }
        public <T> T[] toArray(T[] a)      { return q.toArray(a); }
        public boolean add(E e)            { return q.offerFirst(e); }
        public boolean remove(Object o)    { return q.remove(o); }
        public void clear()                { q.clear(); }
    }
}
                                     
2005-07-24



Hardware and Software, Engineered to Work Together