View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    * 
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.    
18   */ 
19  
20  package org.apache.commons.pipeline.util;
21  
22  import java.util.Collection;
23  import java.util.Comparator;
24  import java.util.LinkedList;
25  import java.util.PriorityQueue;
26  import java.util.Queue;
27  import java.util.concurrent.ConcurrentLinkedQueue;
28  
29  import org.apache.commons.pipeline.StageDriver;
30  
31  /**
32   * Many {@link StageDriver} implementations require for one or more queues
33   * to be created. This interface provides a consistent API for factories used
34   * to create such queues and supplies a couple of default implementations.
35   */
36  public interface QueueFactory<T> {
37      /**
38       * Create a new queue.
39       */
40      public Queue<T> createQueue();
41      
42      public static abstract class AbstractQueueFactory<T> {
43          /**
44           * Holds value of property initialContents.
45           */
46          protected Collection<? extends T> initialContents;
47          
48          /**
49           * Getter for property initialContents.
50           * @return Value of property initialContents.
51           */
52          public Collection<? extends T> getInitialContents() {
53              return this.initialContents;
54          }
55          
56          /**
57           * Setter for property initialContents.
58           * @param initialContents New value of property initialContents.
59           */
60          public void setInitialContents(Collection<? extends T> initialContents) {
61              this.initialContents = initialContents;
62          }
63      }
64      
65      public static class LinkedListFactory<T> extends AbstractQueueFactory<T> implements QueueFactory<T> {
66          public LinkedList<T> createQueue() {
67              if (this.initialContents == null || this.initialContents.isEmpty()) {
68                  return new LinkedList<T>();
69              } else {
70                  return new LinkedList<T>(this.initialContents);
71              }
72          }
73      }
74      
75      public static class ConcurrentLinkedQueueFactory<T> extends AbstractQueueFactory<T> implements QueueFactory<T> {
76          public ConcurrentLinkedQueue<T> createQueue() {
77              if (this.initialContents == null || this.initialContents.isEmpty()) {
78                  return new ConcurrentLinkedQueue<T>();
79              } else {
80                  return new ConcurrentLinkedQueue<T>(this.initialContents);
81              }
82          }
83      }
84      
85      public static class PriorityQueueFactory<T> extends AbstractQueueFactory<T> implements QueueFactory<T> {
86          public PriorityQueue<T> createQueue() {
87              if (comparator == null) {
88                  if (this.initialContents == null || this.initialContents.isEmpty()) {
89                      return new PriorityQueue<T>(initialCapacity);
90                  } else {
91                      return new PriorityQueue<T>(this.initialContents);
92                  }
93              } else {
94                  PriorityQueue<T> queue = new PriorityQueue<T>(initialCapacity, comparator);
95                  queue.addAll(this.initialContents);
96                  return queue;
97              }
98          }
99          
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 }