-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add hashCode() function to java.lang.StringBuilder
- Loading branch information
Showing
6 changed files
with
102 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 - Sebastian Ritter <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
extension StringBuilder : Equatable { | ||
|
||
public static func == (lhs: StringBuilder, rhs: StringBuilder) -> Bool { | ||
return lhs === rhs | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 - Sebastian Ritter <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
import Foundation | ||
|
||
extension StringBuilder : Hashable { | ||
|
||
public var hashValue: Int { | ||
var hasher = Hasher() | ||
hash(into: &hasher) | ||
return hasher.finalize() | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(System.identityHashCode(self)) | ||
hasher.combine(self.content) | ||
} | ||
|
||
// the Java method | ||
public func hashCode () -> Int { | ||
return hashValue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 - Sebastian Ritter <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
import XCTest | ||
@testable import JavApi | ||
|
||
final class JavApi_lang_StringBuilder_Tests: XCTestCase { | ||
|
||
func testEquals () { | ||
let a = StringBuilder("1") | ||
let b = StringBuilder("2") | ||
let c = StringBuilder("1") | ||
XCTAssertFalse(a===b); | ||
XCTAssertFalse(a == b) // false | ||
XCTAssertTrue(a == a) // true | ||
XCTAssertFalse(a===c) // false | ||
XCTAssertFalse(a==c) // false | ||
} | ||
|
||
func testHashCode () { | ||
let a = StringBuilder("1") | ||
let b = StringBuilder("2") | ||
let c = StringBuilder("1") | ||
// call twice with same result | ||
XCTAssertEqual(a.hashCode(), a.hashCode()) | ||
// two objects with different content creates different hashCode | ||
XCTAssertNotEqual(a.hashCode(), b.hashCode()) | ||
// two objects with same content creates different hashCode | ||
XCTAssertNotEqual(a.hashCode(), c.hashCode()) | ||
} | ||
} |