fcf76f49fe61eeaeac35caee9828e54fe1a10759
[platform/upstream/libgee.git] / gee / readonlycollection.vala
1 /* readonlycollection.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 Collection} collections.
27  *
28  * This class decorates any class which implements the {@link Collection}
29  * interface by making it read only. Any method which normally modify data will
30  * throw an error.
31  *
32  * @see Collection
33  */
34 internal class Gee.ReadOnlyCollection<G> : Object, Traversable<G>, Iterable<G>, Collection<G> {
35
36         /**
37          * {@inheritDoc}
38          */
39         public int size {
40                 get { return _collection.size; }
41         }
42
43         /**
44          * {@inheritDoc}
45          */
46         public bool is_empty {
47                 get { return _collection.is_empty; }
48         }
49         
50         /**
51          * {@inheritDoc}
52          */
53         public bool read_only {
54                 get { return true; }
55         }
56
57         protected Collection<G> _collection;
58
59         /**
60          * Constructs a read-only collection that mirrors the content of the
61          * specified collection.
62          *
63          * @param collection the collection to decorate.
64          */
65         public ReadOnlyCollection (Collection<G> collection) {
66                 this._collection = collection;
67         }
68
69         /**
70          * {@inheritDoc}
71          */
72         public void foreach (ForallFunc<G> f) {
73                 _collection.foreach (f);
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         public Gee.Iterator<A> stream<A> (owned StreamFunc<A> f) {
80                 return _collection.stream<A> ((owned)f);
81         }
82
83         /**
84          * {@inheritDoc}
85          */
86         public Gee.Iterator<G> filter (owned Predicate<G> f) {
87                 return _collection.filter ((owned)f);
88         }
89
90         /**
91          * {@inheritDoc}
92          */
93         public Gee.Iterator<G> chop (int offset, int length = -1) {
94                 return _collection.chop (offset, length);
95         }
96
97         /**
98          * {@inheritDoc}
99          */
100         public Type element_type {
101                 get { return typeof (G); }
102         }
103
104         /**
105          * {@inheritDoc}
106          */
107         public Gee.Iterator<G> iterator () {
108                 return new Iterator<G> (_collection.iterator ());
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         public bool contains (G item) {
115                 return _collection.contains (item);
116         }
117
118         /**
119          * Unimplemented method (read only collection).
120          */
121         public bool add (G item) {
122                 assert_not_reached ();
123         }
124
125         /**
126          * Unimplemented method (read only collection).
127          */
128         public bool remove (G item) {
129                 assert_not_reached ();
130         }
131
132         /**
133          * Unimplemented method (read only collection).
134          */
135         public void clear () {
136                 assert_not_reached ();
137         }
138
139         /**
140          * Unimplemented method (read only collection).
141          */
142         public bool add_all (Collection<G> collection) {
143                 assert_not_reached ();
144         }
145
146         /**
147          * {@inheritDoc}
148          */
149         public bool contains_all (Collection<G> collection) {
150                 return _collection.contains_all (collection);
151         }
152
153         /**
154          * Unimplemented method (read only collection).
155          */
156         public bool remove_all (Collection<G> collection) {
157                 assert_not_reached ();
158         }
159
160         /**
161          * Unimplemented method (read only collection).
162          */
163         public bool retain_all (Collection<G> collection) {
164                 assert_not_reached ();
165         }
166
167         /**
168          * {@inheritDoc}
169          */
170         public G[] to_array () {
171                 return _collection.to_array ();
172         }
173
174         protected class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G> {
175                 protected Gee.Iterator<G> _iter;
176
177                 public Iterator (Gee.Iterator<G> iterator) {
178                         _iter = iterator;
179                 }
180
181                 public bool next () {
182                         return _iter.next ();
183                 }
184
185                 public bool has_next () {
186                         return _iter.has_next ();
187                 }
188
189                 public new G get () {
190                         return _iter.get ();
191                 }
192
193                 public void remove () {
194                         assert_not_reached ();
195                 }
196
197                 public bool valid {
198                         get {
199                                 return _iter.valid;
200                         }
201                 }
202
203                 public bool read_only {
204                         get {
205                                 return true;
206                         }
207                 }
208
209                 public Type element_type {
210                         get { return typeof (G); }
211                 }
212
213                 public void foreach (ForallFunc<G> f) {
214                         _iter.foreach (f);
215                 }
216
217                 public Gee.Iterator<A> stream<A> (owned StreamFunc<A, G> f) {
218                         return _iter.stream<A> ((owned)f);
219                 }
220
221                 public Gee.Iterator<G> filter (owned Predicate<G> f) {
222                         return _iter.filter ((owned)f);
223                 }
224
225                 public Gee.Iterator<G> chop (int offset, int length = -1) {
226                         return _iter.chop ( offset, length);
227                 }
228         }
229
230         public virtual Collection<G> read_only_view {
231                 owned get { return this; }
232         }
233
234 }
235