Allow early termination of iteration
[platform/upstream/libgee.git] / gee / abstractcollection.vala
1 /* abstractcollection.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  *      Didier 'Ptitjes' Villevalois <ptitjes@free.fr>
22  */
23
24 /**
25  * Skeletal implementation of the {@link Collection} interface.
26  *
27  * Contains common code shared by all collection implementations.
28  *
29  * @see AbstractList
30  * @see AbstractSet
31  * @see AbstractMultiSet
32  */
33 public abstract class Gee.AbstractCollection<G> : Object, Traversable<G>, Iterable<G>, Collection<G> {
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 bool contains (G item);
48
49         /**
50          * {@inheritDoc}
51          */
52         public abstract bool add (G item);
53
54         /**
55          * {@inheritDoc}
56          */
57         public abstract bool remove (G item);
58
59         /**
60          * {@inheritDoc}
61          */
62         public abstract void clear ();
63
64         /**
65          * {@inheritDoc}
66          */
67         public abstract Iterator<G> iterator ();
68
69         public virtual bool foreach (ForallFunc<G> f) {
70                 return iterator ().foreach (f);
71         }
72
73         public virtual Iterator<A> stream<A> (owned StreamFunc<G, A> f) {
74                 return iterator ().stream<A> ((owned) f);
75         }
76
77         private weak Collection<G> _read_only_view;
78
79         /**
80          * {@inheritDoc}
81          */
82         public virtual Collection<G> read_only_view {
83                 owned get {
84                         Collection<G> instance = _read_only_view;
85                         if (_read_only_view == null) {
86                                 instance = new ReadOnlyCollection<G> (this);
87                                 _read_only_view = instance;
88                                 instance.add_weak_pointer ((void**) (&_read_only_view));
89                         }
90                         return instance;
91                 }
92         }
93
94         // Future-proofing
95         internal virtual void reserved0() {}
96         internal virtual void reserved1() {}
97         internal virtual void reserved2() {}
98         internal virtual void reserved3() {}
99         internal virtual void reserved4() {}
100         internal virtual void reserved5() {}
101         internal virtual void reserved6() {}
102         internal virtual void reserved7() {}
103         internal virtual void reserved8() {}
104         internal virtual void reserved9() {}
105 }