849612e73f0e5b49139e744b8fc1968c56886796
[platform/upstream/libgee.git] / gee / abstractmap.vala
1 /* abstractmap.vala
2  *
3  * Copyright (C) 2007  Jürg Billeter
4  * Copyright (C) 2009  Didier Villevalois
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19  *
20  * Author:
21  *      Tomaž Vajngerl <quikee@gmail.com>
22  */
23
24 /**
25  * Skeletal implementation of the {@link Map} interface.
26  *
27  * Contains common code shared by all map implementations.
28  *
29  * @see HashMap
30  * @see TreeMap
31  */
32 public abstract class Gee.AbstractMap<K,V> : Object, Traversable<Map.Entry<K,V>>, Iterable<Map.Entry<K,V>>, Map<K,V> {
33
34         /**
35          * {@inheritDoc}
36          */
37         public abstract int size { get; }
38         
39         /**
40          * {@inheritDoc}
41          */
42         public abstract bool read_only { get; }
43
44         /**
45          * {@inheritDoc}
46          */
47         public abstract Set<K> keys { owned get; }
48
49         /**
50          * {@inheritDoc}
51          */
52         public abstract Collection<V> values { owned get; }
53
54         /**
55          * {@inheritDoc}
56          */
57         public abstract Set<Map.Entry<K,V>> entries { owned get; }
58
59         /**
60          * {@inheritDoc}
61          */
62         public abstract bool has_key (K key);
63
64         /**
65          * {@inheritDoc}
66          */
67         public abstract bool has (K key, V value);
68
69         /**
70          * {@inheritDoc}
71          */
72         public abstract new V? get (K key);
73
74         /**
75          * {@inheritDoc}
76          */
77         public abstract new void set (K key, V value);
78
79         /**
80          * {@inheritDoc}
81          */
82         public abstract bool unset (K key, out V? value = null);
83
84         /**
85          * {@inheritDoc}
86          */
87         public abstract MapIterator<K,V> map_iterator ();
88
89         /**
90          * {@inheritDoc}
91          */
92         public abstract void clear ();
93
94         private weak Map<K,V> _read_only_view;
95
96         /**
97          * {@inheritDoc}
98          */
99         public virtual Map<K,V> read_only_view {
100                 owned get {
101                         Map<K,V> instance = _read_only_view;
102                         if (_read_only_view == null) {
103                                 instance = new ReadOnlyMap<K,V> (this);
104                                 _read_only_view = instance;
105                                 instance.add_weak_pointer ((void**) (&_read_only_view));
106                         }
107                         return instance;
108                 }
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         public Iterator<Map.Entry<K,V>> iterator () {
115                 return entries.iterator ();
116         }
117
118         /**
119          * {@inheritDoc}
120          */
121         public virtual void foreach (ForallFunc<Map.Entry<K,V>> f) {
122                 iterator ().foreach (f);
123         }
124
125         /**
126          * {@inheritDoc}
127          */
128         public virtual Iterator<A> stream<A> (owned StreamFunc<Map.Entry<K,V>, A> f) {
129                 return iterator ().stream<A> ((owned) f);
130         }
131
132         // Future-proofing
133         internal new virtual void reserved0() {}
134         internal new virtual void reserved1() {}
135         internal new virtual void reserved2() {}
136         internal new virtual void reserved3() {}
137         internal new virtual void reserved4() {}
138         internal new virtual void reserved5() {}
139         internal new virtual void reserved6() {}
140         internal new virtual void reserved7() {}
141         internal new virtual void reserved8() {}
142         internal new virtual void reserved9() {}
143 }