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