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.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.io.Serializable;
  21. import java.util.Collection;
  22. import java.util.Iterator;
  23. import java.util.SortedSet;
  24. import org.apache.commons.collections.Unmodifiable;
  25. import org.apache.commons.collections.iterators.UnmodifiableIterator;
  26. /**
  27. * Decorates another <code>SortedSet</code> to ensure it can't be altered.
  28. * <p>
  29. * This class is Serializable from Commons Collections 3.1.
  30. *
  31. * @since Commons Collections 3.0
  32. * @version $Revision: 1.6 $ $Date: 2004/06/02 22:02:34 $
  33. *
  34. * @author Stephen Colebourne
  35. */
  36. public final class UnmodifiableSortedSet
  37. extends AbstractSortedSetDecorator
  38. implements Unmodifiable, Serializable {
  39. /** Serialization version */
  40. private static final long serialVersionUID = -725356885467962424L;
  41. /**
  42. * Factory method to create an unmodifiable set.
  43. *
  44. * @param set the set to decorate, must not be null
  45. * @throws IllegalArgumentException if set is null
  46. */
  47. public static SortedSet decorate(SortedSet set) {
  48. if (set instanceof Unmodifiable) {
  49. return set;
  50. }
  51. return new UnmodifiableSortedSet(set);
  52. }
  53. //-----------------------------------------------------------------------
  54. /**
  55. * Write the collection out using a custom routine.
  56. *
  57. * @param out the output stream
  58. * @throws IOException
  59. */
  60. private void writeObject(ObjectOutputStream out) throws IOException {
  61. out.defaultWriteObject();
  62. out.writeObject(collection);
  63. }
  64. /**
  65. * Read the collection in using a custom routine.
  66. *
  67. * @param in the input stream
  68. * @throws IOException
  69. * @throws ClassNotFoundException
  70. */
  71. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  72. in.defaultReadObject();
  73. collection = (Collection) in.readObject();
  74. }
  75. //-----------------------------------------------------------------------
  76. /**
  77. * Constructor that wraps (not copies).
  78. *
  79. * @param set the set to decorate, must not be null
  80. * @throws IllegalArgumentException if set is null
  81. */
  82. private UnmodifiableSortedSet(SortedSet set) {
  83. super(set);
  84. }
  85. //-----------------------------------------------------------------------
  86. public Iterator iterator() {
  87. return UnmodifiableIterator.decorate(getCollection().iterator());
  88. }
  89. public boolean add(Object object) {
  90. throw new UnsupportedOperationException();
  91. }
  92. public boolean addAll(Collection coll) {
  93. throw new UnsupportedOperationException();
  94. }
  95. public void clear() {
  96. throw new UnsupportedOperationException();
  97. }
  98. public boolean remove(Object object) {
  99. throw new UnsupportedOperationException();
  100. }
  101. public boolean removeAll(Collection coll) {
  102. throw new UnsupportedOperationException();
  103. }
  104. public boolean retainAll(Collection coll) {
  105. throw new UnsupportedOperationException();
  106. }
  107. //-----------------------------------------------------------------------
  108. public SortedSet subSet(Object fromElement, Object toElement) {
  109. SortedSet sub = getSortedSet().subSet(fromElement, toElement);
  110. return new UnmodifiableSortedSet(sub);
  111. }
  112. public SortedSet headSet(Object toElement) {
  113. SortedSet sub = getSortedSet().headSet(toElement);
  114. return new UnmodifiableSortedSet(sub);
  115. }
  116. public SortedSet tailSet(Object fromElement) {
  117. SortedSet sub = getSortedSet().tailSet(fromElement);
  118. return new UnmodifiableSortedSet(sub);
  119. }
  120. }