Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Samples/JavaProbablyPrime/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import CompilerPluginSupport
import PackageDescription

let package = Package(
name: "JavaProbablyPrime",
platforms: [
.macOS(.v13),
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6),
.macCatalyst(.v13),
],

products: [
.executable(
name: "JavaProbablyPrime",
targets: ["JavaProbablyPrime"]
),
],

dependencies: [
.package(name: "swift-java", path: "../../"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
],

targets: [
.executableTarget(
name: "JavaProbablyPrime",
dependencies: [
.product(name: "JavaKit", package: "swift-java"),
.product(name: "JavaKitVM", package: "swift-java"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
],
swiftSettings: [
.swiftLanguageMode(.v5)
],
plugins: [
.plugin(name: "Java2SwiftPlugin", package: "swift-java"),
]
),
]
)
13 changes: 13 additions & 0 deletions Samples/JavaProbablyPrime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# JavaKit Example: Using Java APIs from Swift

This package contains an example program that uses Java's [`java.math.BigInteger`](https://docs.oracle.com/javase/8/docs/api/?java/math/BigInteger.html) from Swift to determine whether a given number is probably prime. You can try it out with your own very big number:

```
swift run JavaProbablyPrime <very big number>
```

The package itself demonstrates how to:

* Use the Java2Swift build tool plugin to wrap the `java.math.BigInteger` type in Swift.
* Create a `JavaVirtualMachine` instance in a Swift command-line program.
* Create an instance of `BigInteger` in Swift and use its `isProbablyPrime`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"classes" : {
"java.math.BigInteger" : "BigInteger"
}
}
38 changes: 38 additions & 0 deletions Samples/JavaProbablyPrime/Sources/JavaProbablyPrime/prime.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import ArgumentParser
import JavaKit
import JavaKitVM

@main
struct ProbablyPrime: ParsableCommand {
@Argument(help: "The number to check for primality")
var number: String

@Option(help: "The certainty to require in the prime check")
var certainty: Int32 = 10

func run() throws {
let javaVirtualMachine = try JavaVirtualMachine.shared()
let jniEnvironment = try javaVirtualMachine.environment()

let bigInt = BigInteger(number, environment: jniEnvironment)
if bigInt.isProbablePrime(certainty) {
print("\(number) is probably prime")
} else {
print("\(number) is definitely not prime")
}
}
}
120 changes: 0 additions & 120 deletions Sources/JavaKitExample/JavaKitExample.swift

This file was deleted.

27 changes: 0 additions & 27 deletions Sources/JavaKitExample/com/example/swift/HelloSubclass.java

This file was deleted.

57 changes: 0 additions & 57 deletions Sources/JavaKitExample/com/example/swift/HelloSwift.java

This file was deleted.

27 changes: 0 additions & 27 deletions Sources/JavaKitExample/com/example/swift/JavaKitSampleMain.java

This file was deleted.