Java Reference
In-Depth Information
Figure 3.6. Mixing Java and Groovy classes. The Java app instantiates a Location and supplies it with street, city,
and state values. It sends the new Location to the Groovy geocoder, whose
fillInLatLng
method supplies the
latitude and longitude, which can then be retrieved by Java again.
Asthefigureshows,theJavaapplication willusea
Location
classtostorealltheneeded
attributes. It will supply the
street
,
city
, and
state
fields as input parameters, but
the
Location
class will also include
latitude
and
longitude
fields that will be up-
dated by the Groovy geocoder. The geocoder itself will be written in Groovy, because it's
7
Note this is just like the geocoder with the
Stadium
class used in
chapter 2
when I discussed the Groovy Baseball
application. The differences here are the CSV output and that I'm invoking the Groovy implementation from Java.
Here's the new
Location
class, which could be written in either Java or Groovy. This
time, to keep the code simple I'll use a Groovy POGO:
class
Location {
String street
String city
String state
double
latitude
double
longitude
}
The
Location
class encapsulates the address information in strings and provides double
variables for the latitude and longitude values that will be set using the geocoder. Speaking
of the geocoder, the next listing shows a revised version that wraps the script into a class.