Java Reference
In-Depth Information
This is an implementation of the Babylonian method for estimating the square root of a num-
ber. It isn't the most efficient implementation; in particular, the initial guess could be much
better, which would save some iterations. That is deliberate since it allows the calculation to
take some time (emulating business logic), though it does illustrate the basic point made in
Chapter 1 : often the better way to make Java code faster is to write a better algorithm, inde-
pendent of any Java tuning or Java coding practices that are employed.
The standard deviation, average price, and histogram of an implementation of the Stock-
PriceHistory interface are all derived values. In different examples, these values will be
calculated eagerly (when the data is loaded from the entity manager) or lazily (when the
method to retrieve the data is called). Similarly, the StockPrice interface references a
StockOptionPrice interface, which is the price of certain options for the given stock on the
given day. Those option values can be retrieved from the entity manager either eagerly or
lazily. In both cases, the definition of these interfaces allows these different approaches to be
compared in different situations.
These interfaces also fit naturally into a Java EE application: a user can visit a JSP page that
lets her enter the symbol and date range for a stock she is interested in. In the standard ex-
ample, the request will go through a standard servlet that parses the input parameters, calls a
stateless Enterprise JavaBean (EJB) with an embedded Java Persistence API (JPA) bean to
get the underlying data, and forwards the response to a JavaServer Pages (JSP) page, which
formats the underlying data into an HTML presentation:
protected
protected void
void processRequest ( HttpServletRequest request ,
HttpServletResponse response )
throws
throws ServletException , IOException {
try
try {
String symbol = request . getParameter ( "symbol" );
iif ( symbol == null
null ) {
symbol = StockPriceUtils . getRandomSymbol ();
}
... similar processing for
for date and other params ...
StockPriceHistory sph ;
DateFormat df = localDf . get ();
sph = stockSessionBean . getHistory ( symbol , df . parse ( startDate ),
df . parse ( endDate ), doMock , impl );
String saveSession = request . getParameter ( "save" );
iif ( saveSession != null
null ) {
.... Store the data in the user ' s session ....
.... Optionally store the data in a global cache for
for
.... use by other requests
Search WWH ::




Custom Search