3 * Copyright (C) 2007-2008 Jürg Billeter
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.
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.
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
20 * Jürg Billeter <j@bitron.ch>
26 * Read-only view for {@link Map} collections.
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
34 internal class Gee.ReadOnlyMap<K,V> : Object, Traversable<Map.Entry<K,V>>, Iterable<Map.Entry<K,V>>, Map<K,V> {
40 get { return _map.size; }
46 public bool is_empty {
47 get { return _map.is_empty; }
53 public bool read_only {
69 public Collection<V> values {
78 public Set<Map.Entry<K,V>> entries {
84 protected Map<K,V> _map;
87 * Constructs a read-only map that mirrors the content of the specified map.
89 * @param map the map to decorate.
91 public ReadOnlyMap (Map<K,V> map) {
98 public bool has_key (K key) {
99 return _map.has_key (key);
105 public bool contains (K key) {
106 return _map.has_key (key);
112 public bool has (K key, V value) {
113 return _map.has (key, value);
119 public new V? get (K key) {
120 return _map.get (key);
124 * Unimplemented method (read only map).
126 public new void set (K key, V value) {
127 assert_not_reached ();
131 * Unimplemented method (read only map).
133 public bool unset (K key, out V? value = null) {
134 assert_not_reached ();
138 * Unimplemented method (read only map).
140 public bool remove (K key, out V? value = null) {
141 assert_not_reached ();
145 * Unimplemented method (read only map).
147 public void clear () {
148 assert_not_reached ();
154 public Gee.MapIterator<K,V> map_iterator () {
155 return new MapIterator<K,V> (_map.map_iterator ());
159 * Unimplemented method (read only map).
161 public void set_all (Map<K,V> map) {
162 assert_not_reached ();
166 * Unimplemented method (read only map).
168 public bool unset_all (Map<K,V> map) {
169 assert_not_reached ();
173 * Unimplemented method (read only map).
175 public bool remove_all (Map<K,V> map) {
176 assert_not_reached ();
182 public bool has_all (Map<K,V> map) {
183 return _map.has_all (map);
189 public bool contains_all (Map<K,V> map) {
190 return _map.has_all (map);
193 public virtual Map<K,V> read_only_view {
194 owned get { return this; }
200 public Type key_type {
201 get { return typeof (K); }
207 public Type value_type {
208 get { return typeof (V); }
214 public Type element_type {
215 get { return typeof (Map.Entry<K,V>); }
221 public Iterator<Map.Entry<K,V>> iterator () {
222 return entries.iterator ();
228 public bool foreach (ForallFunc<Map.Entry<K, V>> f) {
229 return _map.foreach (f);
235 public Iterator<A> stream<A> (owned StreamFunc<Map.Entry<K, V>, A> f) {
236 return _map.stream<A> ((owned) f);
239 public Iterator<Map.Entry<K, V>> filter (owned Predicate<Map.Entry<K, V>> f) {
240 return _map.filter ((owned)f);
243 public Iterator<Map.Entry<K, V>> chop (int offset, int length = -1) {
244 return _map.chop (offset, length);
247 protected class MapIterator<K,V> : Object, Gee.MapIterator<K,V> {
248 protected Gee.MapIterator<K,V> _iter;
250 public MapIterator (Gee.MapIterator<K,V> iterator) {
254 public bool next () {
255 return _iter.next ();
258 public bool has_next () {
259 return _iter.has_next ();
262 public K get_key () {
263 return _iter.get_key ();
266 public V get_value () {
267 return _iter.get_value ();
270 public void set_value (V value) {
271 assert_not_reached ();
274 public void unset () {
275 assert_not_reached ();
278 public bool read_only {
284 public bool mutable {