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.Transformer;
20: import org.apache.commons.collections.collection.TransformedCollection;
21:
22: /**
23: * Decorates another <code>Buffer</code> to transform objects that are added.
24: * <p>
25: * The add methods are affected by this class.
26: * Thus objects must be removed or searched for using their transformed form.
27: * For example, if the transformation converts Strings to Integers, you must
28: * use the Integer form to remove objects.
29: * <p>
30: * This class is Serializable from Commons Collections 3.1.
31: *
32: * @since Commons Collections 3.0
33: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
34: *
35: * @author Stephen Colebourne
36: */
37: public class TransformedBuffer extends TransformedCollection implements
38: Buffer {
39:
40: /** Serialization version */
41: private static final long serialVersionUID = -7901091318986132033L;
42:
43: /**
44: * Factory method to create a transforming buffer.
45: * <p>
46: * If there are any elements already in the buffer being decorated, they
47: * are NOT transformed.
48: *
49: * @param buffer the buffer to decorate, must not be null
50: * @param transformer the transformer to use for conversion, must not be null
51: * @return a new transformed Buffer
52: * @throws IllegalArgumentException if buffer or transformer is null
53: */
54: public static Buffer decorate(Buffer buffer, Transformer transformer) {
55: return new TransformedBuffer(buffer, transformer);
56: }
57:
58: //-----------------------------------------------------------------------
59: /**
60: * Constructor that wraps (not copies).
61: * <p>
62: * If there are any elements already in the buffer being decorated, they
63: * are NOT transformed.
64: *
65: * @param buffer the buffer to decorate, must not be null
66: * @param transformer the transformer to use for conversion, must not be null
67: * @throws IllegalArgumentException if buffer or transformer is null
68: */
69: protected TransformedBuffer(Buffer buffer, Transformer transformer) {
70: super (buffer, transformer);
71: }
72:
73: /**
74: * Gets the decorated buffer.
75: *
76: * @return the decorated buffer
77: */
78: protected Buffer getBuffer() {
79: return (Buffer) collection;
80: }
81:
82: //-----------------------------------------------------------------------
83: public Object get() {
84: return getBuffer().get();
85: }
86:
87: public Object remove() {
88: return getBuffer().remove();
89: }
90:
91: }
|