1 /* readonlycollection.vala
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 Collection} collections.
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
34 internal class Gee.ReadOnlyCollection<G> : Object, Traversable<G>, Iterable<G>, Collection<G> {
40 get { return _collection.size; }
46 public bool is_empty {
47 get { return _collection.is_empty; }
53 public bool read_only {
57 protected Collection<G> _collection;
60 * Constructs a read-only collection that mirrors the content of the
61 * specified collection.
63 * @param collection the collection to decorate.
65 public ReadOnlyCollection (Collection<G> collection) {
66 this._collection = collection;
72 public void foreach (ForallFunc<G> f) {
73 _collection.foreach (f);
79 public Gee.Iterator<A> stream<A> (owned StreamFunc<A> f) {
80 return _collection.stream<A> ((owned)f);
86 public Gee.Iterator<G> filter (owned Predicate<G> f) {
87 return _collection.filter ((owned)f);
93 public Gee.Iterator<G> chop (int offset, int length = -1) {
94 return _collection.chop (offset, length);
100 public Type element_type {
101 get { return typeof (G); }
107 public Gee.Iterator<G> iterator () {
108 return new Iterator<G> (_collection.iterator ());
114 public bool contains (G item) {
115 return _collection.contains (item);
119 * Unimplemented method (read only collection).
121 public bool add (G item) {
122 assert_not_reached ();
126 * Unimplemented method (read only collection).
128 public bool remove (G item) {
129 assert_not_reached ();
133 * Unimplemented method (read only collection).
135 public void clear () {
136 assert_not_reached ();
140 * Unimplemented method (read only collection).
142 public bool add_all (Collection<G> collection) {
143 assert_not_reached ();
149 public bool contains_all (Collection<G> collection) {
150 return _collection.contains_all (collection);
154 * Unimplemented method (read only collection).
156 public bool remove_all (Collection<G> collection) {
157 assert_not_reached ();
161 * Unimplemented method (read only collection).
163 public bool retain_all (Collection<G> collection) {
164 assert_not_reached ();
170 public G[] to_array () {
171 return _collection.to_array ();
174 protected class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G> {
175 protected Gee.Iterator<G> _iter;
177 public Iterator (Gee.Iterator<G> iterator) {
181 public bool next () {
182 return _iter.next ();
185 public bool has_next () {
186 return _iter.has_next ();
189 public new G get () {
193 public void remove () {
194 assert_not_reached ();
203 public bool read_only {
209 public Type element_type {
210 get { return typeof (G); }
213 public void foreach (ForallFunc<G> f) {
217 public Gee.Iterator<A> stream<A> (owned StreamFunc<A, G> f) {
218 return _iter.stream<A> ((owned)f);
221 public Gee.Iterator<G> filter (owned Predicate<G> f) {
222 return _iter.filter ((owned)f);
225 public Gee.Iterator<G> chop (int offset, int length = -1) {
226 return _iter.chop ( offset, length);
230 public virtual Collection<G> read_only_view {
231 owned get { return this; }