23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> I/O<br />

public abstract StringBuffer format(double number, StringBuffer toAppendTo,<br />

FieldPosition pos)<br />

public abstract StringBuffer format(long number, StringBuffer toAppendTo,<br />

FieldPosition pos)<br />

public abstract Number parse(String text, ParsePosition parsePosition)<br />

The two format methods must format a long and a double respectively, update the<br />

FieldPosition object with the locations of the different fields, append the formatted string<br />

to the string buffer toAppendTo, and return that same string buffer. The parse() method<br />

must read a number in scientific notation, convert it to a java.lang.Number (that is, a<br />

java.lang.Long or a java.lang.Double) and return that.<br />

The concrete formatting methods in NumberFormat all invoke these methods, so they may be<br />

kept as is rather than being overridden. However, it would not hurt to override clone(),<br />

hashCode(), and equals(). You could also add some additional methods to specify<br />

formatting of the exponent. However, to keep this example reasonably compact, I'll assume<br />

that the exponent is only as wide as it needs to be with either a + or a - prefix. This is large<br />

enough to handle both the largest and the smallest double values in <strong>Java</strong><br />

(1.79769313486231570e+308 and 4.94065645841246544e-324, respectively).<br />

A typical exponential number has three fields: the integer, the fraction, and the exponent. It<br />

would be nice to define constants for these fields and use the FieldPosition object passed to<br />

format() to identify the locations of these fields in the formatted output. Regrettably, the<br />

java.text API does not provide adequate support for third-party formatting classes. The<br />

FieldPosition set methods are "friendly"—that is, accessible only to other classes in the<br />

same package—so our class can't use them.<br />

void setBeginIndex(int bi)<br />

void setEndIndex(int ei)<br />

Similarly, there's no way to add extra field definitions, such as an exponent field. Therefore,<br />

any user-created Format subclass will be crippled relative to the ones <strong>Java</strong>Soft provides.<br />

Example 16.9 shows the code for the ExponentialFormat class. There are at least three<br />

different ways to parse and format exponential numbers. The simplest, and the one I've used<br />

here, is to treat an exponential number as a combination of a decimal number plus the letter<br />

"e" or "E" plus an integer. Then use DecimalFormat to format or parse those parts. It would<br />

be slightly more efficient to do all formatting directly, but the benefits of code reuse more<br />

than offset a small increase in efficiency. Internationalization comes for free, since the<br />

DecimalFormat class handles internationalization. A DecimalFormat object for a given<br />

locale may be passed to the constructor. Otherwise, the format for the default locale is<br />

selected.<br />

Example 16.9. ExponentialFormat<br />

import java.text.*;<br />

import java.io.*;<br />

import java.util.*;<br />

424

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!