| @@ 99-136 (lines=38) @@ | ||
| 96 | * @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Collection |
|
| 97 | * @see \AppserverIo\Collections\CollectionInterface::get($key) |
|
| 98 | */ |
|
| 99 | public function get($key) |
|
| 100 | { |
|
| 101 | if (is_null($key)) { |
|
| 102 | throw new NullPointerException('Passed key is null'); |
|
| 103 | } |
|
| 104 | // check if a primitive datatype is passed |
|
| 105 | if (is_integer($key) || is_string($key) || is_double($key) || is_bool($key)) { |
|
| 106 | // return the value for the passed key, if it exists |
|
| 107 | if (isset($this->items[$key])) { |
|
| 108 | return $this->items[$key]; |
|
| 109 | } else { |
|
| 110 | throw new IndexOutOfBoundsException('Index ' . $key . ' out of bounds'); |
|
| 111 | } |
|
| 112 | } |
|
| 113 | // check if an object is passed |
|
| 114 | if (is_object($key)) { |
|
| 115 | if ($key instanceof Strng) { |
|
| 116 | $newKey = $key->stringValue(); |
|
| 117 | } elseif ($key instanceof Flt) { |
|
| 118 | $newKey = $key->floatValue(); |
|
| 119 | } elseif ($key instanceof Integer) { |
|
| 120 | $newKey = $key->intValue(); |
|
| 121 | } elseif ($key instanceof Boolean) { |
|
| 122 | $newKey = $key->booleanValue(); |
|
| 123 | } elseif (method_exists($key, '__toString')) { |
|
| 124 | $newKey = $key->__toString(); |
|
| 125 | } else { |
|
| 126 | throw new InvalidKeyException('Passed key has to be a primitive datatype or ' . 'has to implement the __toString() method'); |
|
| 127 | } |
|
| 128 | // return the value for the passed key, if it exists |
|
| 129 | if (isset($this->items[$newKey])) { |
|
| 130 | return $this->items[$newKey]; |
|
| 131 | } else { |
|
| 132 | throw new IndexOutOfBoundsException('Index ' . $newKey . ' out of bounds'); |
|
| 133 | } |
|
| 134 | } |
|
| 135 | throw new InvalidKeyException('Passed key has to be a primitive datatype or ' . 'has to implement the __toString() method'); |
|
| 136 | } |
|
| 137 | ||
| 138 | /** |
|
| 139 | * This method checks if an element with the passed |
|
| @@ 243-284 (lines=42) @@ | ||
| 240 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
|
| 241 | * @throws \AppserverIo\Collections\IndexOutOfBoundsException |
|
| 242 | */ |
|
| 243 | public function remove($key) |
|
| 244 | { |
|
| 245 | if (is_null($key)) { |
|
| 246 | throw new NullPointerException('Passed key is null'); |
|
| 247 | } |
|
| 248 | // check if a primitive datatype is passed |
|
| 249 | if (is_integer($key) || is_string($key) || is_double($key) || is_bool($key)) { |
|
| 250 | if (isset($this->items[$key])) { |
|
| 251 | // remove the item |
|
| 252 | unset($this->items[$key]); |
|
| 253 | // return the instance |
|
| 254 | return $this; |
|
| 255 | } else { |
|
| 256 | throw new IndexOutOfBoundsException('Index ' . $key . ' out of bounds'); |
|
| 257 | } |
|
| 258 | } |
|
| 259 | // check if an object is passed |
|
| 260 | if (is_object($key)) { |
|
| 261 | if ($key instanceof Strng) { |
|
| 262 | $newKey = $key->stringValue(); |
|
| 263 | } elseif ($key instanceof Flt) { |
|
| 264 | $newKey = $key->floatValue(); |
|
| 265 | } elseif ($key instanceof Integer) { |
|
| 266 | $newKey = $key->intValue(); |
|
| 267 | } elseif ($key instanceof Boolean) { |
|
| 268 | $newKey = $key->booleanValue(); |
|
| 269 | } elseif (method_exists($key, '__toString')) { |
|
| 270 | $newKey = $key->__toString(); |
|
| 271 | } else { |
|
| 272 | throw new InvalidKeyException('Passed key has to be a primitive datatype or ' . 'has to implement the __toString() method'); |
|
| 273 | } |
|
| 274 | if (isset($this->items[$newKey])) { |
|
| 275 | // remove the item |
|
| 276 | unset($this->items[$newKey]); |
|
| 277 | // returns the instance |
|
| 278 | return $this; |
|
| 279 | } else { |
|
| 280 | throw new IndexOutOfBoundsException('Index ' . $newKey . ' out of bounds'); |
|
| 281 | } |
|
| 282 | } |
|
| 283 | throw new InvalidKeyException('Passed key has to be a primitive datatype or ' . 'has to implement the __toString() method'); |
|
| 284 | } |
|
| 285 | ||
| 286 | /** |
|
| 287 | * This method merges the elements of the passed map |
|