Monday 14 September 2009

JavaFX Week 3.4 Binding

I almost missed the task of doing the binding assignment, because I got so wrapped up in the Asyncronous resources, then got interested in the scene-graph topics, but finally I got around to completing it.

It's worth highlighting the fact, that binding to an object will mean that any changes to the attributes within the object, will cause a new object to be instantiated. This was explained in the JavaPassion course, the only addition I would make is that you can verify this easier by adding the hashcode to the details being output (for those that don't know, the hashcode by default will contain an object reference that will be unique for each Address object - you can override this to be whatever you like, but we are relying on it to compare object references to prove that the binding created a new object instance).


// Define Address class.
class Address {
var street: String;
var city: String;
var state: String;
var zip: String;
}

var myStreet = "1 Main Street";
var myCity = "Santa Clara";
var myState = "CA";
var myZip = "95050";

// Bind address variable to Address object
def address = bind Address {
street: myStreet;
city: myCity;
state: myState;
zip: myZip;
};

println("----address.street before myStreet is changed= {address.street}
hash:{address.hashCode()}"); // 1 Main Street

// By changing the value of myStreet, the street variable inside
// the address object is affected. Note that changing the value
// of myStreet actually causes a new Address object to be
// created and then re-assigned to the address variable.
myStreet = "100 Maple Street";
println("----address.street after myStreet is changed
= {address.street} hash:{address.hashCode()}");


Now if you change the binding above to this...


def address = bind Address {
street: myStreet;
city: myCity;
state: myState;
zip: myZip;
};


Then you will notice that the two address hashcodes are identical - proving now that JavaFX did not have the overhead of instantiating a new object, but simply changed the street attribute (most likely what you intended).

Bind for

I was impressed with the binding ability against sequences, I add an object to the sequence seq1 and the sequence seq2 was updated to reflect the new value, but doubled as per the expression in the bind - wow!


var seq1 = [1..5];

// Bind to for expression
def seq2 = bind for (item in seq1) item*2;

printSeqs();

insert 10 into seq1;

printSeqs();

function printSeqs() {
println("----First Sequence:");
for (i in seq1){println(i);}
println("----Second Sequence:");
for (i in seq2){println(i);}
}


binding_to_if contains a bit of code that was a little odd to read at first


var primeOrNot = bind if (Sequences.indexOf(primeNumbers, curr) == -1)
"not a prime number"
else "prime number";


I completely miss-read the line, I read it as 'from the sequence, find the index of...', but it actually reads as 'Using the indexof utility method from the Sequences static method, find the first object in the primeNumbers sequence that matches the value curr'. Here's the JavaFX doc for it:


Searches the specified sequence for an object with the same value. The objects are compared using the method equals(). If the sequence is sorted, binarySearch should be used instead. If the sequence contains multiple elements equal to the specified object, the first occurence in the sequence will be returned. The method nextIndexOf() can be used in consecutive calls to iterate through all occurences of a specified object.



import javafx.util.Sequences;

var primeNumbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
var curr = 1;

// Bind to if expression
var primeOrNot = bind if (Sequences.indexOf(primeNumbers, curr) == -1)
"not a prime number"
else "prime number";
println("----{curr} is {primeOrNot}");

curr = 6;
println("----{curr} is {primeOrNot}");

curr = 3;
println("----{curr} is {primeOrNot}");

No comments:

Post a Comment