5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
10 * @todo This current implementation is not recommended, use a
11 * @todo latency-based iteration or similar approaches
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.
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.
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/>.
26 class TaskPoolIterator extends BaseIterator implements Iterator {
28 * Key for the global list index
30 private $indexKey = 0;
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Creates an instance of this class
45 * @param $listInstance A list of a Listable
46 * @return $iteratorInstance An instance a Iterator class
48 public static final function createTaskPoolIterator (Listable $listInstance) {
50 $iteratorInstance = new TaskPoolIterator();
53 $iteratorInstance->setListInstance($listInstance);
55 // Return the prepared instance
56 return $iteratorInstance;
60 * Getter for current value from group or generic
62 * @return $current Current value in iteration
63 * @throws IndexOutOfBoundsException If $indexKey is out of bounds
65 public function current () {
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);
76 $current = $this->getListInstance()->getEntry($this->key());
83 * Getter for key from group or generic
85 * @return $indexKey Current key in iteration
87 public function key () {
88 return $this->indexKey;
92 * Advances to the next entry
96 public function next () {
101 * Rewinds to the beginning of the iteration
105 public function rewind () {
110 * Checks wether the current entry is valid (not at the end of the list)
112 * @return $isValid Wether the current entry is there
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());