Using JavaFX sequences in a Java program

The last two articles were about how to pass and deal with primitive datatypes, Java objects, and JavaFX objects. This article focuses on JavaFX sequences.

There are quite a number of Java classes implemented to represent JavaFX sequences. Which implementation is used depends on the input parameters during creation. All of the implementations share the interface Sequence, which we must therefore use when dealing with sequences in Java code.

Using a specific sequence

The interface Sequence is generic. When using it as a parameter in a method, we can define the exact type in the method signature or we leave the type open by defining a generic method. The first code sample shows a method which expects a sequence of Integer-objects. Note that the type is not exactly defined, but used as an upper bound. This needs to be done always.

 1 import com.sun.javafx.runtime.sequence.Sequence;
2 import java.util.Iterator;
3
4 public class MyIntegerLibrary {
5
6 public static void printIntegerSum (Sequence<? extends Integer> seq) {
7 int sum = 0;
8 for (Iterator<? extends Integer> it = seq.iterator(); it.hasNext();) {
9 sum += it.next();
10 }
11 System.out.println(sum);
12 }
13 }

Code Sample 1: Calculating sum of an Integer sequence

The method printIntegerSum in Code Sample 1 iterates through the sequence of Integers and calculates the sum. The JavaFX statement in Code Sample 2 creates a sequence of 5 integers and calls the method printIntegerSum.

 1 MyIntegerLibrary.printIntegerSum( [2, 3, 5, 7, 11] );

Code Sample 2: Calling printIntegerSum

Note that the type of this example is pretty strict. It is for example not possible to pass a sequence of Byte-objects. If you want to allow a more general sequence, you have to use a more general type. The sequence cannot be cast automatically. In the example above, the class Number can be used to allow any kind numbers in the given sequence.

Using a generic sequence

Using the interface Sequence in generic methods works as one would expect (with one exception, which will be covered later). Code Sample 3 defines a method which takes an arbitrary sequence and prints its elements using the method toString().

 1 import com.sun.javafx.runtime.sequence.Sequence;
2 import java.util.Iterator;
3
4 public class MySequencePrinter {
5
6 public static <T> void printSequence(Sequence<T> seq) {
7 for (Iterator<T> it = seq.iterator(); it.hasNext();) {
8 System.out.println(it.next().toString());
9 }
10 }
11 }

Code Sample 3: Printing the elements of an arbitrary sequence

The following statement calls the method with an Integer-Sequence.

 1 MySequencePrinter.printSequence( [2, 3, 5, 7, 11] );

Code Sample 4: Calling the Sequence-Printer

When defining a method which expects a sequence and a single element of the same type, you have to define the type of the sequence bounded as in the first example. Code Sample 5 defines a generic method, which takes an arbitrary sequence and an element. As you can see, the type of the sequence-elements extend the type of the single element.

 1 import com.sun.javafx.runtime.sequence.Sequence;
2 import java.util.Iterator;
3
4 public class MyElementCounter {
5
6 public static <T> void countElement(Sequence<? extends T> seq, T key) {
7 int sum = 0;
8 for (Iterator<? extends T>it = seq.iterator(); it.hasNext();) {
9 if (key.equals(it.next()))
10 ++sum;
11 }
12 System.out.println(sum);
13 }
14 }

Code Sample 5: Counting element in a sequence

The method countElement in Code Sample 5 counts how often the element appears in the sequence. The following statement calls the method.

 1 MyElementCounter.countElement( [2, 3, 2, 2, 5, 7, 11, 2], 2 );

Code Sample 6: Calling counting element

If you want to know more about generic types and generric methods, this chapter of the Java Tutorial is a good start.

This article finishes the small series about how to deal with parameters passed from JavaFX to Java after primitive datatypes and Java objects were covered in the first article and JavaFX objects in the second article. The next article will be a short intermezzo on how to create JavaFX objects in Java code. After that will follow another small series which focuses some more on sequences.

Comments:

Post a Comment:
Comments are closed for this entry.
About

Michael Heinrichs

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today