Java Reference
In-Depth Information
The
start
and
end
fields are both of type
ImmutablePoint
. I've added a method to
returnadependent
length
property,which iscomputed usingthePythagorean theorem in
the usual manner. This means I can access the
length
property of an
ImmutableLine
and the access will go through the
getLength
method, but because there's no setter I
can't change the value from outside. The corresponding test for this class is shown in the
following listing.
Listing 4.8. A Spock test for the
ImmutableLine
class
In order to create an
ImmutableLine
I need to first create a pair of
ImmutablePoint
instances that can be used in the
ImmutableLine
constructor. The first test checks that
the contained points are set properly and then checks the
getLength
implementation by
accessing the
length
“field.” Finally, I make sure that I can't reassign the
start
or
end
properties of the line.
Taking this one step further, what happens if the class contains a collection? The @
Im-
mutable
annotation will cause the collection to be wrapped by one of its unmodifiable