Skip to content

Commit

Permalink
make Date equalable, clonable and comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
bastie committed Sep 13, 2024
1 parent 8f46839 commit 9056d06
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 1 deletion.
30 changes: 30 additions & 0 deletions Sources/JavApi/lang/Comparable+Swiftify.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: 2024 - Sebastian Ritter <bastie@users.noreply.github.com>
* SPDX-License-Identifier: MIT
*/

extension ComparableJ {

public static func <= (lhs: Self, rhs: Self) -> Bool {
if lhs == rhs {
return true
}
return lhs < rhs
}

public static func >= (lhs: Self, rhs: Self) -> Bool {
if lhs == rhs {
return true
}
return lhs > rhs
}

public static func > (lhs: Self, rhs: Self) -> Bool {
return try! lhs.compareTo((rhs as! Self.ComparableJ)) > 0
}

public static func < (lhs: Self, rhs: Self) -> Bool {
return try! lhs.compareTo((rhs as! Self.ComparableJ)) < 0
}

}
13 changes: 13 additions & 0 deletions Sources/JavApi/lang/Comparable.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
*/

/// `Comparable` type in Java
public protocol ComparableJ : Comparable {

func compareTo (_ other : ComparableJ?) throws -> Int

associatedtype ComparableJ : java.lang.Comparable
}

1 change: 1 addition & 0 deletions Sources/JavApi/lang/java.lang.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extension java {
extension java.lang {
public typealias Array = Swift.Array
public typealias Cloneable = JavApi.Cloneable
public typealias Comparable = JavApi.ComparableJ
public typealias Exception = JavApi.Throwable
public typealias System = JavApi.System
public typealias String = Swift.String
Expand Down
17 changes: 17 additions & 0 deletions Sources/JavApi/util/Date+Cloneable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* SPDX-FileCopyrightText: 2024 - Sebastian Ritter <bastie@users.noreply.github.com>
* SPDX-License-Identifier: MIT
*/

import Foundation

extension java.util.Date : Cloneable {
public func clone() throws -> java.util.Date {
let copy = java.util.Date()
copy.delegate = self.delegate
return copy
}

public typealias Cloneable = java.util.Date

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

import Foundation

extension java.util.Date : ComparableJ {

public func compareTo(_ other: java.util.Date?) throws -> Int {
if let other {
if self.delegate > other.delegate {
return 1
}
else {
if self.delegate < other.delegate {
return -1
}
else {
return 0
}
}
}
else {
throw Throwable.NullPointerException()
}
}


public typealias Comparable = java.util.Date

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

import Foundation

extension java.util.Date : Equatable {
public static func == (lhs: java.util.Date, rhs: java.util.Date) -> Bool {
return lhs.delegate == rhs.delegate
}
}
2 changes: 1 addition & 1 deletion Sources/JavApi/util/Date.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 - Sebastian Ritter <bastie@users.noreply.github.com>
* SPDX-FileCopyrightText: 2023, 2024 - Sebastian Ritter <bastie@users.noreply.github.com>
* SPDX-License-Identifier: MIT
*/

Expand Down

0 comments on commit 9056d06

Please sign in to comment.