~ubuntu-branches/debian/squeeze/libcommons-collections-java/squeeze

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/collections/BufferUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2004-08-07 00:02:50 UTC
  • Revision ID: james.westby@ubuntu.com-20040807000250-hcnqvrdpxg95nmzr
Tags: upstream-2.1.1
ImportĀ upstreamĀ versionĀ 2.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 1999-2004 The Apache Software Foundation
 
3
 *
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
package org.apache.commons.collections;
 
17
 
 
18
import java.util.Collection;
 
19
/**
 
20
 * Contains static utility methods for operating on {@link Buffer} objects.
 
21
 *
 
22
 * @author Paul Jack
 
23
 * @author Stephen Colebourne
 
24
 * @version $Id: BufferUtils.java,v 1.9.2.1 2004/05/22 12:14:02 scolebourne Exp $
 
25
 * @since 2.1
 
26
 */
 
27
public class BufferUtils {
 
28
 
 
29
    /**
 
30
     * Restrictive constructor
 
31
     */
 
32
    private BufferUtils() {
 
33
    }
 
34
 
 
35
 
 
36
    /**
 
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:
 
41
     *  
 
42
     * <pre>
 
43
     * Buffer b = BufferUtils.synchronizedBuffer(myBuffer);
 
44
     * synchronized (b) {
 
45
     *     Iterator i = b.iterator();
 
46
     *     while (i.hasNext()) {
 
47
     *         process (i.next());
 
48
     *     }
 
49
     * }
 
50
     * </pre>
 
51
     *
 
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
 
55
     */
 
56
    public static Buffer synchronizedBuffer(final Buffer buffer) {
 
57
        return new SynchronizedBuffer(buffer);
 
58
    }
 
59
 
 
60
    /**
 
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>.
 
67
     *
 
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
 
71
     */
 
72
    public static Buffer blockingBuffer(Buffer buffer) {
 
73
        return new SynchronizedBuffer(buffer) {
 
74
 
 
75
            public synchronized boolean add(Object o) {
 
76
                boolean r = collection.add(o);
 
77
                notify();
 
78
                return r;
 
79
            }
 
80
 
 
81
            public synchronized boolean addAll(Collection c) {
 
82
                boolean r = collection.addAll(c);
 
83
                notifyAll();
 
84
                return r;
 
85
            }
 
86
 
 
87
            public synchronized Object get() {
 
88
                while (collection.isEmpty()) {
 
89
                    try {
 
90
                        wait();
 
91
                    } catch (InterruptedException e) {
 
92
                        throw new BufferUnderflowException();
 
93
                    }
 
94
                }
 
95
                return ((Buffer)collection).get();
 
96
            }
 
97
 
 
98
            public synchronized Object remove() {
 
99
                while (collection.isEmpty()) {
 
100
                    try {
 
101
                        wait();
 
102
                    } catch (InterruptedException e) {
 
103
                        throw new BufferUnderflowException();
 
104
                    }
 
105
                }
 
106
                return ((Buffer)collection).remove();
 
107
            }
 
108
        };
 
109
    }
 
110
 
 
111
    /**
 
112
     * Returns an unmodifiable buffer backed by the given buffer.
 
113
     *
 
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
 
117
     */
 
118
    public static Buffer unmodifiableBuffer(Buffer buffer) {
 
119
        return new UnmodifiableBuffer(buffer);
 
120
    }
 
121
 
 
122
    /**
 
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
 
127
     * the buffer.
 
128
     *
 
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
 
133
     */
 
134
    public static Buffer predicatedBuffer(Buffer buffer, final Predicate predicate) {
 
135
        return new PredicatedBuffer(buffer, predicate);
 
136
    }
 
137
 
 
138
 
 
139
 
 
140
    static class SynchronizedBuffer 
 
141
            extends CollectionUtils.SynchronizedCollection
 
142
            implements Buffer {
 
143
 
 
144
        public SynchronizedBuffer(Buffer b) {
 
145
            super(b);
 
146
        }
 
147
 
 
148
        public synchronized Object get() {
 
149
            return ((Buffer)collection).get();
 
150
        }
 
151
 
 
152
        public synchronized Object remove() {
 
153
            return ((Buffer)collection).remove();
 
154
        }        
 
155
    }
 
156
 
 
157
 
 
158
    static class UnmodifiableBuffer 
 
159
            extends CollectionUtils.UnmodifiableCollection
 
160
            implements Buffer {
 
161
 
 
162
        public UnmodifiableBuffer(Buffer b) {
 
163
            super(b);
 
164
        }
 
165
 
 
166
        public Object get() {
 
167
            return ((Buffer)collection).get();
 
168
        }
 
169
 
 
170
        public Object remove() {
 
171
            throw new UnsupportedOperationException();
 
172
        }
 
173
 
 
174
    }
 
175
 
 
176
 
 
177
    static class PredicatedBuffer 
 
178
            extends CollectionUtils.PredicatedCollection
 
179
            implements Buffer {
 
180
 
 
181
        public PredicatedBuffer(Buffer b, Predicate p) {
 
182
            super(b, p);
 
183
        }
 
184
 
 
185
        public Object get() {
 
186
            return ((Buffer)collection).get();
 
187
        }
 
188
 
 
189
        public Object remove() {
 
190
            return ((Buffer)collection).remove();
 
191
        }
 
192
 
 
193
    }
 
194
 
 
195
 
 
196
}