KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > UnmodifiableSortedMap


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.map;
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.Comparator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.SortedMap JavaDoc;
27
28 import org.apache.commons.collections.Unmodifiable;
29 import org.apache.commons.collections.collection.UnmodifiableCollection;
30 import org.apache.commons.collections.set.UnmodifiableSet;
31
32 /**
33  * Decorates another <code>SortedMap</code> to ensure it can't be altered.
34  * <p>
35  * This class is Serializable from Commons Collections 3.1.
36  *
37  * @since Commons Collections 3.0
38  * @version $Revision: 1.7 $ $Date: 2004/04/09 10:46:32 $
39  *
40  * @author Stephen Colebourne
41  */

42 public final class UnmodifiableSortedMap
43         extends AbstractSortedMapDecorator
44         implements Unmodifiable, Serializable JavaDoc {
45
46     /** Serialization version */
47     private static final long serialVersionUID = 5805344239827376360L;
48
49     /**
50      * Factory method to create an unmodifiable sorted map.
51      *
52      * @param map the map to decorate, must not be null
53      * @throws IllegalArgumentException if map is null
54      */

55     public static SortedMap JavaDoc decorate(SortedMap JavaDoc map) {
56         if (map instanceof Unmodifiable) {
57             return map;
58         }
59         return new UnmodifiableSortedMap(map);
60     }
61
62     //-----------------------------------------------------------------------
63
/**
64      * Constructor that wraps (not copies).
65      *
66      * @param map the map to decorate, must not be null
67      * @throws IllegalArgumentException if map is null
68      */

69     private UnmodifiableSortedMap(SortedMap JavaDoc map) {
70         super(map);
71     }
72
73     //-----------------------------------------------------------------------
74
/**
75      * Write the map out using a custom routine.
76      *
77      * @param out the output stream
78      * @throws IOException
79      * @since Commons Collections 3.1
80      */

81     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
82         out.defaultWriteObject();
83         out.writeObject(map);
84     }
85
86     /**
87      * Read the map in using a custom routine.
88      *
89      * @param in the input stream
90      * @throws IOException
91      * @throws ClassNotFoundException
92      * @since Commons Collections 3.1
93      */

94     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
95         in.defaultReadObject();
96         map = (Map JavaDoc) in.readObject();
97     }
98
99     //-----------------------------------------------------------------------
100
public void clear() {
101         throw new UnsupportedOperationException JavaDoc();
102     }
103
104     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
105         throw new UnsupportedOperationException JavaDoc();
106     }
107
108     public void putAll(Map JavaDoc mapToCopy) {
109         throw new UnsupportedOperationException JavaDoc();
110     }
111
112     public Object JavaDoc remove(Object JavaDoc key) {
113         throw new UnsupportedOperationException JavaDoc();
114     }
115
116     public Set JavaDoc entrySet() {
117         Set JavaDoc set = super.entrySet();
118         return UnmodifiableEntrySet.decorate(set);
119     }
120
121     public Set JavaDoc keySet() {
122         Set JavaDoc set = super.keySet();
123         return UnmodifiableSet.decorate(set);
124     }
125
126     public Collection JavaDoc values() {
127         Collection JavaDoc coll = super.values();
128         return UnmodifiableCollection.decorate(coll);
129     }
130
131     //-----------------------------------------------------------------------
132
public Object JavaDoc firstKey() {
133         return getSortedMap().firstKey();
134     }
135
136     public Object JavaDoc lastKey() {
137         return getSortedMap().lastKey();
138     }
139
140     public Comparator JavaDoc comparator() {
141         return getSortedMap().comparator();
142     }
143
144     public SortedMap JavaDoc subMap(Object JavaDoc fromKey, Object JavaDoc toKey) {
145         SortedMap JavaDoc map = getSortedMap().subMap(fromKey, toKey);
146         return new UnmodifiableSortedMap(map);
147     }
148
149     public SortedMap JavaDoc headMap(Object JavaDoc toKey) {
150         SortedMap JavaDoc map = getSortedMap().headMap(toKey);
151         return new UnmodifiableSortedMap(map);
152     }
153
154     public SortedMap JavaDoc tailMap(Object JavaDoc fromKey) {
155         SortedMap JavaDoc map = getSortedMap().tailMap(fromKey);
156         return new UnmodifiableSortedMap(map);
157     }
158
159 }
160
Popular Tags