Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add java.lang.String#hashCode(), java.lang.System.identityHashCode(),…
… and java.util.Objects.hashCode()
  • Loading branch information
chitoku-k committed May 7, 2019
commit 7de369d46725bb9bca1a42dd030b29ba0af1d867
6 changes: 3 additions & 3 deletions src/Packages/java/lang/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PHPJava\Exceptions\NotImplementedException;
use PHPJava\Packages\java\lang\_Object;
use PHPJava\Packages\PHPJava\Kernel\Behavior\System as SystemBehavior;

// use PHPJava\Packages\java\lang\System\Logger;
// use PHPJava\Packages\java\util\Map;
Expand Down Expand Up @@ -194,12 +195,11 @@ public static function static_getSecurityManager($a = null)
*
* @param mixed $a
* @return mixed
* @throws NotImplementedException
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/package-summary.html#identityHashCode
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#identityHashCode(java.lang.Object)
*/
public static function static_identityHashCode($a = null)
{
throw new NotImplementedException(__METHOD__);
return SystemBehavior::identityHashCode($a);
}

/**
Expand Down
24 changes: 17 additions & 7 deletions src/Packages/java/lang/_String.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
class _String extends _Object implements CharSequence
{
private $hash = 0;
private $object = null;

/**
Expand Down Expand Up @@ -223,8 +224,7 @@ public function endsWith($a = null)
*
* @param mixed $a
* @return mixed
* @throws NotImplementedException
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/package-summary.html#equals
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#equals(java.lang.Object)
*/
public function equals($a = null)
{
Expand Down Expand Up @@ -304,14 +304,24 @@ public function getChars($a = null, $b = null, $c = null, $d = null)
/**
* Returns a hash code for this string.
*
* @param mixed $a
* @return mixed
* @throws NotImplementedException
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/package-summary.html#hashCode
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#hashCode()
*/
public function hashCode($a = null)
public function hashCode()
{
throw new NotImplementedException(__METHOD__);
$count = $this->length();

$h = $this->hash;
if ($h || !$count) {
return $h;
}

$str = $this->toString();
for ($i = 0; $i < $count; ++$i) {
$h = 31 * $h + ord($str[$i]);
}

return $this->hash = $h;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/Packages/java/util/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,15 @@ public static function static_hash($a = null)
*
* @param mixed $a
* @return mixed
* @throws NotImplementedException
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/package-summary.html#hashCode
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Objects.html#hashCode(java.lang.Object)
*/
public static function static_hashCode($a = null)
{
throw new NotImplementedException(__METHOD__);
if ($a === null) {
return 0;
}

return $a->hashCode();
}

/**
Expand Down