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.bag;
  17. import java.util.Set;
  18. import org.apache.commons.collections.Bag;
  19. import org.apache.commons.collections.collection.SynchronizedCollection;
  20. import org.apache.commons.collections.set.SynchronizedSet;
  21. /**
  22. * Decorates another <code>Bag</code> to synchronize its behaviour
  23. * for a multi-threaded environment.
  24. * <p>
  25. * Methods are synchronized, then forwarded to the decorated bag.
  26. * Iterators must be separately synchronized around the loop.
  27. * <p>
  28. * This class is Serializable from Commons Collections 3.1.
  29. *
  30. * @since Commons Collections 3.0
  31. * @version $Revision: 1.8 $ $Date: 2004/06/03 22:02:12 $
  32. *
  33. * @author Stephen Colebourne
  34. */
  35. public class SynchronizedBag
  36. extends SynchronizedCollection implements Bag {
  37. /** Serialization version */
  38. private static final long serialVersionUID = 8084674570753837109L;
  39. /**
  40. * Factory method to create a synchronized bag.
  41. *
  42. * @param bag the bag to decorate, must not be null
  43. * @return a new synchronized Bag
  44. * @throws IllegalArgumentException if bag is null
  45. */
  46. public static Bag decorate(Bag bag) {
  47. return new SynchronizedBag(bag);
  48. }
  49. //-----------------------------------------------------------------------
  50. /**
  51. * Constructor that wraps (not copies).
  52. *
  53. * @param bag the bag to decorate, must not be null
  54. * @throws IllegalArgumentException if bag is null
  55. */
  56. protected SynchronizedBag(Bag bag) {
  57. super(bag);
  58. }
  59. /**
  60. * Constructor that wraps (not copies).
  61. *
  62. * @param bag the bag to decorate, must not be null
  63. * @param lock the lock to use, must not be null
  64. * @throws IllegalArgumentException if bag is null
  65. */
  66. protected SynchronizedBag(Bag bag, Object lock) {
  67. super(bag, lock);
  68. }
  69. /**
  70. * Gets the bag being decorated.
  71. *
  72. * @return the decorated bag
  73. */
  74. protected Bag getBag() {
  75. return (Bag) collection;
  76. }
  77. //-----------------------------------------------------------------------
  78. public boolean add(Object object, int count) {
  79. synchronized (lock) {
  80. return getBag().add(object, count);
  81. }
  82. }
  83. public boolean remove(Object object, int count) {
  84. synchronized (lock) {
  85. return getBag().remove(object, count);
  86. }
  87. }
  88. public Set uniqueSet() {
  89. synchronized (lock) {
  90. Set set = getBag().uniqueSet();
  91. return new SynchronizedBagSet(set, lock);
  92. }
  93. }
  94. public int getCount(Object object) {
  95. synchronized (lock) {
  96. return getBag().getCount(object);
  97. }
  98. }
  99. //-----------------------------------------------------------------------
  100. /**
  101. * Synchronized Set for the Bag class.
  102. */
  103. class SynchronizedBagSet extends SynchronizedSet {
  104. /**
  105. * Constructor.
  106. * @param set the set to decorate
  107. * @param lock the lock to use, shared with the bag
  108. */
  109. SynchronizedBagSet(Set set, Object lock) {
  110. super(set, lock);
  111. }
  112. }
  113. }