KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > buffer > PredicatedBuffer


1 /*
2  * Copyright 2003-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.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: 1.7 $ $Date: 2004/06/03 22:02:13 $
37  *
38  * @author Stephen Colebourne
39  * @author Paul Jack
40  */

41 public class PredicatedBuffer extends PredicatedCollection implements Buffer {
42
43     /** Serialization version */
44     private static final long serialVersionUID = 2307609000539943581L;
45
46     /**
47      * Factory method to create a predicated (validating) buffer.
48      * <p>
49      * If there are any elements already in the buffer being decorated, they
50      * are validated.
51      *
52      * @param buffer the buffer to decorate, must not be null
53      * @param predicate the predicate to use for validation, must not be null
54      * @return a new predicated Buffer
55      * @throws IllegalArgumentException if buffer or predicate is null
56      * @throws IllegalArgumentException if the buffer contains invalid elements
57      */

58     public static Buffer decorate(Buffer buffer, Predicate predicate) {
59         return new PredicatedBuffer(buffer, predicate);
60     }
61     
62     //-----------------------------------------------------------------------
63
/**
64      * Constructor that wraps (not copies).
65      * <p>
66      * If there are any elements already in the collection being decorated, they
67      * are validated.
68      *
69      * @param buffer the buffer to decorate, must not be null
70      * @param predicate the predicate to use for validation, must not be null
71      * @throws IllegalArgumentException if buffer or predicate is null
72      * @throws IllegalArgumentException if the buffer contains invalid elements
73      */

74     protected PredicatedBuffer(Buffer buffer, Predicate predicate) {
75         super(buffer, predicate);
76     }
77
78     /**
79      * Gets the buffer being decorated.
80      *
81      * @return the decorated buffer
82      */

83     protected Buffer getBuffer() {
84         return (Buffer) getCollection();
85     }
86
87     //-----------------------------------------------------------------------
88
public Object JavaDoc get() {
89         return getBuffer().get();
90     }
91
92     public Object JavaDoc remove() {
93         return getBuffer().remove();
94     }
95
96 }
97
Popular Tags