1
|
|
|
<?php |
2
|
|
|
namespace GenericCollections\Traits; |
3
|
|
|
|
4
|
|
|
use GenericCollections\Exceptions\ContainerDoesNotAllowNullException; |
5
|
|
|
use GenericCollections\Exceptions\ContainerIsEmptyException; |
6
|
|
|
use GenericCollections\Exceptions\ContainerNotUniqueMemberException; |
7
|
|
|
use GenericCollections\Exceptions\InvalidElementTypeException; |
8
|
|
|
use GenericCollections\Internal\DoubleLinkedList; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This trait include all deque standard methods to queue, stack and deque |
12
|
|
|
* |
13
|
|
|
* @property DoubleLinkedList $storage |
14
|
|
|
* |
15
|
|
|
* @package GenericCollections\Traits |
16
|
|
|
*/ |
17
|
|
|
trait DequeCommonMethods |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* This function must return the container internal name, like 'deque', 'queue' or 'stack' |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
abstract protected function containerInternalName(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @see \GenericCollections\Interfaces\BaseOptionsInterface::optionAllowNullMembers |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
abstract public function optionAllowNullMembers(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @see \GenericCollections\Interfaces\BaseOptionsInterface::optionUniqueValues |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
abstract public function optionUniqueValues(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @see \GenericCollections\Interfaces\BaseOptionsInterface::optionComparisonIsIdentical |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
abstract public function optionComparisonIsIdentical(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @see \GenericCollections\Interfaces\BaseCollectionInterface::checkElementType |
45
|
|
|
* @param mixed $element |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
abstract public function checkElementType($element); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @see \GenericCollections\Interfaces\BaseCollectionInterface::getElementType |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
abstract public function getElementType(); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @see \GenericCollections\Internal\StorageInterface::isEmpty |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
abstract public function isEmpty(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Throw an exception when add a non valid element |
64
|
|
|
* |
65
|
|
|
* @param $element |
66
|
|
|
*/ |
67
|
72 |
|
private function checkElementAdd($element) |
68
|
|
|
{ |
69
|
|
|
// always throw an exception if element is null and container does not allow nulls |
70
|
72 |
|
if (! $this->optionAllowNullMembers() && is_null($element)) { |
71
|
8 |
|
throw new ContainerDoesNotAllowNullException( |
72
|
8 |
|
$this->containerInternalName(), |
73
|
|
|
get_class($this) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
// always throw an exception if element is not the correct type |
77
|
64 |
|
if (! $this->checkElementType($element)) { |
78
|
6 |
|
throw new InvalidElementTypeException( |
79
|
6 |
|
$this->containerInternalName(), |
80
|
6 |
|
$this->getElementType(), |
81
|
|
|
get_class($this) |
82
|
|
|
); |
83
|
|
|
} |
84
|
58 |
|
if ($this->optionUniqueValues() && $this->contains($element)) { |
85
|
6 |
|
throw new ContainerNotUniqueMemberException( |
86
|
6 |
|
$this->containerInternalName(), |
87
|
|
|
get_class($this) |
88
|
|
|
); |
89
|
|
|
} |
90
|
58 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Throw an exception when add a non valid element |
94
|
|
|
* |
95
|
|
|
* @param $element |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
20 |
|
private function checkElementOffer($element) |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
20 |
|
$this->checkElementAdd($element); |
102
|
9 |
|
} catch (ContainerNotUniqueMemberException $ex) { |
103
|
3 |
|
return false; |
104
|
|
|
} |
105
|
11 |
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
12 |
|
public function addFirst($element) |
109
|
|
|
{ |
110
|
12 |
|
$this->checkElementAdd($element); |
111
|
8 |
|
$this->storage->unshift($element); |
112
|
8 |
|
} |
113
|
|
|
|
114
|
52 |
|
public function addLast($element) |
115
|
|
|
{ |
116
|
52 |
|
$this->checkElementAdd($element); |
117
|
47 |
|
$this->storage->push($element); |
118
|
47 |
|
} |
119
|
|
|
|
120
|
7 |
|
public function offerFirst($element) |
121
|
|
|
{ |
122
|
7 |
|
if (! $this->checkElementOffer($element)) { |
123
|
1 |
|
return false; |
124
|
|
|
} |
125
|
4 |
|
$this->storage->unshift($element); |
126
|
4 |
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
13 |
|
public function offerLast($element) |
130
|
|
|
{ |
131
|
13 |
|
if (! $this->checkElementOffer($element)) { |
132
|
2 |
|
return false; |
133
|
|
|
} |
134
|
7 |
|
$this->storage->push($element); |
135
|
7 |
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
8 |
|
public function getFirst() |
139
|
|
|
{ |
140
|
8 |
|
if ($this->isEmpty()) { |
141
|
2 |
|
throw new ContainerIsEmptyException($this->containerInternalName(), 'get'); |
142
|
|
|
} |
143
|
6 |
|
return $this->storage->bottom(); |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
public function peekFirst() |
147
|
|
|
{ |
148
|
2 |
|
if ($this->isEmpty()) { |
149
|
2 |
|
return null; |
150
|
|
|
} |
151
|
2 |
|
return $this->storage->bottom(); |
152
|
|
|
} |
153
|
|
|
|
154
|
6 |
|
public function removeFirst() |
155
|
|
|
{ |
156
|
6 |
|
if ($this->isEmpty()) { |
157
|
2 |
|
throw new ContainerIsEmptyException($this->containerInternalName(), 'remove'); |
158
|
|
|
} |
159
|
4 |
|
return $this->storage->shift(); |
160
|
|
|
} |
161
|
|
|
|
162
|
4 |
|
public function pollFirst() |
163
|
|
|
{ |
164
|
4 |
|
if ($this->isEmpty()) { |
165
|
2 |
|
return null; |
166
|
|
|
} |
167
|
4 |
|
return $this->storage->shift(); |
168
|
|
|
} |
169
|
|
|
|
170
|
9 |
|
public function contains($element) |
171
|
|
|
{ |
172
|
9 |
|
return $this->storage->contains($element); |
173
|
|
|
} |
174
|
|
|
|
175
|
81 |
|
protected function createStorageObject() |
176
|
|
|
{ |
177
|
81 |
|
$this->storage = new DoubleLinkedList(); |
178
|
81 |
|
if (! $this->optionComparisonIsIdentical()) { |
179
|
2 |
|
$this->storage->strictComparisonOff(); |
180
|
|
|
} |
181
|
81 |
|
} |
182
|
|
|
} |
183
|
|
|
|