|
Utilities classes
Finally, JFXtras provides a pair of utilities classes in its org.jfxtras.util package. The GeometryUtil class provides functions for converting between JavaFX and Java versions of the Point2D and Rectangle2D classes, whereas the SequenceUtil class provides the following sequence-oriented functions:
* public characterSequence(start: java.lang.String, end: java.lang.String): <any>[] generates and returns a sequence of one-character strings, starting with the first character of the start string and ending with the first character of the end string.
* public concat(seq: java.lang.String[]): java.lang.String concatenates all elements in a sequence of Strings into a single String, which is returned.
* public fold(ident: Number, seq: Number[], func: com.sun.javafx.functions.Function2): Number performs what is known in functional languages as a left fold operation. This operation consists of passing ident's value and seq's first element to reduce function func, which combines these two inputs into a single output. This output value and seq's second element are passed to func, resulting in a new output value. This output value and seq's third value are passed to func, and so on until there are no elements left in seq. The fold() function returns the final output value. Check out Wikipedia's Fold (higher-order function) entry to learn more about this operation.
* public fold(ident: Integer, seq: Integer[], func: com.sun.javafx.functions.Function2): Integer is equivalent to the former function, but is typed for Integers.
* public fold(ident: java.lang.Object, seq: java.lang.Object[], func: com.sun.javafx.functions.Function2): java.lang.Object is equivalent to the former function, but is typed for java.lang.Objects.
* public join(seq: java.lang.String[], delimiter: java.lang.String): java.lang.String joins all of seq's String elements into a single String, with the specified delimiter appearing between successive Strings. The resulting String is returned.
* public sum(seq: Number[]): Number adds all elements of a Numbers sequence and returns the total.
* public sum(seq: Integer[]): Integer adds all elements of an Integers sequence and returns the total.
Listing 1 presented an example of SequenceUtil's characterSequence() function. Listing 10 provides another example of this function, and also demonstrates most of the other functions.
Listing 10. Main.fx for a UtilDemo project
/*
* Main.fx
*/
package utildemo;
import org.jfxtras.util.SequenceUtil;
def letters = SequenceUtil.characterSequence ("dog", "bone");
println (letters);
println (SequenceUtil.concat (letters));
println (SequenceUtil.join (letters, ", "));
def grades = [ 69, 23, 46, 58 ];
println ("Average = {SequenceUtil.sum (grades)/sizeof grades}");
// What is the sum of the series 1+1/2+1/4+1/8+1/16+... (forever)?
var numbers: Number[];
for (i in [0..100])
insert 1.0/java.lang.Math.pow (2, i) into numbers;
println ("Sum = {SequenceUtil.sum (numbers)}");
// Use a fold function to calculate 5! (factorial).
println (SequenceUtil.fold (1, [2, 3, 4, 5], multiply));
function multiply (a: Integer, b: Integer): Integer
{
a*b
}
This script generates the following output:
[ d, c, b ]
dcb
d, c, b
Average = 49
Sum = 2.0
120
In conclusion
JFXtras offers considerable value for your JavaFX projects; I wouldn't be surprised to see JFXtras features appearing in future releases of JavaFX. Now that you've finished this article's brief tour of JFXtras 0.2, I recommend digging deeper into its unit-testing framework and other features. And don't forget to check out JFXtras 0.3, which supports MiGLayout and is compatible with JavaFX 1.1! |
|