Skip to content

Commit f0e869f

Browse files
committed
Clean up and fix samples
1 parent 39787f8 commit f0e869f

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

Samples/JavaProbablyPrime/Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ let package = Package(
2626
.executableTarget(
2727
name: "JavaProbablyPrime",
2828
dependencies: [
29+
.product(name: "JavaKitCollection", package: "swift-java"),
2930
.product(name: "JavaKit", package: "swift-java"),
3031
.product(name: "ArgumentParser", package: "swift-argument-parser"),
3132
],

Samples/JavaProbablyPrime/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ swift run JavaProbablyPrime <very big number>
99
The package itself demonstrates how to:
1010

1111
* Use the Java2Swift build tool plugin to wrap the `java.math.BigInteger` type in Swift.
12-
* Create a `JavaVirtualMachine` instance in a Swift command-line program.
1312
* Create an instance of `BigInteger` in Swift and use its `isProbablyPrime`.

Samples/JavaProbablyPrime/Sources/JavaProbablyPrime/prime.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ struct ProbablyPrime: ParsableCommand {
2424
var certainty: Int32 = 10
2525

2626
func run() throws {
27-
let javaVirtualMachine = try JavaVirtualMachine.shared()
28-
let jniEnvironment = try javaVirtualMachine.environment()
29-
30-
let bigInt = BigInteger(number, environment: jniEnvironment)
27+
let bigInt = BigInteger(number)
3128
if bigInt.isProbablePrime(certainty) {
3229
print("\(number) is probably prime")
3330
} else {

Samples/JavaSieve/Sources/JavaSieve/main.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import JavaKit
16-
import JavaKitVM
1716

1817
let jvm = try JavaVirtualMachine.shared(classPath: ["QuadraticSieve-1.0.jar"])
1918
do {
20-
let sieveClass = try JavaClass<SieveOfEratosthenes>(in: jvm.environment())
19+
let sieveClass = try JavaClass<SieveOfEratosthenes>(environment: jvm.environment())
2120
for prime in sieveClass.findPrimes(100)! {
2221
print("Found prime: \(prime.intValue())")
2322
}

USER_GUIDE.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,35 +61,43 @@ public struct BigInteger {
6161

6262
This Swift type wraps `java.math.BigInteger`, exposing its constructors, methods, and fields for use directly to Swift. Let's try using it!
6363

64-
### Creating a Java Virtual Machine instance from Swift
64+
### Creating a `BigInteger` and determine whether it is probably prime
6565

66-
The `JavaVirtualMachine` class, which is part of the `JavaKitVM` module, provides the ability to create and query the Java Virtual Machine. One can create a shared instance of `JavaVirtualMachine` by calling `JavaVirtualMachine.shared()`, optionally passing along extra options to the JVM (such as the class path):
66+
Now, we can go ahead and create a `BigInteger` instance from a Swift string like this:
6767

6868
```swift
69-
let javaVirtualMachine = try JavaVirtualMachine.shared()
69+
let bigInt = BigInteger(veryBigNumber)
7070
```
7171

72-
If the JVM is already running, a `JavaVirtualMachine` instance will be created to reference that existing JVM. Given a `JavaVirtualMachine` instance, one can query the JNI environment for the currently-active thread by calling `environment()`, e.g.,
72+
And then call methods on it. For example, check whether the big integer is a probable prime with some certainty:
7373

7474
```swift
75-
let jniEnvironment = try javaVirtualMachine.environment()
75+
if bigInt.isProbablePrime(10) {
76+
print("\(bigInt.toString()) is probably prime")
77+
}
7678
```
7779

78-
This JNI environment can be used to create instances of Java objects. For example, we can create a `BigInteger` instance from a Swift string like this:
80+
Swift ensures that the Java garbage collector will keep the object alive until `bigInt` (and any copies of it) are been destroyed.
81+
82+
### Creating a Java Virtual Machine instance from Swift
83+
84+
When JavaKit requires a running Java Virtual Machine to use an operation (for example, to create an instance of `BigInteger`), it will query to determine if one is running and, if not, create one. To exercise more control over the creation and configuration of the Java virtual machine, use the `JavaVirtualMachine` class, which provides creation and query operations. One can create a shared instance by calling `JavaVirtualMachine.shared()`, optionally passing along extra options to the JVM (such as the class path):
7985

8086
```swift
81-
let bigInt = BigInteger(veryBigNumber, environment: jniEnvironment)
87+
let javaVirtualMachine = try JavaVirtualMachine.shared()
8288
```
8389

84-
And then call methods on it. For example, check whether the big integer is a probable prime with some certainty:
90+
If the JVM is already running, a `JavaVirtualMachine` instance will be created to reference that existing JVM. Given a `JavaVirtualMachine` instance, one can query the JNI environment for the currently-active thread by calling `environment()`, e.g.,
8591

8692
```swift
87-
if bigInt.isProbablePrime(10) {
88-
print("\(bigInt.toString()) is probably prime")
89-
}
93+
let jniEnvironment = try javaVirtualMachine.environment()
9094
```
9195

92-
Swift ensures that the Java garbage collector will keep the object alive until `bigInt` (and any copies of it) are been destroyed.
96+
This JNI environment can be used to create instances of Java objects in a specific JNI environment. For example, we can pass this environment along when we create the `BigInteger` instance from a Swift string, like this:
97+
98+
```swift
99+
let bigInt = BigInteger(veryBigNumber, environment: jniEnvironment)
100+
```
93101

94102
### Importing a Jar file into Swift
95103

@@ -134,7 +142,7 @@ The resulting configuration file will look something like this:
134142
}
135143
```
136144

137-
As with the previous `JavaProbablyPrime` sample, the `JavaSieve` target in `Package.swift` should depend on the `swift-java` package modules (`JavaKit`, `JavaKitVM`) and apply the `Java2Swift` plugin. This makes all of the Java classes found in the Jar file available to Swift within the `JavaSieve` target.
145+
As with the previous `JavaProbablyPrime` sample, the `JavaSieve` target in `Package.swift` should depend on the `swift-java` package modules (`JavaKit`) and apply the `Java2Swift` plugin. This makes all of the Java classes found in the Jar file available to Swift within the `JavaSieve` target.
138146

139147
If you inspect the build output, there are a number of warnings that look like this:
140148

@@ -197,7 +205,6 @@ Putting it all together, we can define a main program in `Sources/JavaSieve/main
197205

198206
```swift
199207
import JavaKit
200-
import JavaKitVM
201208

202209
let jvm = try JavaVirtualMachine.shared(classPath: ["QuadraticSieve-1.0.jar"])
203210
do {

0 commit comments

Comments
 (0)