KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > set > UnmodifiableSortedSet


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
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.SortedSet JavaDoc;
25
26 import org.apache.commons.collections.Unmodifiable;
27 import org.apache.commons.collections.iterators.UnmodifiableIterator;
28
29 /**
30  * Decorates another <code>SortedSet</code> to ensure it can't be altered.
31  * <p>
32  * This class is Serializable from Commons Collections 3.1.
33  *
34  * @since Commons Collections 3.0
35  * @version $Revision: 1.6 $ $Date: 2004/06/02 22:02:34 $
36  *
37  * @author Stephen Colebourne
38  */

39 public final class UnmodifiableSortedSet
40         extends AbstractSortedSetDecorator
41         implements Unmodifiable, Serializable JavaDoc {
42
43     /** Serialization version */
44     private static final long serialVersionUID = -725356885467962424L;
45
46     /**
47      * Factory method to create an unmodifiable set.
48      *
49      * @param set the set to decorate, must not be null
50      * @throws IllegalArgumentException if set is null
51      */

52     public static SortedSet JavaDoc decorate(SortedSet JavaDoc set) {
53         if (set instanceof Unmodifiable) {
54             return set;
55         }
56         return new UnmodifiableSortedSet(set);
57     }
58
59     //-----------------------------------------------------------------------
60
/**
61      * Write the collection out using a custom routine.
62      *
63      * @param out the output stream
64      * @throws IOException
65      */

66     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
67         out.defaultWriteObject();
68         out.writeObject(collection);
69     }
70
71     /**
72      * Read the collection in using a custom routine.
73      *
74      * @param in the input stream
75      * @throws IOException
76      * @throws ClassNotFoundException
77      */

78     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
79         in.defaultReadObject();
80         collection = (Collection JavaDoc) in.readObject();
81     }
82
83     //-----------------------------------------------------------------------
84
/**
85      * Constructor that wraps (not copies).
86      *
87      * @param set the set to decorate, must not be null
88      * @throws IllegalArgumentException if set is null
89      */

90     private UnmodifiableSortedSet(SortedSet JavaDoc set) {
91         super(set);
92     }
93
94     //-----------------------------------------------------------------------
95
public Iterator JavaDoc iterator() {
96         return UnmodifiableIterator.decorate(getCollection().iterator());
97     }
98
99     public boolean add(Object JavaDoc object) {
100         throw new UnsupportedOperationException JavaDoc();
101     }
102
103     public boolean addAll(Collection JavaDoc coll) {
104         throw new UnsupportedOperationException JavaDoc();
105     }
106
107     public void clear() {
108         throw new UnsupportedOperationException JavaDoc();
109     }
110
111     public boolean remove(Object JavaDoc object) {
112         throw new UnsupportedOperationException JavaDoc();
113     }
114
115     public boolean removeAll(Collection JavaDoc coll) {
116         throw new UnsupportedOperationException JavaDoc();
117     }
118
119     public boolean retainAll(Collection JavaDoc coll) {
120         throw new UnsupportedOperationException JavaDoc();
121     }
122
123     //-----------------------------------------------------------------------
124
public SortedSet JavaDoc subSet(Object JavaDoc fromElement, Object JavaDoc toElement) {
125         SortedSet JavaDoc sub = getSortedSet().subSet(fromElement, toElement);
126         return new UnmodifiableSortedSet(sub);
127     }
128
129     public SortedSet JavaDoc headSet(Object JavaDoc toElement) {
130         SortedSet JavaDoc sub = getSortedSet().headSet(toElement);
131         return new UnmodifiableSortedSet(sub);
132     }
133
134     public SortedSet JavaDoc tailSet(Object JavaDoc fromElement) {
135         SortedSet JavaDoc sub = getSortedSet().tailSet(fromElement);
136         return new UnmodifiableSortedSet(sub);
137     }
138
139 }
140
Popular Tags