Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReadOnlyHashMap |
|
| 0.0;0 |
1 | /* | |
2 | * Licensed under the MIT License | |
3 | * | |
4 | * Copyright 2010 (c) Flávio Silva, http://flsilva.com | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person | |
7 | * obtaining a copy of this software and associated documentation | |
8 | * files (the "Software"), to deal in the Software without | |
9 | * restriction, including without limitation the rights to use, | |
10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the | |
12 | * Software is furnished to do so, subject to the following | |
13 | * conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be | |
16 | * included in all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
25 | * OTHER DEALINGS IN THE SOFTWARE. | |
26 | * | |
27 | * http://www.opensource.org/licenses/mit-license.php | |
28 | */ | |
29 | ||
30 | 1 | package org.as3collections.maps |
31 | { | |
32 | import org.as3collections.AbstractHashMap; | |
33 | import org.as3collections.ICollection; | |
34 | import org.as3collections.IIterator; | |
35 | import org.as3collections.IMap; | |
36 | import org.as3collections.IMapEntry; | |
37 | import org.as3collections.iterators.ReadOnlyMapIterator; | |
38 | import org.as3coreaddendum.errors.UnsupportedOperationError; | |
39 | import org.as3utils.ReflectionUtil; | |
40 | ||
41 | /** | |
42 | * A <code>HashMap</code> that doesn't allow modifications. | |
43 | * It receives all the mappings by its constructor and can no longer be changed. | |
44 | * All methods that change the map will throw an <code>UnsupportedOperationError</code>. | |
45 | * | |
46 | * @example | |
47 | * | |
48 | * <listing version="3.0"> | |
49 | * import org.as3collections.IMap; | |
50 | * import org.as3collections.maps.HashMap; | |
51 | * import org.as3collections.maps.ReadOnlyHashMap; | |
52 | * | |
53 | * var obj:Object = {fa:"fb",ga:"gb",ha:"hb"} | |
54 | * | |
55 | * var map1:IMap = new HashMap(); | |
56 | * | |
57 | * map1.putAllByObject(obj); | |
58 | * | |
59 | * map1 // {ha=hb,ga=gb,fa=fb} | |
60 | * map1.size() // 3 | |
61 | * | |
62 | * var map2:IMap = new ReadOnlyHashMap(map1); | |
63 | * | |
64 | * map2 // {ha=hb,ga=gb,fa=fb} | |
65 | * map2.size() // 3 | |
66 | * | |
67 | * map2.put(1, 2) // UnsupportedOperationError: ReadOnlyHashMap is a read-only map and doesn't allow modifications. | |
68 | * | |
69 | * map2.remove(1) // UnsupportedOperationError: ReadOnlyHashMap is a read-only map and doesn't allow modifications. | |
70 | * </listing> | |
71 | * | |
72 | * @author Flávio Silva | |
73 | */ | |
74 | public class ReadOnlyHashMap extends AbstractHashMap | |
75 | { | |
76 | /** | |
77 | * Constructor, creates a new <code>ReadOnlyHashMap</code> object. | |
78 | * | |
79 | * @param source an map to fill the list. | |
80 | * @throws ArgumentError if the <code>source</code> argument is <code>null</code>. | |
81 | */ | |
82 | public function ReadOnlyHashMap(source:IMap) | |
83 | 1 | { |
84 | 1 | if (!source) throw new ArgumentError("The 'source' argument must not be 'null'."); |
85 | ||
86 | 1 | var it:IIterator = source.iterator(); |
87 | var value:*; | |
88 | ||
89 | 1 | while (it.hasNext()) |
90 | { | |
91 | 1 | value = it.next(); |
92 | ||
93 | 1 | map[it.pointer()] = value; |
94 | 1 | values[value] = 1; |
95 | ||
96 | 1 | keyAdded(it.pointer()); |
97 | 1 | valueAdded(value); |
98 | ||
99 | 1 | _size++; |
100 | } | |
101 | 1 | } |
102 | ||
103 | /** | |
104 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
105 | * | |
106 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyHashMap</code> is a read-only map and doesn't allow modifications. | |
107 | */ | |
108 | override public function clear(): void | |
109 | { | |
110 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only map and doesn't allow modifications."); |
111 | } | |
112 | ||
113 | /** | |
114 | * Creates and return a new <code>ReadOnlyHashMap</code> object containing all mappings in this map. | |
115 | * | |
116 | * @return a new <code>ReadOnlyHashMap</code> object containing all mappings in this map. | |
117 | */ | |
118 | override public function clone(): * | |
119 | { | |
120 | 1 | return new ReadOnlyHashMap(this); |
121 | } | |
122 | ||
123 | /** | |
124 | * Returns an iterator over a set of mappings. | |
125 | * <p>This implementation returns a <code>ReadOnlyMapIterator</code> object.</p> | |
126 | * | |
127 | * @return an iterator over a set of values. | |
128 | * @see org.as3collections.iterators.ReadOnlyMapIterator ReadOnlyMapIterator | |
129 | */ | |
130 | override public function iterator(): IIterator | |
131 | { | |
132 | 1 | return new ReadOnlyMapIterator(this); |
133 | } | |
134 | ||
135 | /** | |
136 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
137 | * | |
138 | * @param key | |
139 | * @param value | |
140 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyHashMap</code> is a read-only map and doesn't allow modifications. | |
141 | * @return | |
142 | */ | |
143 | override public function put(key:*, value:*): * | |
144 | { | |
145 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only map and doesn't allow modifications."); |
146 | } | |
147 | ||
148 | /** | |
149 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
150 | * | |
151 | * @param map | |
152 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyHashMap</code> is a read-only map and doesn't allow modifications. | |
153 | */ | |
154 | override public function putAll(map:IMap): void | |
155 | { | |
156 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only map and doesn't allow modifications."); |
157 | } | |
158 | ||
159 | /** | |
160 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
161 | * | |
162 | * @param o | |
163 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyHashMap</code> is a read-only map and doesn't allow modifications. | |
164 | */ | |
165 | override public function putAllByObject(o:Object): void | |
166 | { | |
167 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only map and doesn't allow modifications."); |
168 | } | |
169 | ||
170 | /** | |
171 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
172 | * | |
173 | * @param entry | |
174 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyHashMap</code> is a read-only map and doesn't allow modifications. | |
175 | * @return | |
176 | */ | |
177 | override public function putEntry(entry:IMapEntry): * | |
178 | { | |
179 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only map and doesn't allow modifications."); |
180 | } | |
181 | ||
182 | /** | |
183 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
184 | * | |
185 | * @param key | |
186 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyHashMap</code> is a read-only map and doesn't allow modifications. | |
187 | * @return | |
188 | */ | |
189 | override public function remove(key:*): * | |
190 | { | |
191 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only map and doesn't allow modifications."); |
192 | } | |
193 | ||
194 | /** | |
195 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
196 | * | |
197 | * @param keys | |
198 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyHashMap</code> is a read-only map and doesn't allow modifications. | |
199 | * @return | |
200 | */ | |
201 | override public function removeAll(keys:ICollection): Boolean | |
202 | { | |
203 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only map and doesn't allow modifications."); |
204 | } | |
205 | ||
206 | /** | |
207 | * This implementation always throws an <code>UnsupportedOperationError</code>. | |
208 | * | |
209 | * @param keys | |
210 | * @throws org.as3coreaddendum.errors.UnsupportedOperationError <code>ReadOnlyHashMap</code> is a read-only map and doesn't allow modifications. | |
211 | * @return | |
212 | */ | |
213 | override public function retainAll(keys:ICollection): Boolean | |
214 | { | |
215 | 1 | throw new UnsupportedOperationError(ReflectionUtil.getClassName(this) + " is a read-only map and doesn't allow modifications."); |
216 | } | |
217 | ||
218 | } | |
219 | ||
220 | } |