Skip to content

Commit

Permalink
add toHexString and BugFix: rename Exception name
Browse files Browse the repository at this point in the history
  • Loading branch information
bastie committed Jan 4, 2025
1 parent 19f8e55 commit df8e1ed
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/JavApi/io/ByteArrayInputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension java.io {

public override func read(_ array: inout [UInt8], _ offset: Int, _ length: Int) throws -> Int {
guard offset >= 0, length >= 0, length <= (array.count - offset) else {
throw java.lang.Throwable.IndexOutOufBoundsException()
throw java.lang.Throwable.IndexOutOfBoundsException()
}
let k = min (length, (self.count - pos))

Expand Down
6 changes: 6 additions & 0 deletions Sources/JavApi/lang/Int+Java.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ extension Int {
public static func reverseBytes (_ value : Int) -> Int {
return Int (Int32(value).byteSwapped)
}


public func compareTo(_ other : Int) -> Int {
return self > other ? 1 : (self < other ? -1 : 0)
}

}
13 changes: 13 additions & 0 deletions Sources/JavApi/lang/Int16+Java.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* SPDX-FileCopyrightText: 2024 - Sebastian Ritter <bastie@users.noreply.github.com>
* SPDX-License-Identifier: MIT
*/

extension Int16 {
public func compareTo(_ other : Int16) -> Int {
return self > other ? 1 : (self < other ? -1 : 0)
}
public static func parseInt (_ decimalNumber : Int16) -> String {
return Integer.toHexString (decimalNumber)
}
}
7 changes: 7 additions & 0 deletions Sources/JavApi/lang/Int32+Java.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ extension Int32 {
public static func reverseByte (_ value : Int32) -> Int32 {
return value.byteSwapped
}
public func compareTo(_ other : Int32) -> Int {
return self > other ? 1 : (self < other ? -1 : 0)
}

public static func parseInt (_ decimalNumber : Int32) -> String {
return Integer.toHexString (decimalNumber)
}
}
8 changes: 8 additions & 0 deletions Sources/JavApi/lang/Int64+Java.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@ extension Int64 {
public static func numberOfTrailingZeros (long : Int64) -> Int {
return long.trailingZeroBitCount
}

public func compareTo(_ other : Int64) -> Int {
return self > other ? 1 : (self < other ? -1 : 0)
}

public static func parseInt (_ decimalNumber : Int64) -> String {
return Integer.toHexString (decimalNumber)
}
}

5 changes: 5 additions & 0 deletions Sources/JavApi/lang/Integer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ public class Integer {
public static func valueOf (_ string : String, _ radix : Int) throws -> Int {
return try parseInt(string)
}

public static func toHexString (_ decimalNumber : any BinaryInteger) -> String {
let hexString = String(decimalNumber, radix: 16)
return hexString
}
}
6 changes: 3 additions & 3 deletions Sources/JavApi/lang/StringBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public class StringBuilder {

public func charAt (_ offset : Int) throws -> Character {
guard offset > -1, offset < self.count else {
throw java.lang.Throwable.IndexOutOufBoundsException(offset, "the index is negative or greater than or equal to count of String")
throw java.lang.Throwable.IndexOutOfBoundsException(offset, "the index is negative or greater than or equal to count of String")
}
return Array (self.content)[offset]
}

public func deleteCharAt (_ offset : Int) throws -> StringBuilder {
guard offset > -1, offset < self.count else {
throw java.lang.Throwable.IndexOutOufBoundsException(offset, "the index is negative or greater than or equal to count of String")
throw java.lang.Throwable.IndexOutOfBoundsException(offset, "the index is negative or greater than or equal to count of String")
}
var asCharArray = Array(self.content)
asCharArray.removeFirst(offset)
Expand All @@ -60,7 +60,7 @@ public class StringBuilder {

public func setLength (_ newLength : Int) throws {
guard newLength >= 0 else {
throw Throwable.IndexOutOufBoundsException(newLength, "New length need to be equals or greater than zero but is \(newLength)")
throw Throwable.IndexOutOfBoundsException(newLength, "New length need to be equals or greater than zero but is \(newLength)")
}

switch newLength {
Expand Down
12 changes: 12 additions & 0 deletions Sources/JavApi/lang/Throwable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,25 @@ public enum Throwable : Error {
case InstantiationError (_ message : String = "InstantiationError")
/// - Since: JavaApi &gt; 0.16.0 (Java 1.0)
case InternalError (_ message : String = "InternalError")
/// - Since: JavaApi &lt; 0.19.2 (Java 9.0)
case IndexOutOfBoundsException (_ withValue : Int)
/// - Since: JavaApi &gt; 0.19.2 (Java 16.0)
/// - Note: As result of implementation way in swift, we add a ignored parameter to be different to `case` with `Int` value
case IndexOutOfBoundsException (_ withLongValue : Int64, _ignored : Bool = true)
/// - Since: JavaApi &lt; 0.16.0 (Java 1.0)
case IndexOutOfBoundsException (_ withValue : Int = 0, String = "IndexOutOufBoundsException")

/// - Since: JavaApi &lt; 0.16.0 (Java 9.0)
@available(*, deprecated, renamed: "IndexOutOfBoundsException", message: "use IndexOutOfBoundsException instead and spell correctly")
case IndexOutOufBoundsException (_ withValue : Int)
/// - Since: JavaApi &gt; 0.16.0 (Java 16.0)
/// - Note: As result of implementation way in swift, we add a ignored parameter to be different to `case` with `Int` value
@available(*, deprecated, renamed: "IndexOutOfBoundsException", message: "use IndexOutOfBoundsException instead and spell correctly")
case IndexOutOufBoundsException (_ withLongValue : Int64, _ignored : Bool = true)
/// - Since: JavaApi &lt; 0.16.0 (Java 1.0)
@available(*, deprecated, renamed: "IndexOutOfBoundsException", message: "use IndexOutOfBoundsException instead and spell correctly")
case IndexOutOufBoundsException (_ withValue : Int = 0, String = "IndexOutOufBoundsException")

/// - Since: JavaApi &gt; 0.16.0 (Java 1.0)
case LinkageError (_ message : String = "LinkageError")
/// - Since: JavaApi &gt; 0.16.0 (Java 1.0)
Expand Down
4 changes: 2 additions & 2 deletions Sources/JavApi/nio/ByteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ open class ByteBuffer {

open func put (_ bytes : [UInt8], _ offset : Int, _ length : Int) throws -> ByteBuffer {
guard offset > -1 && offset < bytes.count else {
throw Throwable.IndexOutOufBoundsException(offset, "illegal start position \(offset)")
throw Throwable.IndexOutOfBoundsException(offset, "illegal start position \(offset)")
}
guard length > -1 && length <= (bytes.count-offset) else {
throw Throwable.IndexOutOufBoundsException(offset, "illegal length \(length)")
throw Throwable.IndexOutOfBoundsException(offset, "illegal length \(length)")
}
for i in offset..<(offset+length) {
try _ = self.put(bytes[i])
Expand Down

0 comments on commit df8e1ed

Please sign in to comment.