1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Collections\IdentityDictionary |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/collections |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Collections; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Lang\NullPointerException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This class is the implementation of a Dictionary. |
27
|
|
|
* |
28
|
|
|
* A dictionary uses objects as keys instead of integers |
29
|
|
|
* like a HashMap. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/appserver-io/collections |
35
|
|
|
* @link http://www.appserver.io |
36
|
|
|
*/ |
37
|
|
|
class IdentityDictionary extends Dictionary |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This method returns the element that has the passed |
42
|
|
|
* key as a reference (has to be an object) from the |
43
|
|
|
* Dictionary. |
44
|
|
|
* |
45
|
|
|
* @param object $key Holds the key to the key of the element to return |
46
|
|
|
* |
47
|
|
|
* @return mixed The requested value |
48
|
|
|
* @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
49
|
|
|
* @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key OR value are NULL |
50
|
|
|
* @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Dictionary |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function get($key) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
if (is_null($key)) { |
55
|
|
|
throw new NullPointerException('Passed key is null'); |
56
|
|
|
} |
57
|
|
|
if (!is_object($key)) { |
58
|
|
|
throw new InvalidKeyException('Passed key has to be an object'); |
59
|
|
|
} |
60
|
|
|
// run over all keys and check if one is equal to the passed one |
61
|
|
|
foreach ($this->keys as $id => $value) { |
62
|
|
|
// if the actual is equal to the passed key .. |
63
|
|
|
if ($key === $value) { |
64
|
|
|
// return the item with the passed key |
65
|
|
|
if (array_key_exists($id, $this->items)) { |
66
|
|
|
return $this->items[$id]; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
// if no value is found throw an exception |
71
|
|
|
throw new IndexOutOfBoundsException('Index out of bounds'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* This method checks if the element that has the passed |
76
|
|
|
* key as a reference (has to be an object) exists in |
77
|
|
|
* the Dictionary. |
78
|
|
|
* |
79
|
|
|
* @param object $key Holds the reference to the key of the element that should exists in the Dictionary |
80
|
|
|
* |
81
|
|
|
* @return boolean Returns TRUE if an element with the passed key exists in the Dictionary |
82
|
|
|
* @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
83
|
|
|
* @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function exists($key) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
if (is_null($key)) { |
88
|
|
|
throw new NullPointerException('Passed key is null'); |
89
|
|
|
} |
90
|
|
|
if (! is_object($key)) { |
91
|
|
|
throw new InvalidKeyException('Passed key has to be an object'); |
92
|
|
|
} |
93
|
|
|
// run over all keys and check if one is equal to the passed one |
94
|
|
|
foreach ($this->keys as $id => $value) { |
95
|
|
|
// if the actual is equal to the passed key .. |
96
|
|
|
if ($key === $value) { |
97
|
|
|
// return TRUE if the key is found |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
// return FALSE if the key is not found |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* This method removes the element that has the passed |
107
|
|
|
* key as a reference (has to be an object) from the |
108
|
|
|
* Dictionary. |
109
|
|
|
* |
110
|
|
|
* @param object $key Holds the reference of the key of the element to remove |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
* @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
114
|
|
|
* @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
115
|
|
|
* @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Dictionary |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function remove($key) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
if (is_null($key)) { |
120
|
|
|
throw new NullPointerException('Passed key is null'); |
121
|
|
|
} |
122
|
|
|
if (! is_object($key)) { |
123
|
|
|
throw new InvalidKeyException('Passed key has to be an object'); |
124
|
|
|
} |
125
|
|
|
// run over all keys and check if one is a reference of the passed one |
126
|
|
|
foreach ($this->keys as $id => $value) { |
127
|
|
|
// if the actual is equal to the passed key .. |
128
|
|
|
if ($key === $value) { |
129
|
|
|
// unset the elements |
130
|
|
|
unset($this->items[$id]); |
131
|
|
|
unset($this->keys[$id]); |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
// throw an exception if key is not found in internal array |
136
|
|
|
throw new IndexOutOfBoundsException('Index out of bounds'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.