changes: TINF-175 TZPC-4722
[platform/upstream/libgee.git] / gee / readonlylist.vala
1 /* readonlylist.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 List} collections.
27  *
28  * This class decorates any class which implements the {@link List}
29  * interface by making it read only. Any method which normally modify data will
30  * throw an error.
31  *
32  * @see List
33  */
34 internal class Gee.ReadOnlyList<G> : Gee.ReadOnlyCollection<G>, List<G> {
35
36         /**
37          * Constructs a read-only list that mirrors the content of the specified
38          * list.
39          *
40          * @param list the list to decorate.
41          */
42         public ReadOnlyList (List<G> list) {
43                 base (list);
44         }
45
46         /**
47          * {@inheritDoc}
48          */
49         public ListIterator<G> list_iterator () {
50                 return new Iterator<G> (((Gee.List<G>) _collection).list_iterator ());
51         }
52
53         /**
54          * {@inheritDoc}
55          */
56         public int index_of (G item) {
57                 return ((Gee.List<G>) _collection).index_of (item);
58         }
59
60         /**
61          * Unimplemented method (read only list).
62          */
63         public void insert (int index, G item) {
64                 assert_not_reached ();
65         }
66
67         /**
68          * Unimplemented method (read only list).
69          */
70         public G remove_at (int index) {
71                 assert_not_reached ();
72         }
73
74         /**
75          * {@inheritDoc}
76          */
77         public new G? get (int index) {
78                 return ((Gee.List<G>) _collection).get (index);
79         }
80
81         /**
82          * Unimplemented method (read only list).
83          */
84         public new void set (int index, G o) {
85                 assert_not_reached ();
86         }
87
88         /**
89          * {@inheritDoc}
90          */
91         public List<G>? slice (int start, int stop) {
92                 return ((Gee.List<G>) _collection).slice (start, stop);
93         }
94
95         /**
96          * {@inheritDoc}
97          */
98         public G? first () {
99                 return ((Gee.List<G>) _collection).first ();
100         }
101
102         /**
103          * {@inheritDoc}
104          */
105         public G? last () {
106                 return ((Gee.List<G>) _collection).last ();
107         }
108
109         /**
110          * Unimplemented method (read only list).
111          */
112         public void insert_all (int index, Collection<G> collection) {
113                 assert_not_reached ();
114         }
115
116         /**
117          * {@inheritDoc}
118          */
119         public void sort (owned CompareDataFunc<G>? compare = null) {
120                 assert_not_reached ();
121         }
122
123         /**
124          * {@inheritDoc}
125          */
126         public virtual new List<G> read_only_view {
127                 owned get { return this; }
128         }
129
130
131         protected class Iterator<G> : ReadOnlyCollection.Iterator<G>, ListIterator<G> {
132                 public Iterator (ListIterator<G> iterator) {
133                         base (iterator);
134                 }
135
136                 public new void set (G item) {
137                         assert_not_reached ();
138                 }
139
140                 public void add (G item) {
141                         assert_not_reached ();
142                 }
143
144                 public int index () {
145                         return ((ListIterator<G>) _iter).index ();
146                 }
147         }
148 }
149