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.Comparator;
  18. import org.apache.commons.collections.Bag;
  19. import org.apache.commons.collections.SortedBag;
  20. /**
  21. * Decorates another <code>SortedBag</code> to synchronize its behaviour
  22. * for a multi-threaded environment.
  23. * <p>
  24. * Methods are synchronized, then forwarded to the decorated bag.
  25. * Iterators must be separately synchronized around the loop.
  26. * <p>
  27. * This class is Serializable from Commons Collections 3.1.
  28. *
  29. * @since Commons Collections 3.0
  30. * @version $Revision: 1.8 $ $Date: 2004/06/03 22:02:12 $
  31. *
  32. * @author Stephen Colebourne
  33. */
  34. public class SynchronizedSortedBag
  35. extends SynchronizedBag implements SortedBag {
  36. /** Serialization version */
  37. private static final long serialVersionUID = 722374056718497858L;
  38. /**
  39. * Factory method to create a synchronized sorted bag.
  40. *
  41. * @param bag the bag to decorate, must not be null
  42. * @return a new synchronized SortedBag
  43. * @throws IllegalArgumentException if bag is null
  44. */
  45. public static SortedBag decorate(SortedBag bag) {
  46. return new SynchronizedSortedBag(bag);
  47. }
  48. //-----------------------------------------------------------------------
  49. /**
  50. * Constructor that wraps (not copies).
  51. *
  52. * @param bag the bag to decorate, must not be null
  53. * @throws IllegalArgumentException if bag is null
  54. */
  55. protected SynchronizedSortedBag(SortedBag bag) {
  56. super(bag);
  57. }
  58. /**
  59. * Constructor that wraps (not copies).
  60. *
  61. * @param bag the bag to decorate, must not be null
  62. * @param lock the lock to use, must not be null
  63. * @throws IllegalArgumentException if bag is null
  64. */
  65. protected SynchronizedSortedBag(Bag bag, Object lock) {
  66. super(bag, lock);
  67. }
  68. /**
  69. * Gets the bag being decorated.
  70. *
  71. * @return the decorated bag
  72. */
  73. protected SortedBag getSortedBag() {
  74. return (SortedBag) collection;
  75. }
  76. //-----------------------------------------------------------------------
  77. public synchronized Object first() {
  78. synchronized (lock) {
  79. return getSortedBag().first();
  80. }
  81. }
  82. public synchronized Object last() {
  83. synchronized (lock) {
  84. return getSortedBag().last();
  85. }
  86. }
  87. public synchronized Comparator comparator() {
  88. synchronized (lock) {
  89. return getSortedBag().comparator();
  90. }
  91. }
  92. }