1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Maxim Sokolovsky |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace WS\Utils\Collections; |
7
|
|
|
|
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
class ImmutableList implements ListSequence |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private $decoratedList; |
14
|
|
|
|
15
|
9 |
|
public function __construct(?array $elements = null) |
16
|
|
|
{ |
17
|
9 |
|
$this->decoratedList = new ArrayList($elements); |
18
|
9 |
|
} |
19
|
|
|
|
20
|
1 |
|
public static function fromCollection(Collection $collection): self |
21
|
|
|
{ |
22
|
1 |
|
return new static($collection->toArray()); |
23
|
|
|
} |
24
|
|
|
|
25
|
1 |
|
public static function of(...$elements): self |
26
|
|
|
{ |
27
|
1 |
|
return new static($elements ?: null); |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public function stream(): Stream |
31
|
|
|
{ |
32
|
1 |
|
return $this->decoratedList->stream(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function parallelStream($workersPool = null): Stream |
36
|
|
|
{ |
37
|
|
|
return $this->decoratedList->parallelStream($workersPool); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function remove($element): bool |
41
|
|
|
{ |
42
|
1 |
|
throw $this->createBlockingException(); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function get(int $index) |
46
|
|
|
{ |
47
|
1 |
|
return $this->decoratedList->get($index); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function set($element, int $index) |
51
|
|
|
{ |
52
|
1 |
|
throw $this->createBlockingException(); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function indexOf($element): ?int |
56
|
|
|
{ |
57
|
1 |
|
return $this->decoratedList->indexOf($element); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function lastIndexOf($element): ?int |
61
|
|
|
{ |
62
|
1 |
|
return $this->decoratedList->lastIndexOf($element); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function removeAt(int $index) |
66
|
|
|
{ |
67
|
1 |
|
throw $this->createBlockingException(); |
68
|
|
|
} |
69
|
|
|
|
70
|
7 |
|
private function createBlockingException(): RuntimeException |
71
|
|
|
{ |
72
|
7 |
|
return new RuntimeException('Is immutable list. Everything modifier call is prohibited'); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function add($element): bool |
76
|
|
|
{ |
77
|
1 |
|
throw $this->createBlockingException(); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function addAll(iterable $elements): bool |
81
|
|
|
{ |
82
|
1 |
|
throw $this->createBlockingException(); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function merge(Collection $collection): bool |
86
|
|
|
{ |
87
|
1 |
|
throw $this->createBlockingException(); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function clear(): void |
91
|
|
|
{ |
92
|
1 |
|
throw $this->createBlockingException(); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function contains($element): bool |
96
|
|
|
{ |
97
|
1 |
|
return $this->decoratedList->contains($element); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function equals(Collection $collection): bool |
101
|
|
|
{ |
102
|
1 |
|
return $this->decoratedList->equals($collection); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
public function size(): int |
106
|
|
|
{ |
107
|
2 |
|
return $this->decoratedList->size(); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function isEmpty(): bool |
111
|
|
|
{ |
112
|
1 |
|
return $this->decoratedList->isEmpty(); |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
public function toArray(): array |
116
|
|
|
{ |
117
|
2 |
|
return $this->decoratedList->toArray(); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
public function copy(): Collection |
121
|
|
|
{ |
122
|
1 |
|
return $this->decoratedList->copy(); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
public function getIterator() |
126
|
|
|
{ |
127
|
1 |
|
return $this->decoratedList->getIterator(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|