Skip to content

Commit cce97fd

Browse files
committed
Make JavaIterator a Swift Iterator and Java list a Swift Sequence
We should generalize this to all of the various Java collection types.
1 parent b796623 commit cce97fd

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
import JavaKit
15+
16+
extension JavaIterator: IteratorProtocol {
17+
public typealias Element = E
18+
19+
public func next() -> E? {
20+
if hasNext() {
21+
let nextResult: JavaObject? = next()
22+
return nextResult.map { $0.as(E.self)! }
23+
}
24+
25+
return nil
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
extension List: Sequence {
16+
public typealias Element = E
17+
public typealias Iterator = JavaIterator<E>
18+
19+
public func makeIterator() -> Iterator {
20+
return self.iterator()!.as(JavaIterator<E>.self)!
21+
}
22+
}
23+

0 commit comments

Comments
 (0)