1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35
36 public interface QueueFactory<T> {
37
38
39
40 public Queue<T> createQueue();
41
42 public static abstract class AbstractQueueFactory<T> {
43
44
45
46 protected Collection<? extends T> initialContents;
47
48
49
50
51
52 public Collection<? extends T> getInitialContents() {
53 return this.initialContents;
54 }
55
56
57
58
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
102
103
104 private int initialCapacity = 11;
105
106
107
108
109
110 public int getInitialCapacity() {
111 return this.initialCapacity;
112 }
113
114
115
116
117
118 public void setInitialCapacity(int initialCapacity) {
119 this.initialCapacity = initialCapacity;
120 }
121
122
123
124
125 private Comparator<? super T> comparator;
126
127
128
129
130
131 public Comparator<? super T> getComparator() {
132 return this.comparator;
133 }
134
135
136
137
138
139 public void setComparator(Comparator<? super T> comparator) {
140 this.comparator = comparator;
141 }
142 }
143 }