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.collection.SynchronizedCollection;
  19. /**
  20. * Decorates another <code>Buffer</code> to synchronize its behaviour
  21. * for a multi-threaded environment.
  22. * <p>
  23. * Methods are synchronized, then forwarded to the decorated buffer.
  24. * <p>
  25. * This class is Serializable from Commons Collections 3.1.
  26. *
  27. * @since Commons Collections 3.0
  28. * @version $Revision: 1.6 $ $Date: 2004/06/03 22:02:13 $
  29. *
  30. * @author Stephen Colebourne
  31. */
  32. public class SynchronizedBuffer extends SynchronizedCollection implements Buffer {
  33. /** Serialization version */
  34. private static final long serialVersionUID = -6859936183953626253L;
  35. /**
  36. * Factory method to create a synchronized buffer.
  37. *
  38. * @param buffer the buffer to decorate, must not be null
  39. * @return a new synchronized Buffer
  40. * @throws IllegalArgumentException if buffer is null
  41. */
  42. public static Buffer decorate(Buffer buffer) {
  43. return new SynchronizedBuffer(buffer);
  44. }
  45. //-----------------------------------------------------------------------
  46. /**
  47. * Constructor that wraps (not copies).
  48. *
  49. * @param buffer the buffer to decorate, must not be null
  50. * @throws IllegalArgumentException if the buffer is null
  51. */
  52. protected SynchronizedBuffer(Buffer buffer) {
  53. super(buffer);
  54. }
  55. /**
  56. * Constructor that wraps (not copies).
  57. *
  58. * @param buffer the buffer to decorate, must not be null
  59. * @param lock the lock object to use, must not be null
  60. * @throws IllegalArgumentException if the buffer is null
  61. */
  62. protected SynchronizedBuffer(Buffer buffer, Object lock) {
  63. super(buffer, lock);
  64. }
  65. /**
  66. * Gets the buffer being decorated.
  67. *
  68. * @return the decorated buffer
  69. */
  70. protected Buffer getBuffer() {
  71. return (Buffer) collection;
  72. }
  73. //-----------------------------------------------------------------------
  74. public Object get() {
  75. synchronized (lock) {
  76. return getBuffer().get();
  77. }
  78. }
  79. public Object remove() {
  80. synchronized (lock) {
  81. return getBuffer().remove();
  82. }
  83. }
  84. }