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 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class DefaultIterator extends BaseIterator implements Iterator, Registerable {
37 * Key for the global list index
39 private $indexKey = 0;
42 * Protected constructor
46 private function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
52 * Creates an instance of this class
54 * @param $listInstance A list of a Listable
55 * @return $iteratorInstance An instance a Iterator class
57 public static final function createDefaultIterator (Listable $listInstance) {
59 $iteratorInstance = new DefaultIterator();
62 $iteratorInstance->setListInstance($listInstance);
64 // Return the prepared instance
65 return $iteratorInstance;
69 * Getter for current value from group or generic
71 * @return $current Current value in iteration
72 * @throws IndexOutOfBoundsException If $indexKey is out of bounds
74 public function current () {
78 // Is the entry valid?
79 if (!$this->valid()) {
80 // Throw an exception here
81 throw new IndexOutOfBoundsException($this->key(), self::EXCEPTION_INDEX_OUT_OF_BOUNDS);
85 $current = $this->getListInstance()->getEntryByIndex($this->key());
92 * Getter for key from group or generic
94 * @return $indexKey Current key in iteration
96 public function key () {
97 return $this->indexKey;
101 * Advances to the next entry
105 public function next () {
110 * Rewinds to the beginning of the iteration
114 public function rewind () {
119 * Checks whether the current entry is valid (not at the end of the list)
121 * @return $isValid Whether the current entry is there
123 public function valid () {
124 // Check for total active peers and if we are not at the end
125 $isValid = ($this->key() < $this->getListInstance()->count());