001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     * 
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.    
018     */ 
019    
020    package org.apache.commons.pipeline.util;
021    
022    import java.util.Collection;
023    import java.util.Comparator;
024    import java.util.LinkedList;
025    import java.util.PriorityQueue;
026    import java.util.Queue;
027    import java.util.concurrent.ConcurrentLinkedQueue;
028    
029    import org.apache.commons.pipeline.StageDriver;
030    
031    /**
032     * Many {@link StageDriver} implementations require for one or more queues
033     * to be created. This interface provides a consistent API for factories used
034     * to create such queues and supplies a couple of default implementations.
035     */
036    public interface QueueFactory<T> {
037        /**
038         * Create a new queue.
039         */
040        public Queue<T> createQueue();
041        
042        public static abstract class AbstractQueueFactory<T> {
043            /**
044             * Holds value of property initialContents.
045             */
046            protected Collection<? extends T> initialContents;
047            
048            /**
049             * Getter for property initialContents.
050             * @return Value of property initialContents.
051             */
052            public Collection<? extends T> getInitialContents() {
053                return this.initialContents;
054            }
055            
056            /**
057             * Setter for property initialContents.
058             * @param initialContents New value of property initialContents.
059             */
060            public void setInitialContents(Collection<? extends T> initialContents) {
061                this.initialContents = initialContents;
062            }
063        }
064        
065        public static class LinkedListFactory<T> extends AbstractQueueFactory<T> implements QueueFactory<T> {
066            public LinkedList<T> createQueue() {
067                if (this.initialContents == null || this.initialContents.isEmpty()) {
068                    return new LinkedList<T>();
069                } else {
070                    return new LinkedList<T>(this.initialContents);
071                }
072            }
073        }
074        
075        public static class ConcurrentLinkedQueueFactory<T> extends AbstractQueueFactory<T> implements QueueFactory<T> {
076            public ConcurrentLinkedQueue<T> createQueue() {
077                if (this.initialContents == null || this.initialContents.isEmpty()) {
078                    return new ConcurrentLinkedQueue<T>();
079                } else {
080                    return new ConcurrentLinkedQueue<T>(this.initialContents);
081                }
082            }
083        }
084        
085        public static class PriorityQueueFactory<T> extends AbstractQueueFactory<T> implements QueueFactory<T> {
086            public PriorityQueue<T> createQueue() {
087                if (comparator == null) {
088                    if (this.initialContents == null || this.initialContents.isEmpty()) {
089                        return new PriorityQueue<T>(initialCapacity);
090                    } else {
091                        return new PriorityQueue<T>(this.initialContents);
092                    }
093                } else {
094                    PriorityQueue<T> queue = new PriorityQueue<T>(initialCapacity, comparator);
095                    queue.addAll(this.initialContents);
096                    return queue;
097                }
098            }
099            
100            /**
101             * Holds value of property initialCapacity. Default value is the same
102             * as that for java.util.concurrent.PriorityQueue.
103             */
104            private int initialCapacity = 11;
105            
106            /**
107             * Getter for property initialCapacity.
108             * @return Value of property initialCapacity.
109             */
110            public int getInitialCapacity() {
111                return this.initialCapacity;
112            }
113            
114            /**
115             * Setter for property initialCapacity.
116             * @param initialCapacity New value of property initialCapacity.
117             */
118            public void setInitialCapacity(int initialCapacity) {
119                this.initialCapacity = initialCapacity;
120            }
121            
122            /**
123             * Holds value of property comparator.
124             */
125            private Comparator<? super T> comparator;
126            
127            /**
128             * Getter for property comparator.
129             * @return Value of property comparator.
130             */
131            public Comparator<? super T> getComparator() {
132                return this.comparator;
133            }
134            
135            /**
136             * Setter for property comparator.
137             * @param comparator New value of property comparator.
138             */
139            public void setComparator(Comparator<? super T> comparator) {
140                this.comparator = comparator;
141            }
142        }
143    }