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.Predicate;
  19. import org.apache.commons.collections.collection.PredicatedCollection;
  20. /**
  21. * Decorates another <code>Buffer</code> to validate that additions
  22. * match a specified predicate.
  23. * <p>
  24. * This buffer exists to provide validation for the decorated buffer.
  25. * It is normally created to decorate an empty buffer.
  26. * If an object cannot be added to the buffer, an IllegalArgumentException is thrown.
  27. * <p>
  28. * One usage would be to ensure that no null entries are added to the buffer.
  29. * <pre>Buffer buffer = PredicatedBuffer.decorate(new UnboundedFifoBuffer(), NotNullPredicate.INSTANCE);</pre>
  30. * <p>
  31. * This class is Serializable from Commons Collections 3.1.
  32. *
  33. * @since Commons Collections 3.0
  34. * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:13 $
  35. *
  36. * @author Stephen Colebourne
  37. * @author Paul Jack
  38. */
  39. public class PredicatedBuffer extends PredicatedCollection implements Buffer {
  40. /** Serialization version */
  41. private static final long serialVersionUID = 2307609000539943581L;
  42. /**
  43. * Factory method to create a predicated (validating) buffer.
  44. * <p>
  45. * If there are any elements already in the buffer being decorated, they
  46. * are validated.
  47. *
  48. * @param buffer the buffer to decorate, must not be null
  49. * @param predicate the predicate to use for validation, must not be null
  50. * @return a new predicated Buffer
  51. * @throws IllegalArgumentException if buffer or predicate is null
  52. * @throws IllegalArgumentException if the buffer contains invalid elements
  53. */
  54. public static Buffer decorate(Buffer buffer, Predicate predicate) {
  55. return new PredicatedBuffer(buffer, predicate);
  56. }
  57. //-----------------------------------------------------------------------
  58. /**
  59. * Constructor that wraps (not copies).
  60. * <p>
  61. * If there are any elements already in the collection being decorated, they
  62. * are validated.
  63. *
  64. * @param buffer the buffer to decorate, must not be null
  65. * @param predicate the predicate to use for validation, must not be null
  66. * @throws IllegalArgumentException if buffer or predicate is null
  67. * @throws IllegalArgumentException if the buffer contains invalid elements
  68. */
  69. protected PredicatedBuffer(Buffer buffer, Predicate predicate) {
  70. super(buffer, predicate);
  71. }
  72. /**
  73. * Gets the buffer being decorated.
  74. *
  75. * @return the decorated buffer
  76. */
  77. protected Buffer getBuffer() {
  78. return (Buffer) getCollection();
  79. }
  80. //-----------------------------------------------------------------------
  81. public Object get() {
  82. return getBuffer().get();
  83. }
  84. public Object remove() {
  85. return getBuffer().remove();
  86. }
  87. }