1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.core; |
20 | |
|
21 | |
import java.io.Serializable; |
22 | |
import java.util.concurrent.ConcurrentLinkedQueue; |
23 | |
|
24 | 11 | public enum BufferStrategy { |
25 | 1 | BY_INSTANCE { |
26 | |
@Override |
27 | |
public BufferProvider<char[]> newCharProvider(final int size) { |
28 | 0 | return new CharBufferByInstanceProvider(size); |
29 | |
} |
30 | |
|
31 | |
@Override |
32 | |
public BufferProvider<StringBuilder> newStringBuilderProvider(final int size) { |
33 | 0 | return new StringBuilderByInstanceProvider(size); |
34 | |
} |
35 | |
}, |
36 | 1 | THREAD_LOCAL { |
37 | |
@Override |
38 | |
public BufferProvider<char[]> newCharProvider(final int size) { |
39 | 0 | return new CharBufferThreadLocalProvider(size); |
40 | |
} |
41 | |
|
42 | |
@Override |
43 | |
public BufferProvider<StringBuilder> newStringBuilderProvider(final int size) { |
44 | 0 | return new StringBuilderThreadLocalProvider(size); |
45 | |
} |
46 | |
}, |
47 | 1 | QUEUE { |
48 | |
@Override |
49 | |
public BufferProvider<char[]> newCharProvider(final int size) { |
50 | 633 | return new CharBufferQueueProvider(size); |
51 | |
} |
52 | |
|
53 | |
@Override |
54 | |
public BufferProvider<StringBuilder> newStringBuilderProvider(final int size) { |
55 | 1 | return new StringBuilderQueueProvider(size); |
56 | |
} |
57 | |
}, |
58 | 1 | SINGLETON { |
59 | |
@Override |
60 | |
public BufferProvider<char[]> newCharProvider(final int size) { |
61 | 0 | return new CharBufferSingletonProvider(size); |
62 | |
} |
63 | |
|
64 | |
@Override |
65 | |
public BufferProvider<StringBuilder> newStringBuilderProvider(final int size) { |
66 | 0 | return new StringBuilderSingletonProvider(size); |
67 | |
} |
68 | |
}; |
69 | |
|
70 | |
public abstract BufferProvider<char[]> newCharProvider(int size); |
71 | |
public abstract BufferProvider<StringBuilder> newStringBuilderProvider(int size); |
72 | |
|
73 | |
public static interface BufferProvider<T> extends Serializable { |
74 | |
T newBuffer(); |
75 | |
|
76 | |
void release(T value); |
77 | |
} |
78 | |
|
79 | 0 | private static class CharBufferSingletonProvider extends SingletonProvider<char[]> { |
80 | |
public CharBufferSingletonProvider(final int size) { |
81 | 0 | super(size); |
82 | 0 | } |
83 | |
|
84 | |
@Override |
85 | |
protected char[] newInstance(int size) { |
86 | 0 | return new char[size]; |
87 | |
} |
88 | |
|
89 | |
@Override |
90 | |
public void release(final char[] value) { |
91 | |
|
92 | 0 | } |
93 | |
} |
94 | |
|
95 | 0 | private static class StringBuilderSingletonProvider extends SingletonProvider<StringBuilder> { |
96 | |
public StringBuilderSingletonProvider(final int size) { |
97 | 0 | super(size); |
98 | 0 | } |
99 | |
|
100 | |
@Override |
101 | |
protected StringBuilder newInstance(final int size) { |
102 | 0 | return new StringBuilder(size); |
103 | |
} |
104 | |
|
105 | |
@Override |
106 | |
public void release(final StringBuilder value) { |
107 | 0 | value.setLength(0); |
108 | 0 | } |
109 | |
} |
110 | |
|
111 | |
private static abstract class SingletonProvider<T> implements BufferProvider<T> { |
112 | |
protected final T buffer; |
113 | |
|
114 | 0 | public SingletonProvider(final int size) { |
115 | 0 | buffer = newInstance(size); |
116 | 0 | } |
117 | |
|
118 | |
protected abstract T newInstance(int size); |
119 | |
|
120 | |
@Override |
121 | |
public T newBuffer() { |
122 | 0 | return buffer; |
123 | |
} |
124 | |
|
125 | |
@Override |
126 | |
public void release(final T value) { |
127 | |
|
128 | 0 | } |
129 | |
} |
130 | |
|
131 | |
private static abstract class ThreadLocalProvider<T> implements BufferProvider<T> { |
132 | |
private final ThreadLocalBufferCache<T> cache; |
133 | |
|
134 | 0 | public ThreadLocalProvider(final int size) { |
135 | 0 | cache = new ThreadLocalBufferCache<T>(size) { |
136 | |
@Override |
137 | |
protected T newValue(int defaultSize) { |
138 | 0 | return newInstance(size); |
139 | |
} |
140 | |
}; |
141 | 0 | } |
142 | |
|
143 | |
protected abstract T newInstance(int size); |
144 | |
|
145 | |
@Override |
146 | |
public T newBuffer() { |
147 | 0 | return cache.getCache(); |
148 | |
} |
149 | |
|
150 | |
@Override |
151 | |
public void release(final T value) { |
152 | 0 | cache.release(value); |
153 | 0 | } |
154 | |
} |
155 | |
|
156 | 0 | private static class CharBufferThreadLocalProvider extends ThreadLocalProvider<char[]> { |
157 | |
public CharBufferThreadLocalProvider(int size) { |
158 | 0 | super(size); |
159 | 0 | } |
160 | |
|
161 | |
@Override |
162 | |
protected char[] newInstance(final int size) { |
163 | 0 | return new char[size]; |
164 | |
} |
165 | |
} |
166 | |
|
167 | 0 | private static class StringBuilderThreadLocalProvider extends ThreadLocalProvider<StringBuilder> { |
168 | |
public StringBuilderThreadLocalProvider(int size) { |
169 | 0 | super(size); |
170 | 0 | } |
171 | |
|
172 | |
@Override |
173 | |
protected StringBuilder newInstance(final int size) { |
174 | 0 | return new StringBuilder(size); |
175 | |
} |
176 | |
|
177 | |
@Override |
178 | |
public void release(final StringBuilder value) { |
179 | 0 | value.setLength(0); |
180 | 0 | super.release(value); |
181 | 0 | } |
182 | |
} |
183 | |
|
184 | 0 | private static class CharBufferByInstanceProvider implements BufferProvider<char[]> { |
185 | |
private final int size; |
186 | |
|
187 | 0 | public CharBufferByInstanceProvider(final int size) { |
188 | 0 | this.size = size; |
189 | 0 | } |
190 | |
|
191 | |
@Override |
192 | |
public char[] newBuffer() { |
193 | 0 | return new char[size]; |
194 | |
} |
195 | |
|
196 | |
@Override |
197 | |
public void release(final char[] value) { |
198 | |
|
199 | 0 | } |
200 | |
} |
201 | |
|
202 | 0 | private static class StringBuilderByInstanceProvider implements BufferProvider<StringBuilder> { |
203 | |
private final int size; |
204 | |
|
205 | 0 | public StringBuilderByInstanceProvider(final int size) { |
206 | 0 | this.size = size; |
207 | 0 | } |
208 | |
|
209 | |
@Override |
210 | |
public StringBuilder newBuffer() { |
211 | 0 | return new StringBuilder(size); |
212 | |
} |
213 | |
|
214 | |
@Override |
215 | |
public void release(final StringBuilder value) { |
216 | |
|
217 | 0 | } |
218 | |
} |
219 | |
|
220 | |
private static abstract class QueueProvider<T> implements BufferProvider<T> { |
221 | |
private final int size; |
222 | 634 | private final ConcurrentLinkedQueue<T> queue = new ConcurrentLinkedQueue<T>(); |
223 | |
|
224 | 634 | public QueueProvider(final int size) { |
225 | 634 | this.size = size; |
226 | 634 | } |
227 | |
|
228 | |
protected abstract T newInstance(int size); |
229 | |
|
230 | |
@Override |
231 | |
public T newBuffer() { |
232 | 955 | final T buffer = queue.poll(); |
233 | 955 | if (buffer == null) { |
234 | 800 | return newInstance(size); |
235 | |
} |
236 | 155 | return buffer; |
237 | |
} |
238 | |
|
239 | |
@Override |
240 | |
public void release(final T value) { |
241 | 583 | queue.offer(value); |
242 | 583 | } |
243 | |
} |
244 | |
|
245 | 799 | private static class CharBufferQueueProvider extends QueueProvider<char[]> { |
246 | |
public CharBufferQueueProvider(final int size) { |
247 | 633 | super(size); |
248 | 633 | } |
249 | |
|
250 | |
@Override |
251 | |
protected char[] newInstance(int size) { |
252 | 799 | return new char[size]; |
253 | |
} |
254 | |
} |
255 | |
|
256 | 4 | private static class StringBuilderQueueProvider extends QueueProvider<StringBuilder> { |
257 | |
public StringBuilderQueueProvider(final int size) { |
258 | 1 | super(size); |
259 | 1 | } |
260 | |
|
261 | |
@Override |
262 | |
protected StringBuilder newInstance(int size) { |
263 | 1 | return new StringBuilder(size); |
264 | |
} |
265 | |
|
266 | |
@Override |
267 | |
public void release(final StringBuilder value) { |
268 | 12 | value.setLength(0); |
269 | 12 | super.release(value); |
270 | 12 | } |
271 | |
} |
272 | |
} |