Skip to content

Commit

Permalink
add hashCode() function to java.lang.StringBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
bastie committed Sep 15, 2024
1 parent e66a934 commit 9129a1d
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Sources/JavApi/lang/String+Java.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ extension String {
return self
}

/// Hashcode of ``String``
public func hashCode () -> Int {
return self.hashValue
}
}

fileprivate let TRIM_CHARACTER_SET = CharacterSet(charactersIn : "\u{0000}\u{0001}\u{0002}\u{0003}\u{0004}\u{0005}\u{0006}\u{0007}\u{0008}\u{0009}\u{000A}\u{000B}\u{000C}\u{000D}\u{000E}\u{000F}\u{0010}\u{0011}\u{0012}\u{0013}\u{0014}\u{0015}\u{0016}\u{0017}\u{0018}\u{0019}\u{001A}\u{001B}\u{001C}\u{001D}\u{001E}\u{001F}\u{0020}") // different to strip can be readed f.e. here: (https://stackoverflow.com/questions/51266582/difference-between-string-trim-and-strip-methods-in-java-11)
Expand Down
9 changes: 9 additions & 0 deletions Sources/JavApi/lang/String+Swiftify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

extension String {

// overwrite Foundation.String hashValue with Java like hashCode
var hashValue : Int {
var hash = 0
for character in self.reversed() {
hash = 31 * hash + Int(character.asciiValue!)
}
return hash
}

/// Returns the bytes of String in given encoding
/// - Returns byte array
public func getBytes () -> [UInt8] {
Expand Down
11 changes: 11 additions & 0 deletions Sources/JavApi/lang/StringBuilder+Equalable.swift
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
}
}
25 changes: 25 additions & 0 deletions Sources/JavApi/lang/StringBuilder+Hashable.swift
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
}
}
31 changes: 21 additions & 10 deletions Sources/JavApi/lang/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public struct System {
}
}

/// Return the value from environment variable
/// - Returns value of environment variable or nil
static public func getenv (_ name : String) -> String? {
return ProcessInfo.processInfo.environment[name]
/// Return the current time in milliseconds
///
/// - Returns: milliseconds as Int64
public static func currentTimeMillis () -> Int64 {
return Int64(Date().timeIntervalSince1970.advanced(by: 0)*1_000)
}

/// Exits the application
/// - Parameters:
/// - Parameter status return value e.g. for scripting with your application
Expand All @@ -85,10 +85,21 @@ public struct System {
Foundation.exit(Int32(status))
}

/// Return the current time in milliseconds
///
/// - Returns: milliseconds as Int64
public static func currentTimeMillis () -> Int64 {
return Int64(Date().timeIntervalSince1970.advanced(by: 0)*1_000)
/// Returns a hashCode
/// - Note: unsafe
public static func identityHashCode (_ x : AnyObject?) -> Int {
if let x {
return ObjectIdentifier(x).hashValue
}
else {
return 0
}
}

/// Return the value from environment variable
/// - Returns value of environment variable or nil
public static func getenv (_ name : String) -> String? {
return ProcessInfo.processInfo.environment[name]
}

}
32 changes: 32 additions & 0 deletions Tests/JavApiTests/JavApi_lang_StringBuilder_Tests.swift
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())
}
}

0 comments on commit 9129a1d

Please sign in to comment.