KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Array JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.commons.collections.Unmodifiable;
25 import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
26 import org.apache.commons.collections.keyvalue.AbstractMapEntryDecorator;
27 import org.apache.commons.collections.set.AbstractSetDecorator;
28
29 /**
30  * Decorates a map entry <code>Set</code> to ensure it can't be altered.
31  *
32  * @since Commons Collections 3.0
33  * @version $Revision: 1.6 $ $Date: 2004/02/18 01:13:19 $
34  *
35  * @author Stephen Colebourne
36  */

37 public final class UnmodifiableEntrySet
38         extends AbstractSetDecorator implements Unmodifiable {
39
40     /**
41      * Factory method to create an unmodifiable set of Map Entry objects.
42      *
43      * @param set the set to decorate, must not be null
44      * @throws IllegalArgumentException if set is null
45      */

46     public static Set JavaDoc decorate(Set JavaDoc set) {
47         if (set instanceof Unmodifiable) {
48             return set;
49         }
50         return new UnmodifiableEntrySet(set);
51     }
52
53     //-----------------------------------------------------------------------
54
/**
55      * Constructor that wraps (not copies).
56      *
57      * @param set the set to decorate, must not be null
58      * @throws IllegalArgumentException if set is null
59      */

60     private UnmodifiableEntrySet(Set JavaDoc set) {
61         super(set);
62     }
63
64     //-----------------------------------------------------------------------
65
public boolean add(Object JavaDoc object) {
66         throw new UnsupportedOperationException JavaDoc();
67     }
68
69     public boolean addAll(Collection JavaDoc coll) {
70         throw new UnsupportedOperationException JavaDoc();
71     }
72
73     public void clear() {
74         throw new UnsupportedOperationException JavaDoc();
75     }
76
77     public boolean remove(Object JavaDoc object) {
78         throw new UnsupportedOperationException JavaDoc();
79     }
80
81     public boolean removeAll(Collection JavaDoc coll) {
82         throw new UnsupportedOperationException JavaDoc();
83     }
84
85     public boolean retainAll(Collection JavaDoc coll) {
86         throw new UnsupportedOperationException JavaDoc();
87     }
88
89     //-----------------------------------------------------------------------
90
public Iterator JavaDoc iterator() {
91         return new UnmodifiableEntrySetIterator(collection.iterator());
92     }
93     
94     public Object JavaDoc[] toArray() {
95         Object JavaDoc[] array = collection.toArray();
96         for (int i = 0; i < array.length; i++) {
97             array[i] = new UnmodifiableEntry((Map.Entry JavaDoc) array[i]);
98         }
99         return array;
100     }
101     
102     public Object JavaDoc[] toArray(Object JavaDoc array[]) {
103         Object JavaDoc[] result = array;
104         if (array.length > 0) {
105             // we must create a new array to handle multi-threaded situations
106
// where another thread could access data before we decorate it
107
result = (Object JavaDoc[]) Array.newInstance(array.getClass().getComponentType(), 0);
108         }
109         result = collection.toArray(result);
110         for (int i = 0; i < result.length; i++) {
111             result[i] = new UnmodifiableEntry((Map.Entry JavaDoc) result[i]);
112         }
113
114         // check to see if result should be returned straight
115
if (result.length > array.length) {
116             return result;
117         }
118
119         // copy back into input array to fulfil the method contract
120
System.arraycopy(result, 0, array, 0, result.length);
121         if (array.length > result.length) {
122             array[result.length] = null;
123         }
124         return array;
125     }
126     
127     //-----------------------------------------------------------------------
128
/**
129      * Implementation of an entry set iterator.
130      */

131     final static class UnmodifiableEntrySetIterator extends AbstractIteratorDecorator {
132         
133         protected UnmodifiableEntrySetIterator(Iterator JavaDoc iterator) {
134             super(iterator);
135         }
136         
137         public Object JavaDoc next() {
138             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
139             return new UnmodifiableEntry(entry);
140         }
141         
142         public void remove() {
143             throw new UnsupportedOperationException JavaDoc();
144         }
145     }
146
147     //-----------------------------------------------------------------------
148
/**
149      * Implementation of a map entry that is unmodifiable.
150      */

151     final static class UnmodifiableEntry extends AbstractMapEntryDecorator {
152
153         protected UnmodifiableEntry(Map.Entry JavaDoc entry) {
154             super(entry);
155         }
156
157         public Object JavaDoc setValue(Object JavaDoc obj) {
158             throw new UnsupportedOperationException JavaDoc();
159         }
160     }
161
162 }
163
Popular Tags