1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Collections\TreeMap |
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\Strng; |
24
|
|
|
use AppserverIo\Lang\Integer; |
25
|
|
|
use AppserverIo\Lang\Flt; |
26
|
|
|
use AppserverIo\Lang\Boolean; |
27
|
|
|
use AppserverIo\Lang\ClassCastException; |
28
|
|
|
use AppserverIo\Lang\NullPointerException; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This class is the implementation of a sorted HashMap. |
32
|
|
|
* |
33
|
|
|
* @author Tim Wagner <[email protected]> |
34
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
36
|
|
|
* @link https://github.com/appserver-io/collections |
37
|
|
|
* @link http://www.appserver.io |
38
|
|
|
*/ |
39
|
|
|
class TreeMap extends AbstractMap implements SortedMapInterface |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Holds the comparator to sort the internal array. |
44
|
|
|
* |
45
|
|
|
* @var \AppserverIo\Collections\ComparatorInterface |
46
|
|
|
*/ |
47
|
|
|
protected $comparator = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Standard constructor that adds the array passed |
51
|
|
|
* as parameter to the internal member variable. |
52
|
|
|
* |
53
|
|
|
* Be careful when using a comparator, because sorting |
54
|
|
|
* a large map can take much processor time and memory. |
55
|
|
|
* |
56
|
|
|
* @param \AppserverIo\Collections\ComparatorInterface $comparator Holds the comparator to use |
57
|
|
|
* @param array $items An array to initialize the TreeMap |
58
|
|
|
* |
59
|
|
|
* @throws \AppserverIo\Lang\ClassCastException Is thrown if the parameter items is not of type array |
60
|
|
|
*/ |
61
|
7 |
View Code Duplication |
public function __construct(ComparatorInterface $comparator = null, $items = array()) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
// parent constructor to ensure property preset |
64
|
7 |
|
parent::__construct(); |
65
|
|
|
|
66
|
|
|
// set the comparator |
67
|
7 |
|
$this->comparator = $comparator; |
68
|
|
|
// check if NULL is passed, is yes, to nothing |
69
|
7 |
|
if (is_null($items)) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
// check if an array is passed |
73
|
7 |
|
if (is_array($items)) { |
74
|
|
|
// initialize the TreeMap with the values of the passed array |
75
|
7 |
|
foreach ($items as $key => $item) { |
76
|
|
|
$this->add($key, $item); |
77
|
7 |
|
} |
78
|
7 |
|
return; |
79
|
|
|
} |
80
|
|
|
// if not a array is passed throw an exception |
81
|
|
|
throw new ClassCastException('Passed object is not an array'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* This method adds the passed object with the passed key |
86
|
|
|
* to the TreeMap. |
87
|
|
|
* |
88
|
|
|
* @param mixed $key The key to add the passed value under |
89
|
|
|
* @param mixed $object The object to add to the TreeMap |
90
|
|
|
* |
91
|
|
|
* @return \AppserverIo\Collections\TreeMap |
92
|
|
|
* @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an primitive datatype |
93
|
|
|
* @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is null or not a flat datatype like Integer, Strng, Double or Boolean |
94
|
|
|
*/ |
95
|
5 |
|
public function add($key, $object) |
96
|
|
|
{ |
97
|
5 |
|
if (is_null($key)) { |
98
|
|
|
throw new NullPointerException('Passed key is null'); |
99
|
|
|
} |
100
|
|
|
// check if a primitive datatype is passed |
101
|
5 |
View Code Duplication |
if (is_integer($key) || is_string($key) || is_double($key) || is_bool($key)) { |
|
|
|
|
102
|
|
|
// add the passed object to the internal array |
103
|
5 |
|
$this->items[$key] = $object; |
104
|
|
|
// sort the instance |
105
|
5 |
|
$this->sort(); |
106
|
|
|
// return the instance |
107
|
5 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
// check if an object is passed |
110
|
|
View Code Duplication |
if (is_object($key)) { |
|
|
|
|
111
|
|
|
if ($key instanceof Strng) { |
112
|
|
|
$newKey = $key->stringValue(); |
113
|
|
|
} elseif ($key instanceof Flt) { |
114
|
|
|
$newKey = $key->floatValue(); |
115
|
|
|
} elseif ($key instanceof Integer) { |
116
|
|
|
$newKey = $key->intValue(); |
117
|
|
|
} elseif ($key instanceof Boolean) { |
118
|
|
|
$newKey = $key->booleanValue(); |
119
|
|
|
} elseif (method_exists($key, '__toString')) { |
120
|
|
|
$newKey = $key->__toString(); |
121
|
|
|
} else { |
122
|
|
|
throw new InvalidKeyException('Passed key has to be a primitive datatype or has to implement the __toString() method'); |
123
|
|
|
} |
124
|
|
|
// add the passed object to the internal array |
125
|
|
|
$this->items[$newKey] = $object; |
126
|
|
|
// sort the instance |
127
|
|
|
$this->sort(); |
128
|
|
|
// return the instance |
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
throw new InvalidKeyException('Passed key has to be a primitive datatype or has to implement the __toString() method'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Sorts the instance with the given comparator |
136
|
|
|
* or the PHP ksort() function. |
137
|
|
|
* |
138
|
|
|
* @return boolean|null Returns TRUE if the instance was sorted successfully |
139
|
|
|
*/ |
140
|
5 |
|
protected function sort() |
141
|
|
|
{ |
142
|
|
|
// if no comparator is passed sort the internal array |
143
|
|
|
// by its keys, else use the comparator |
144
|
5 |
|
if ($this->comparator == null) { |
145
|
4 |
|
return ksort($this->items); |
146
|
|
|
} else { |
147
|
1 |
|
return CollectionUtils::sort($this, $this->comparator); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* This method returns a new TreeMap initialized with the |
153
|
|
|
* passed array. |
154
|
|
|
* |
155
|
|
|
* @param array $array Holds the array to initialize the new TreeMap |
156
|
|
|
* |
157
|
|
|
* @return \AppserverIo\Collections\TreeMap Returns a TreeMap initialized with the passed array |
158
|
|
|
* @throws \AppserverIo\Lang\ClassCastException Is thrown if the passed object is not an array |
159
|
|
|
*/ |
160
|
|
|
public static function fromArray($array) |
161
|
|
|
{ |
162
|
|
|
// check if the passed object is an array and set it |
163
|
|
|
if (is_array($array)) { |
164
|
|
|
return new TreeMap(null, $array); |
165
|
|
|
} |
166
|
|
|
// throw an exception if the passed object is not an array |
167
|
|
|
throw new ClassCastException('Passed object is not an array'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* This method returns the comparator passed |
172
|
|
|
* with the constructor. |
173
|
|
|
* |
174
|
|
|
* @return \AppserverIo\Collections\ComparatorInterface Holds the comparator passed with the constructor |
175
|
|
|
* @see \AppserverIo\Collections\SortedMap::comparator() |
176
|
|
|
*/ |
177
|
|
|
public function comparator() |
178
|
|
|
{ |
179
|
|
|
return $this->comparator; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
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.