b907f8b02a00cf7fec00672e375aca29be1e039f
[yaffs-website] / vendor / doctrine / collections / lib / Doctrine / Common / Collections / AbstractLazyCollection.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\Common\Collections;
21
22 use Closure;
23
24 /**
25  * Lazy collection that is backed by a concrete collection
26  *
27  * @author MichaĆ«l Gallego <mic.gallego@gmail.com>
28  * @since  1.2
29  */
30 abstract class AbstractLazyCollection implements Collection
31 {
32     /**
33      * The backed collection to use
34      *
35      * @var Collection
36      */
37     protected $collection;
38
39     /**
40      * @var boolean
41      */
42     protected $initialized = false;
43
44     /**
45      * {@inheritDoc}
46      */
47     public function count()
48     {
49         $this->initialize();
50         return $this->collection->count();
51     }
52
53     /**
54      * {@inheritDoc}
55      */
56     public function add($element)
57     {
58         $this->initialize();
59         return $this->collection->add($element);
60     }
61
62     /**
63      * {@inheritDoc}
64      */
65     public function clear()
66     {
67         $this->initialize();
68         $this->collection->clear();
69     }
70
71     /**
72      * {@inheritDoc}
73      */
74     public function contains($element)
75     {
76         $this->initialize();
77         return $this->collection->contains($element);
78     }
79
80     /**
81      * {@inheritDoc}
82      */
83     public function isEmpty()
84     {
85         $this->initialize();
86         return $this->collection->isEmpty();
87     }
88
89     /**
90      * {@inheritDoc}
91      */
92     public function remove($key)
93     {
94         $this->initialize();
95         return $this->collection->remove($key);
96     }
97
98     /**
99      * {@inheritDoc}
100      */
101     public function removeElement($element)
102     {
103         $this->initialize();
104         return $this->collection->removeElement($element);
105     }
106
107     /**
108      * {@inheritDoc}
109      */
110     public function containsKey($key)
111     {
112         $this->initialize();
113         return $this->collection->containsKey($key);
114     }
115
116     /**
117      * {@inheritDoc}
118      */
119     public function get($key)
120     {
121         $this->initialize();
122         return $this->collection->get($key);
123     }
124
125     /**
126      * {@inheritDoc}
127      */
128     public function getKeys()
129     {
130         $this->initialize();
131         return $this->collection->getKeys();
132     }
133
134     /**
135      * {@inheritDoc}
136      */
137     public function getValues()
138     {
139         $this->initialize();
140         return $this->collection->getValues();
141     }
142
143     /**
144      * {@inheritDoc}
145      */
146     public function set($key, $value)
147     {
148         $this->initialize();
149         $this->collection->set($key, $value);
150     }
151
152     /**
153      * {@inheritDoc}
154      */
155     public function toArray()
156     {
157         $this->initialize();
158         return $this->collection->toArray();
159     }
160
161     /**
162      * {@inheritDoc}
163      */
164     public function first()
165     {
166         $this->initialize();
167         return $this->collection->first();
168     }
169
170     /**
171      * {@inheritDoc}
172      */
173     public function last()
174     {
175         $this->initialize();
176         return $this->collection->last();
177     }
178
179     /**
180      * {@inheritDoc}
181      */
182     public function key()
183     {
184         $this->initialize();
185         return $this->collection->key();
186     }
187
188     /**
189      * {@inheritDoc}
190      */
191     public function current()
192     {
193         $this->initialize();
194         return $this->collection->current();
195     }
196
197     /**
198      * {@inheritDoc}
199      */
200     public function next()
201     {
202         $this->initialize();
203         return $this->collection->next();
204     }
205
206     /**
207      * {@inheritDoc}
208      */
209     public function exists(Closure $p)
210     {
211         $this->initialize();
212         return $this->collection->exists($p);
213     }
214
215     /**
216      * {@inheritDoc}
217      */
218     public function filter(Closure $p)
219     {
220         $this->initialize();
221         return $this->collection->filter($p);
222     }
223
224     /**
225      * {@inheritDoc}
226      */
227     public function forAll(Closure $p)
228     {
229         $this->initialize();
230         return $this->collection->forAll($p);
231     }
232
233     /**
234      * {@inheritDoc}
235      */
236     public function map(Closure $func)
237     {
238         $this->initialize();
239         return $this->collection->map($func);
240     }
241
242     /**
243      * {@inheritDoc}
244      */
245     public function partition(Closure $p)
246     {
247         $this->initialize();
248         return $this->collection->partition($p);
249     }
250
251     /**
252      * {@inheritDoc}
253      */
254     public function indexOf($element)
255     {
256         $this->initialize();
257         return $this->collection->indexOf($element);
258     }
259
260     /**
261      * {@inheritDoc}
262      */
263     public function slice($offset, $length = null)
264     {
265         $this->initialize();
266         return $this->collection->slice($offset, $length);
267     }
268
269     /**
270      * {@inheritDoc}
271      */
272     public function getIterator()
273     {
274         $this->initialize();
275         return $this->collection->getIterator();
276     }
277
278     /**
279      * {@inheritDoc}
280      */
281     public function offsetExists($offset)
282     {
283         $this->initialize();
284         return $this->collection->offsetExists($offset);
285     }
286
287     /**
288      * {@inheritDoc}
289      */
290     public function offsetGet($offset)
291     {
292         $this->initialize();
293         return $this->collection->offsetGet($offset);
294     }
295
296     /**
297      * {@inheritDoc}
298      */
299     public function offsetSet($offset, $value)
300     {
301         $this->initialize();
302         $this->collection->offsetSet($offset, $value);
303     }
304
305     /**
306      * {@inheritDoc}
307      */
308     public function offsetUnset($offset)
309     {
310         $this->initialize();
311         $this->collection->offsetUnset($offset);
312     }
313
314     /**
315      * Is the lazy collection already initialized?
316      *
317      * @return bool
318      */
319     public function isInitialized()
320     {
321         return $this->initialized;
322     }
323
324     /**
325      * Initialize the collection
326      *
327      * @return void
328      */
329     protected function initialize()
330     {
331         if ( ! $this->initialized) {
332             $this->doInitialize();
333             $this->initialized = true;
334         }
335     }
336
337     /**
338      * Do the initialization logic
339      *
340      * @return void
341      */
342     abstract protected function doInitialize();
343 }