001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.set;
017:
018: import java.io.IOException;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import java.io.Serializable;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.SortedSet;
025:
026: import org.apache.commons.collections.Unmodifiable;
027: import org.apache.commons.collections.iterators.UnmodifiableIterator;
028:
029: /**
030: * Decorates another <code>SortedSet</code> to ensure it can't be altered.
031: * <p>
032: * This class is Serializable from Commons Collections 3.1.
033: *
034: * @since Commons Collections 3.0
035: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
036: *
037: * @author Stephen Colebourne
038: */
039: public final class UnmodifiableSortedSet extends
040: AbstractSortedSetDecorator implements Unmodifiable,
041: Serializable {
042:
043: /** Serialization version */
044: private static final long serialVersionUID = -725356885467962424L;
045:
046: /**
047: * Factory method to create an unmodifiable set.
048: *
049: * @param set the set to decorate, must not be null
050: * @throws IllegalArgumentException if set is null
051: */
052: public static SortedSet decorate(SortedSet set) {
053: if (set instanceof Unmodifiable) {
054: return set;
055: }
056: return new UnmodifiableSortedSet(set);
057: }
058:
059: //-----------------------------------------------------------------------
060: /**
061: * Write the collection out using a custom routine.
062: *
063: * @param out the output stream
064: * @throws IOException
065: */
066: private void writeObject(ObjectOutputStream out) throws IOException {
067: out.defaultWriteObject();
068: out.writeObject(collection);
069: }
070:
071: /**
072: * Read the collection in using a custom routine.
073: *
074: * @param in the input stream
075: * @throws IOException
076: * @throws ClassNotFoundException
077: */
078: private void readObject(ObjectInputStream in) throws IOException,
079: ClassNotFoundException {
080: in.defaultReadObject();
081: collection = (Collection) in.readObject();
082: }
083:
084: //-----------------------------------------------------------------------
085: /**
086: * Constructor that wraps (not copies).
087: *
088: * @param set the set to decorate, must not be null
089: * @throws IllegalArgumentException if set is null
090: */
091: private UnmodifiableSortedSet(SortedSet set) {
092: super (set);
093: }
094:
095: //-----------------------------------------------------------------------
096: public Iterator iterator() {
097: return UnmodifiableIterator
098: .decorate(getCollection().iterator());
099: }
100:
101: public boolean add(Object object) {
102: throw new UnsupportedOperationException();
103: }
104:
105: public boolean addAll(Collection coll) {
106: throw new UnsupportedOperationException();
107: }
108:
109: public void clear() {
110: throw new UnsupportedOperationException();
111: }
112:
113: public boolean remove(Object object) {
114: throw new UnsupportedOperationException();
115: }
116:
117: public boolean removeAll(Collection coll) {
118: throw new UnsupportedOperationException();
119: }
120:
121: public boolean retainAll(Collection coll) {
122: throw new UnsupportedOperationException();
123: }
124:
125: //-----------------------------------------------------------------------
126: public SortedSet subSet(Object fromElement, Object toElement) {
127: SortedSet sub = getSortedSet().subSet(fromElement, toElement);
128: return new UnmodifiableSortedSet(sub);
129: }
130:
131: public SortedSet headSet(Object toElement) {
132: SortedSet sub = getSortedSet().headSet(toElement);
133: return new UnmodifiableSortedSet(sub);
134: }
135:
136: public SortedSet tailSet(Object fromElement) {
137: SortedSet sub = getSortedSet().tailSet(fromElement);
138: return new UnmodifiableSortedSet(sub);
139: }
140:
141: }
|