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