3 namespace Org\Mxchange\CoreFramework\Iterator;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Iterator\BaseIterator;
7 use Org\Mxchange\CoreFramework\Lists\Listable;
8 use Org\Mxchange\CoreFramework\Registry\Registerable;
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 <<<<<<< HEAD:framework/main/classes/iterator/default/class_DefaultIterator.php
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
22 >>>>>>> Some updates::inc/main/classes/iterator/default/class_DefaultIterator.php
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class DefaultIterator extends BaseIterator implements Iterator, Registerable {
41 * Key for the global list index
43 private $indexKey = 0;
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates an instance of this class
58 * @param $listInstance A list of a Listable
59 * @return $iteratorInstance An instance a Iterator class
61 public static final function createDefaultIterator (Listable $listInstance) {
63 $iteratorInstance = new DefaultIterator();
66 $iteratorInstance->setListInstance($listInstance);
68 // Return the prepared instance
69 return $iteratorInstance;
73 * Getter for current value from group or generic
75 * @return $current Current value in iteration
76 * @throws IndexOutOfBoundsException If $indexKey is out of bounds
78 public function current () {
82 // Is the entry valid?
83 if (!$this->valid()) {
84 // Throw an exception here
85 throw new IndexOutOfBoundsException($this->key(), self::EXCEPTION_INDEX_OUT_OF_BOUNDS);
89 $current = $this->getListInstance()->getEntry($this->key());
96 * Getter for key from group or generic
98 * @return $indexKey Current key in iteration
100 public function key () {
101 return $this->indexKey;
105 * Advances to the next entry
109 public function next () {
114 * Rewinds to the beginning of the iteration
118 public function rewind () {
123 * Checks whether the current entry is valid (not at the end of the list)
125 * @return $isValid Whether the current entry is there
127 public function valid () {
128 // Check for total active peers and if we are not at the end
129 $isValid = ($this->key() < $this->getListInstance()->count());