1. /*
  2. * Copyright 2002-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;
  17. import org.apache.commons.collections.buffer.BlockingBuffer;
  18. import org.apache.commons.collections.buffer.PredicatedBuffer;
  19. import org.apache.commons.collections.buffer.SynchronizedBuffer;
  20. import org.apache.commons.collections.buffer.TransformedBuffer;
  21. import org.apache.commons.collections.buffer.TypedBuffer;
  22. import org.apache.commons.collections.buffer.UnmodifiableBuffer;
  23. /**
  24. * Provides utility methods and decorators for {@link Buffer} instances.
  25. *
  26. * @since Commons Collections 2.1
  27. * @version $Revision: 1.20 $ $Date: 2004/04/01 20:12:00 $
  28. *
  29. * @author Paul Jack
  30. * @author Stephen Colebourne
  31. */
  32. public class BufferUtils {
  33. /**
  34. * An empty unmodifiable buffer.
  35. */
  36. public static final Buffer EMPTY_BUFFER = UnmodifiableBuffer.decorate(new ArrayStack(1));
  37. /**
  38. * <code>BufferUtils</code> should not normally be instantiated.
  39. */
  40. public BufferUtils() {
  41. }
  42. //-----------------------------------------------------------------------
  43. /**
  44. * Returns a synchronized buffer backed by the given buffer.
  45. * Much like the synchronized collections returned by
  46. * {@link java.util.Collections}, you must manually synchronize on
  47. * the returned buffer's iterator to avoid non-deterministic behavior:
  48. *
  49. * <pre>
  50. * Buffer b = BufferUtils.synchronizedBuffer(myBuffer);
  51. * synchronized (b) {
  52. * Iterator i = b.iterator();
  53. * while (i.hasNext()) {
  54. * process (i.next());
  55. * }
  56. * }
  57. * </pre>
  58. *
  59. * @param buffer the buffer to synchronize, must not be null
  60. * @return a synchronized buffer backed by that buffer
  61. * @throws IllegalArgumentException if the Buffer is null
  62. */
  63. public static Buffer synchronizedBuffer(Buffer buffer) {
  64. return SynchronizedBuffer.decorate(buffer);
  65. }
  66. /**
  67. * Returns a synchronized buffer backed by the given buffer that will
  68. * block on {@link Buffer#get()} and {@link Buffer#remove()} operations.
  69. * If the buffer is empty, then the {@link Buffer#get()} and
  70. * {@link Buffer#remove()} operations will block until new elements
  71. * are added to the buffer, rather than immediately throwing a
  72. * <code>BufferUnderflowException</code>.
  73. *
  74. * @param buffer the buffer to synchronize, must not be null
  75. * @return a blocking buffer backed by that buffer
  76. * @throws IllegalArgumentException if the Buffer is null
  77. */
  78. public static Buffer blockingBuffer(Buffer buffer) {
  79. return BlockingBuffer.decorate(buffer);
  80. }
  81. /**
  82. * Returns an unmodifiable buffer backed by the given buffer.
  83. *
  84. * @param buffer the buffer to make unmodifiable, must not be null
  85. * @return an unmodifiable buffer backed by that buffer
  86. * @throws IllegalArgumentException if the Buffer is null
  87. */
  88. public static Buffer unmodifiableBuffer(Buffer buffer) {
  89. return UnmodifiableBuffer.decorate(buffer);
  90. }
  91. /**
  92. * Returns a predicated (validating) buffer backed by the given buffer.
  93. * <p>
  94. * Only objects that pass the test in the given predicate can be added to the buffer.
  95. * Trying to add an invalid object results in an IllegalArgumentException.
  96. * It is important not to use the original buffer after invoking this method,
  97. * as it is a backdoor for adding invalid objects.
  98. *
  99. * @param buffer the buffer to predicate, must not be null
  100. * @param predicate the predicate used to evaluate new elements, must not be null
  101. * @return a predicated buffer
  102. * @throws IllegalArgumentException if the Buffer or Predicate is null
  103. */
  104. public static Buffer predicatedBuffer(Buffer buffer, Predicate predicate) {
  105. return PredicatedBuffer.decorate(buffer, predicate);
  106. }
  107. /**
  108. * Returns a typed buffer backed by the given buffer.
  109. * <p>
  110. * Only elements of the specified type can be added to the buffer.
  111. *
  112. * @param buffer the buffer to predicate, must not be null
  113. * @param type the type to allow into the buffer, must not be null
  114. * @return a typed buffer
  115. * @throws IllegalArgumentException if the buffer or type is null
  116. */
  117. public static Buffer typedBuffer(Buffer buffer, Class type) {
  118. return TypedBuffer.decorate(buffer, type);
  119. }
  120. /**
  121. * Returns a transformed buffer backed by the given buffer.
  122. * <p>
  123. * Each object is passed through the transformer as it is added to the
  124. * Buffer. It is important not to use the original buffer after invoking this
  125. * method, as it is a backdoor for adding untransformed objects.
  126. *
  127. * @param buffer the buffer to predicate, must not be null
  128. * @param transformer the transformer for the buffer, must not be null
  129. * @return a transformed buffer backed by the given buffer
  130. * @throws IllegalArgumentException if the Buffer or Transformer is null
  131. */
  132. public static Buffer transformedBuffer(Buffer buffer, Transformer transformer) {
  133. return TransformedBuffer.decorate(buffer, transformer);
  134. }
  135. }