2
* Copyright 1999-2004 The Apache Software Foundation
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package org.apache.commons.collections;
18
import java.util.Collection;
20
* Contains static utility methods for operating on {@link Buffer} objects.
23
* @author Stephen Colebourne
24
* @version $Id: BufferUtils.java,v 1.9.2.1 2004/05/22 12:14:02 scolebourne Exp $
27
public class BufferUtils {
30
* Restrictive constructor
32
private BufferUtils() {
37
* Returns a synchronized buffer backed by the given buffer.
38
* Much like the synchronized collections returned by
39
* {@link java.util.Collections}, you must manually synchronize on
40
* the returned buffer's iterator to avoid non-deterministic behavior:
43
* Buffer b = BufferUtils.synchronizedBuffer(myBuffer);
45
* Iterator i = b.iterator();
46
* while (i.hasNext()) {
52
* @param buffer the buffer to synchronize, must not be null
53
* @return a synchronized buffer backed by that buffer
54
* @throws IllegalArgumentException if the Buffer is null
56
public static Buffer synchronizedBuffer(final Buffer buffer) {
57
return new SynchronizedBuffer(buffer);
61
* Returns a synchronized buffer backed by the given buffer that will
62
* block on {@link Buffer#get()} and {@link Buffer#remove()} operations.
63
* If the buffer is empty, then the {@link Buffer#get()} and
64
* {@link Buffer#remove()} operations will block until new elements
65
* are added to the buffer, rather than immediately throwing a
66
* <code>BufferUnderflowException</code>.
68
* @param buffer the buffer to synchronize, must not be null
69
* @return a blocking buffer backed by that buffer
70
* @throws IllegalArgumentException if the Buffer is null
72
public static Buffer blockingBuffer(Buffer buffer) {
73
return new SynchronizedBuffer(buffer) {
75
public synchronized boolean add(Object o) {
76
boolean r = collection.add(o);
81
public synchronized boolean addAll(Collection c) {
82
boolean r = collection.addAll(c);
87
public synchronized Object get() {
88
while (collection.isEmpty()) {
91
} catch (InterruptedException e) {
92
throw new BufferUnderflowException();
95
return ((Buffer)collection).get();
98
public synchronized Object remove() {
99
while (collection.isEmpty()) {
102
} catch (InterruptedException e) {
103
throw new BufferUnderflowException();
106
return ((Buffer)collection).remove();
112
* Returns an unmodifiable buffer backed by the given buffer.
114
* @param buffer the buffer to make unmodifiable, must not be null
115
* @return an unmodifiable buffer backed by that buffer
116
* @throws IllegalArgumentException if the Buffer is null
118
public static Buffer unmodifiableBuffer(Buffer buffer) {
119
return new UnmodifiableBuffer(buffer);
123
* Returns a predicated buffer backed by the given buffer. Elements are
124
* evaluated with the given predicate before being added to the buffer.
125
* If the predicate evaluation returns false, then an
126
* IllegalArgumentException is raised and the element is not added to
129
* @param buffer the buffer to predicate, must not be null
130
* @param predicate the predicate used to evaluate new elements, must not be null
131
* @return a predicated buffer
132
* @throws IllegalArgumentException if the Buffer or Predicate is null
134
public static Buffer predicatedBuffer(Buffer buffer, final Predicate predicate) {
135
return new PredicatedBuffer(buffer, predicate);
140
static class SynchronizedBuffer
141
extends CollectionUtils.SynchronizedCollection
144
public SynchronizedBuffer(Buffer b) {
148
public synchronized Object get() {
149
return ((Buffer)collection).get();
152
public synchronized Object remove() {
153
return ((Buffer)collection).remove();
158
static class UnmodifiableBuffer
159
extends CollectionUtils.UnmodifiableCollection
162
public UnmodifiableBuffer(Buffer b) {
166
public Object get() {
167
return ((Buffer)collection).get();
170
public Object remove() {
171
throw new UnsupportedOperationException();
177
static class PredicatedBuffer
178
extends CollectionUtils.PredicatedCollection
181
public PredicatedBuffer(Buffer b, Predicate p) {
185
public Object get() {
186
return ((Buffer)collection).get();
189
public Object remove() {
190
return ((Buffer)collection).remove();