Skip to content

Commit 0ae5e23

Browse files
authored
Merge pull request #133 from php-java/system/identityhashcode
Add java.lang.String#hashCode(), java.lang.System.identityHashCode() and java.util.Objects.hashCode()
2 parents 4d2d1b9 + 7de369d commit 0ae5e23

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

src/Core/JVM/ConstantPool.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public function __construct(ReaderInterface $reader, int $entries)
5151
}
5252
}
5353

54+
/**
55+
* @return StructureInterface[]
56+
*/
5457
public function getEntries(): array
5558
{
5659
return $this->entries;
@@ -94,31 +97,49 @@ private function read($entryTag): ?StructureInterface
9497
throw new ReadEntryException('Entry tag ' . sprintf('0x%04X', $entryTag) . ' is not defined.');
9598
}
9699

100+
/**
101+
* @return boolean
102+
*/
97103
public function offsetExists($offset)
98104
{
99105
return isset($this->entries[$offset]);
100106
}
101107

108+
/**
109+
* @return StructureInterface
110+
*/
102111
public function offsetGet($offset)
103112
{
104113
return $this->entries[$offset];
105114
}
106115

116+
/**
117+
* @return int
118+
*/
107119
public function count()
108120
{
109121
return count($this->entries);
110122
}
111123

124+
/**
125+
* @throws ReadOnlyException
126+
*/
112127
public function offsetSet($offset, $value)
113128
{
114129
throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.');
115130
}
116131

132+
/**
133+
* @throws ReadOnlyException
134+
*/
117135
public function offsetUnset($offset)
118136
{
119137
throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.');
120138
}
121139

140+
/**
141+
* @return \ArrayIterator<StructureInterface>
142+
*/
122143
public function getIterator()
123144
{
124145
return new \ArrayIterator($this->entries);

src/Packages/java/lang/System.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PHPJava\Exceptions\NotImplementedException;
55
use PHPJava\Packages\java\lang\_Object;
6+
use PHPJava\Packages\PHPJava\Kernel\Behavior\System as SystemBehavior;
67

78
// use PHPJava\Packages\java\lang\System\Logger;
89
// use PHPJava\Packages\java\util\Map;
@@ -194,12 +195,11 @@ public static function static_getSecurityManager($a = null)
194195
*
195196
* @param mixed $a
196197
* @return mixed
197-
* @throws NotImplementedException
198-
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/package-summary.html#identityHashCode
198+
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#identityHashCode(java.lang.Object)
199199
*/
200200
public static function static_identityHashCode($a = null)
201201
{
202-
throw new NotImplementedException(__METHOD__);
202+
return SystemBehavior::identityHashCode($a);
203203
}
204204

205205
/**

src/Packages/java/lang/_String.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
class _String extends _Object implements CharSequence
2121
{
22+
private $hash = 0;
2223
private $object = null;
2324

2425
/**
@@ -223,8 +224,7 @@ public function endsWith($a = null)
223224
*
224225
* @param mixed $a
225226
* @return mixed
226-
* @throws NotImplementedException
227-
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/package-summary.html#equals
227+
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#equals(java.lang.Object)
228228
*/
229229
public function equals($a = null)
230230
{
@@ -304,14 +304,24 @@ public function getChars($a = null, $b = null, $c = null, $d = null)
304304
/**
305305
* Returns a hash code for this string.
306306
*
307-
* @param mixed $a
308307
* @return mixed
309-
* @throws NotImplementedException
310-
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/package-summary.html#hashCode
308+
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#hashCode()
311309
*/
312-
public function hashCode($a = null)
310+
public function hashCode()
313311
{
314-
throw new NotImplementedException(__METHOD__);
312+
$count = $this->length();
313+
314+
$h = $this->hash;
315+
if ($h || !$count) {
316+
return $h;
317+
}
318+
319+
$str = $this->toString();
320+
for ($i = 0; $i < $count; ++$i) {
321+
$h = 31 * $h + ord($str[$i]);
322+
}
323+
324+
return $this->hash = $h;
315325
}
316326

317327
/**

src/Packages/java/util/Objects.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,15 @@ public static function static_hash($a = null)
120120
*
121121
* @param mixed $a
122122
* @return mixed
123-
* @throws NotImplementedException
124-
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/package-summary.html#hashCode
123+
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Objects.html#hashCode(java.lang.Object)
125124
*/
126125
public static function static_hashCode($a = null)
127126
{
128-
throw new NotImplementedException(__METHOD__);
127+
if ($a === null) {
128+
return 0;
129+
}
130+
131+
return $a->hashCode();
129132
}
130133

131134
/**

0 commit comments

Comments
 (0)