Allow early termination of iteration
[platform/upstream/libgee.git] / gee / readonlymap.vala
1 /* readonlymap.vala
2  *
3  * Copyright (C) 2007-2008  Jürg Billeter
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18  *
19  * Author:
20  *      Jürg Billeter <j@bitron.ch>
21  */
22
23 using GLib;
24
25 /**
26  * Read-only view for {@link Map} collections.
27  *
28  * This class decorates any class which implements the {@link Map} interface
29  * by making it read only. Any method which normally modify data will throw an
30  * error.
31  *
32  * @see Map
33  */
34 internal class Gee.ReadOnlyMap<K,V> : Object, Traversable<Map.Entry<K,V>>, Iterable<Map.Entry<K,V>>, Map<K,V> {
35
36         /**
37          * {@inheritDoc}
38          */
39         public int size {
40                 get { return _map.size; }
41         }
42
43         /**
44          * {@inheritDoc}
45          */
46         public bool is_empty {
47                 get { return _map.is_empty; }
48         }
49
50         /**
51          * {@inheritDoc}
52          */
53         public bool read_only {
54                 get { return true; }
55         }
56
57         /**
58          * {@inheritDoc}
59          */
60         public Set<K> keys {
61                 owned get {
62                         return _map.keys;
63                 }
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         public Collection<V> values {
70                 owned get {
71                         return _map.values;
72                 }
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         public Set<Map.Entry<K,V>> entries {
79                 owned get {
80                         return _map.entries;
81                 }
82         }
83
84         protected Map<K,V> _map;
85
86         /**
87          * Constructs a read-only map that mirrors the content of the specified map.
88          *
89          * @param map the map to decorate.
90          */
91         public ReadOnlyMap (Map<K,V> map) {
92                 this._map = map;
93         }
94
95         /**
96          * {@inheritDoc}
97          */
98         public bool has_key (K key) {
99                 return _map.has_key (key);
100         }
101
102         /**
103          * {@inheritDoc}
104          */
105         public bool contains (K key) {
106                 return _map.has_key (key);
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         public bool has (K key, V value) {
113                 return _map.has (key, value);
114         }
115
116         /**
117          * {@inheritDoc}
118          */
119         public new V? get (K key) {
120                 return _map.get (key);
121         }
122
123         /**
124          * Unimplemented method (read only map).
125          */
126         public new void set (K key, V value) {
127                 assert_not_reached ();
128         }
129
130         /**
131          * Unimplemented method (read only map).
132          */
133         public bool unset (K key, out V? value = null) {
134                 assert_not_reached ();
135         }
136
137         /**
138          * Unimplemented method (read only map).
139          */
140         public bool remove (K key, out V? value = null) {
141                 assert_not_reached ();
142         }
143
144         /**
145          * Unimplemented method (read only map).
146          */
147         public void clear () {
148                 assert_not_reached ();
149         }
150
151         /**
152          * {@inheritDoc}
153          */
154         public Gee.MapIterator<K,V> map_iterator () {
155                 return new MapIterator<K,V> (_map.map_iterator ());
156         }
157
158         /**
159          * Unimplemented method (read only map).
160          */
161         public void set_all (Map<K,V> map) {
162                 assert_not_reached ();
163         }
164
165         /**
166          * Unimplemented method (read only map).
167          */
168         public bool unset_all (Map<K,V> map) {
169                 assert_not_reached ();
170         }
171
172         /**
173          * Unimplemented method (read only map).
174          */
175         public bool remove_all (Map<K,V> map) {
176                 assert_not_reached ();
177         }
178
179         /**
180          * {@inheritDoc}
181          */
182         public bool has_all (Map<K,V> map) {
183                 return _map.has_all (map);
184         }
185
186         /**
187          * {@inheritDoc}
188          */
189         public bool contains_all (Map<K,V> map) {
190                 return _map.has_all (map);
191         }
192
193         public virtual Map<K,V> read_only_view {
194                 owned get { return this; }
195         }
196
197         /**
198          * {@inheritDoc}
199          */
200         public Type key_type {
201                 get { return typeof (K); }
202         }
203
204         /**
205          * {@inheritDoc}
206          */
207         public Type value_type {
208                 get { return typeof (V); }
209         }
210
211         /**
212          * {@inheritDoc}
213          */
214         public Type element_type {
215                 get { return typeof (Map.Entry<K,V>); }
216         }
217
218         /**
219          * {@inheritDoc}
220          */
221         public Iterator<Map.Entry<K,V>> iterator () {
222                 return entries.iterator ();
223         }
224
225         /**
226          * {@inheritDoc}
227          */
228         public bool foreach (ForallFunc<Map.Entry<K, V>> f) {
229                 return _map.foreach (f);
230         }
231
232         /**
233          * {@inheritDoc}
234          */
235         public Iterator<A> stream<A> (owned StreamFunc<Map.Entry<K, V>, A> f) {
236                 return _map.stream<A> ((owned) f);
237         }
238
239         public Iterator<Map.Entry<K, V>> filter (owned Predicate<Map.Entry<K, V>> f) {
240                 return _map.filter ((owned)f);
241         }
242
243         public Iterator<Map.Entry<K, V>> chop (int offset, int length = -1) {
244                 return _map.chop (offset, length);
245         }
246
247         protected class MapIterator<K,V> : Object, Gee.MapIterator<K,V> {
248                 protected Gee.MapIterator<K,V> _iter;
249
250                 public MapIterator (Gee.MapIterator<K,V> iterator) {
251                         _iter = iterator;
252                 }
253
254                 public bool next () {
255                         return _iter.next ();
256                 }
257
258                 public bool has_next () {
259                         return _iter.has_next ();
260                 }
261
262                 public K get_key () {
263                         return _iter.get_key ();
264                 }
265
266                 public V get_value () {
267                         return _iter.get_value ();
268                 }
269
270                 public void set_value (V value) {
271                         assert_not_reached ();
272                 }
273
274                 public void unset () {
275                         assert_not_reached ();
276                 }
277                 
278                 public bool read_only {
279                         get {
280                                 return true;
281                         }
282                 }
283                 
284                 public bool mutable {
285                         get {
286                                 return false;
287                         }
288                 }
289                 
290                 public bool valid {
291                         get {
292                                 return _iter.valid;
293                         }
294                 }
295         }
296 }
297