Updated 'core'.
[hub.git] / application / hub / main / iterator / pool / tasks / class_TaskPoolIterator.php
1 <?php
2 /**
3  * A TaskPool iterator
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  * @todo                This current implementation is not recommended, use a
11  * @todo                latency-based iteration or similar approaches
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26 class TaskPoolIterator extends BaseIterator implements Iterator, Registerable {
27         /**
28          * Key for the global list index
29          */
30         private $indexKey = 0;
31
32         /**
33          * Protected constructor
34          *
35          * @return      void
36          */
37         protected function __construct () {
38                 // Call parent constructor
39                 parent::__construct(__CLASS__);
40         }
41
42         /**
43          * Creates an instance of this class
44          *
45          * @param       $listInstance           A list of a Listable
46          * @return      $iteratorInstance       An instance a Iterator class
47          */
48         public static final function createTaskPoolIterator (Listable $listInstance) {
49                 // Get new instance
50                 $iteratorInstance = new TaskPoolIterator();
51
52                 // Set the list
53                 $iteratorInstance->setListInstance($listInstance);
54
55                 // Return the prepared instance
56                 return $iteratorInstance;
57         }
58
59         /**
60          * Getter for current value from group or generic
61          *
62          * @return      $current        Current value in iteration
63          * @throws      IndexOutOfBoundsException       If $indexKey is out of bounds
64          */
65         public function current () {
66                 // Default is null
67                 $current = NULL;
68
69                 // Is the entry valid?
70                 if (!$this->valid()) {
71                         // Throw an exception here
72                         throw new IndexOutOfBoundsException($this->key(), self::EXCEPTION_INDEX_OUT_OF_BOUNDS);
73                 } // END - if
74
75                 // Now get the entry
76                 $current = $this->getListInstance()->getEntry($this->key());
77
78                 // Return it
79                 return $current;
80         }
81
82         /**
83          * Getter for key from group or generic
84          *
85          * @return      $indexKey       Current key in iteration
86          */
87         public function key () {
88                 return $this->indexKey;
89         }
90
91         /**
92          * Advances to the next entry
93          *
94          * @return      void
95          */
96         public function next () {
97                 $this->indexKey++;
98         }
99
100         /**
101          * Rewinds to the beginning of the iteration
102          *
103          * @return      void
104          */
105         public function rewind () {
106                 $this->indexKey = 0;
107         }
108
109         /**
110          * Checks whether the current entry is valid (not at the end of the list)
111          *
112          * @return      $isValid        Whether the current entry is there
113          */
114         public function valid () {
115                 // Check for total active peers and if we are not at the end
116                 $isValid = ($this->key() < $this->getListInstance()->count());
117
118                 // Return result
119                 return $isValid;
120         }
121 }
122
123 // [EOF]
124 ?>