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.set;
  17. import java.util.Comparator;
  18. import java.util.SortedSet;
  19. import org.apache.commons.collections.collection.SynchronizedCollection;
  20. /**
  21. * Decorates another <code>SortedSet</code> to synchronize its behaviour
  22. * for a multi-threaded environment.
  23. * <p>
  24. * Methods are synchronized, then forwarded to the decorated set.
  25. * <p>
  26. * This class is Serializable from Commons Collections 3.1.
  27. *
  28. * @since Commons Collections 3.0
  29. * @version $Revision: 1.5 $ $Date: 2004/06/03 22:02:13 $
  30. *
  31. * @author Stephen Colebourne
  32. */
  33. public class SynchronizedSortedSet extends SynchronizedCollection implements SortedSet {
  34. /** Serialization version */
  35. private static final long serialVersionUID = 2775582861954500111L;
  36. /**
  37. * Factory method to create a synchronized set.
  38. *
  39. * @param set the set to decorate, must not be null
  40. * @throws IllegalArgumentException if set is null
  41. */
  42. public static SortedSet decorate(SortedSet set) {
  43. return new SynchronizedSortedSet(set);
  44. }
  45. //-----------------------------------------------------------------------
  46. /**
  47. * Constructor that wraps (not copies).
  48. *
  49. * @param set the set to decorate, must not be null
  50. * @throws IllegalArgumentException if set is null
  51. */
  52. protected SynchronizedSortedSet(SortedSet set) {
  53. super(set);
  54. }
  55. /**
  56. * Constructor that wraps (not copies).
  57. *
  58. * @param set the set to decorate, must not be null
  59. * @param lock the lock object to use, must not be null
  60. * @throws IllegalArgumentException if set is null
  61. */
  62. protected SynchronizedSortedSet(SortedSet set, Object lock) {
  63. super(set, lock);
  64. }
  65. /**
  66. * Gets the decorated set.
  67. *
  68. * @return the decorated set
  69. */
  70. protected SortedSet getSortedSet() {
  71. return (SortedSet) collection;
  72. }
  73. //-----------------------------------------------------------------------
  74. public SortedSet subSet(Object fromElement, Object toElement) {
  75. synchronized (lock) {
  76. SortedSet set = getSortedSet().subSet(fromElement, toElement);
  77. // the lock is passed into the constructor here to ensure that the
  78. // subset is synchronized on the same lock as the parent
  79. return new SynchronizedSortedSet(set, lock);
  80. }
  81. }
  82. public SortedSet headSet(Object toElement) {
  83. synchronized (lock) {
  84. SortedSet set = getSortedSet().headSet(toElement);
  85. // the lock is passed into the constructor here to ensure that the
  86. // headset is synchronized on the same lock as the parent
  87. return new SynchronizedSortedSet(set, lock);
  88. }
  89. }
  90. public SortedSet tailSet(Object fromElement) {
  91. synchronized (lock) {
  92. SortedSet set = getSortedSet().tailSet(fromElement);
  93. // the lock is passed into the constructor here to ensure that the
  94. // tailset is synchronized on the same lock as the parent
  95. return new SynchronizedSortedSet(set, lock);
  96. }
  97. }
  98. public Object first() {
  99. synchronized (lock) {
  100. return getSortedSet().first();
  101. }
  102. }
  103. public Object last() {
  104. synchronized (lock) {
  105. return getSortedSet().last();
  106. }
  107. }
  108. public Comparator comparator() {
  109. synchronized (lock) {
  110. return getSortedSet().comparator();
  111. }
  112. }
  113. }