01: /*
02: * Copyright 2003-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.buffer;
17:
18: import org.apache.commons.collections.Buffer;
19: import org.apache.commons.collections.Predicate;
20: import org.apache.commons.collections.collection.PredicatedCollection;
21:
22: /**
23: * Decorates another <code>Buffer</code> to validate that additions
24: * match a specified predicate.
25: * <p>
26: * This buffer exists to provide validation for the decorated buffer.
27: * It is normally created to decorate an empty buffer.
28: * If an object cannot be added to the buffer, an IllegalArgumentException is thrown.
29: * <p>
30: * One usage would be to ensure that no null entries are added to the buffer.
31: * <pre>Buffer buffer = PredicatedBuffer.decorate(new UnboundedFifoBuffer(), NotNullPredicate.INSTANCE);</pre>
32: * <p>
33: * This class is Serializable from Commons Collections 3.1.
34: *
35: * @since Commons Collections 3.0
36: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
37: *
38: * @author Stephen Colebourne
39: * @author Paul Jack
40: */
41: public class PredicatedBuffer extends PredicatedCollection implements
42: Buffer {
43:
44: /** Serialization version */
45: private static final long serialVersionUID = 2307609000539943581L;
46:
47: /**
48: * Factory method to create a predicated (validating) buffer.
49: * <p>
50: * If there are any elements already in the buffer being decorated, they
51: * are validated.
52: *
53: * @param buffer the buffer to decorate, must not be null
54: * @param predicate the predicate to use for validation, must not be null
55: * @return a new predicated Buffer
56: * @throws IllegalArgumentException if buffer or predicate is null
57: * @throws IllegalArgumentException if the buffer contains invalid elements
58: */
59: public static Buffer decorate(Buffer buffer, Predicate predicate) {
60: return new PredicatedBuffer(buffer, predicate);
61: }
62:
63: //-----------------------------------------------------------------------
64: /**
65: * Constructor that wraps (not copies).
66: * <p>
67: * If there are any elements already in the collection being decorated, they
68: * are validated.
69: *
70: * @param buffer the buffer to decorate, must not be null
71: * @param predicate the predicate to use for validation, must not be null
72: * @throws IllegalArgumentException if buffer or predicate is null
73: * @throws IllegalArgumentException if the buffer contains invalid elements
74: */
75: protected PredicatedBuffer(Buffer buffer, Predicate predicate) {
76: super (buffer, predicate);
77: }
78:
79: /**
80: * Gets the buffer being decorated.
81: *
82: * @return the decorated buffer
83: */
84: protected Buffer getBuffer() {
85: return (Buffer) getCollection();
86: }
87:
88: //-----------------------------------------------------------------------
89: public Object get() {
90: return getBuffer().get();
91: }
92:
93: public Object remove() {
94: return getBuffer().remove();
95: }
96:
97: }
|