1
|
|
|
<?php |
2
|
|
|
namespace Jmw\Collection\Lists; |
3
|
|
|
|
4
|
|
|
use Jmw\Collection\IteratorInterface; |
5
|
|
|
use Jmw\Collection\Exception\IllegalStateException; |
6
|
|
|
use Jmw\Collection\Exception\NoSuchElementException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The ListIterator is used for iterating over lists |
10
|
|
|
* @author john |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class ListIterator implements IteratorInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The current iteration index |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
protected $index; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The underlying ListInterface object |
23
|
|
|
* @var ListInterface |
24
|
|
|
*/ |
25
|
|
|
protected $list; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* canRemove determine whether or not we can remove an item. |
29
|
|
|
* The list can't remove things until after next has been called |
30
|
|
|
* @var boolean |
31
|
|
|
*/ |
32
|
|
|
protected $canRemove; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructs a new ListIterator at the specified start position |
36
|
|
|
* @param ListInterface $list |
37
|
|
|
* @param int $start |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ListInterface $list, $start = 0) |
40
|
|
|
{ |
41
|
|
|
$this->index = $start - 1; |
42
|
|
|
|
43
|
|
|
$this->list = $list; |
44
|
|
|
|
45
|
|
|
$this->canRemove = false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns true if this list iterator has more elements when traversing the list in the forward direction. |
50
|
|
|
* @return boolean |
51
|
|
|
*/ |
52
|
|
|
public function hasNext() |
53
|
|
|
{ |
54
|
|
|
return $this->index+1 < $this->list->size(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the next element in the list and advances the cursor position. |
59
|
|
|
* @return multitype |
60
|
|
|
* @throws NoSuchElementException |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function next() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
if($this->hasNext()) |
65
|
|
|
{ |
66
|
|
|
$this->canRemove = true; |
67
|
|
|
return $this->list->get(++$this->index); |
68
|
|
|
} |
69
|
|
|
else |
70
|
|
|
{ |
71
|
|
|
throw new NoSuchElementException("Could not find element at index {$this->index}"); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Removes from the list the last element that was returned by next() |
77
|
|
|
* @return void |
78
|
|
|
* @throws IllegalStateException |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function remove() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
if($this->canRemove) |
83
|
|
|
{ |
84
|
|
|
$this->list->removeAt($this->index--); |
85
|
|
|
$this->canRemove = false; |
86
|
|
|
} |
87
|
|
|
else |
88
|
|
|
{ |
89
|
|
|
throw new IllegalStateException("Cannot remove element unless next has been called"); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.