Flylib.com

Books Software

 
 
 

Real World Web Services - page 66


Time

The Time package has four classes: DateFormatUtils , DateUtils , FastDateFormat , and StopWatch . Both DateFormatUtils and FastDateFormat serve to make it easier to render dates into strings. FastDateFormat is a quick, thread-safe implementation of a date and time formatter. DateFormatUtils includes several FastDateFormat objects, preconfigured for different uses, including different ISO8601 formats as well as an SMTP format.

The StopWatch class allows you to perform timings much like a stopwatchit allows you to start a timer, perform a split, stop, reset, and so on. Note that the StopWatch class is really only a holder for time values from System.currentTimeMillis() there is no notion of a scheduler or execution thread. Interestingly, the code:

StopWatch testWatch = new StopWatch();
try {
     testWatch.start();
     Thread.sleep(1000);
     testWatch.stop();
     System.out.println(testWatch.getTime());
   } catch (Exception e)  { e.printStackTrace(); }

. . . returns a value of 1001.


Summary

Although the Lang package offers a wide suite of packages, it can be hard to keep track of the various capabilities. This chapter provides an overview, but for more detail on the base classes and the time package, see Appendix A

Almost every Java application can benefit from the Lang package. Similarly, many programs can benefit from the Collections package, described in the next chapter.

Project Ideas

Write a Swing user interface to wrap the conversion capabilities of StringEscapeUtils . Compare this with writing a command-line interface for the same capabilities, perhaps using the CLI package described in Chapter 13, "CLI (Command Line Interface)." What are the tradeoffs of this approach?

Create a clock application using the various date and time formats as described in the Time package.



Chapter 11. Collections

The term "collections" refers to the notion of objects that contain or manage other objects. Most developers are familiar with the basic collections, including the classic array (such as String[] ) or the built-in Java classes (such as java.util.Hashtable ). The introduction of a rich Java Collections Framework in Java 2 (JDK 1.2) brought a standardized implementation of classes for expressing more complex relationships. The Apache Jakarta Commons Collections project adds even more capability, as described in this chapter.

Table 11-1 compares some of the key differences between basic collection types. When possible, you should try to rely on the interfaces shown, not on the underlying implementation. For example, even if you have marked a HashMap as typed, sorted, and unmodifiable, you should still interface with it as a java.util.Map whenever possible:

Table 11-1. Collection Features
 

Interface

Duplicates

Keys

Indexed

Ordered

Array

Object[]

Yes

No

Yes

Yes

Bag

org.apache.commons
.collections.Bag

Yes

No

No

No

List

java.util.List

Yes

No

Yes

Yes

Map

java.util.Map

No

Yes

No

Optional

Primitive Array

primitive[]

Yes

No

Yes

Yes

Set

java.util.Set

No

No

No

Optional


java.util.Map myMap = new java.util.HashMap();

For more information on the built-in JDK 1.4 collections, see http://java.sun.com/j2se/1.4.2/docs/guide/collections/reference.html. Note that this chapter does not duplicate information included in the default JDK documentationfor example, the Stack and SortedMap interfaces are concepts covered as part of the standard Java collection interface.

The meaning of each column heading in Table 11-1 is as follows :

Interface: The expected Java interface for this collection type.

Duplicates: Are duplicate elements allowed?

Keys: Are elements accessible by a key value (e.g., java.util.Hashtable )?

Indexed: Is the order of the elements maintained by a column in the database?

Ordered: Are the results returned as a sorted collection?