Additionally, you can compare time literals for equality (30s == 30s), inequality (30s != 40s), and more (10m20m, 10m >= 10m). You can also negate time literals (-2h), and invoke various conversion functions (3h.toMinutes() and 23m.toSeconds ()).
When the JavaFX compiler encounters an expression consisting of time literals and operators, it converts this syntactic sugar to equivalent Duration instances and function calls. For example, var startTime = 10m is equivalent to var startTime = Duration { millis: 600000 }, and startTime+20s is equivalent to startTime.add (Duration { millis: 20000 }).
A bug to watch out for
The NetBeans implementation of JavaFX Script is somewhat buggy. For example, var startTime = 10m; startTime += 10s; compiles correctly, but results in a thrown java.lang.ClassCastException at runtime. The workaround to this bug involves replacing += with +, which results in startTime = startTime+10s;. (This bug has been reported. See the Resources section to learn about reporting bugs in JavaFX.)
Sequences
The javafx.lang package's Sequences class is useful for manipulating sequences via its various utility functions. For example, you can sort a sequence via the Sequences public static sort(seq: java.lang.Object[], c: java.util.Comparator): [] function, which is demonstrated by the following script:
Listing 3. Sorting a sequence
var ages = [32, 23, 45, 19, 67, 98, 52];
java.lang.System.out.println (javafx.lang.Sequences.sort (ages, null))
// Output: [ 19, 23, 32, 45, 52, 67, 98 ]
Additional interesting functions that you'll find in Sequences include:
* public static binarySearch(seq: java.lang.Comparable[], key: java.lang.Comparable): Integer -- search the specified sequence for the specified object using the binary search algorithm.
* public static reverse(seq: java.lang.Object[]): [] -- reverse a sequence.
* public static shuffle(seq: java.lang.Object[]): [] -- randomly permute a sequence (as in shuffling a deck of cards) using a default source of randomness.
|