Spec-Zone .ru
спецификации, руководства, описания, API
|
|
JavaTM 2 Platform Standard Edition |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.text.Format
Format
is an abstract base class for formatting locale-sensitive
information such as dates, messages, and numbers.
Format
defines the programming interface for formatting
locale-sensitive objects into String
s (the
format
method) and for parsing String
s back
into objects (the parseObject
method). Any String
formatted by format
is guaranteed to be parseable by
parseObject
.
If formatting is unsuccessful because the Format
object
cannot format the type of object specified, format
throws an
IllegalArgumentException
. Otherwise, if there is something
illformed about the object, format
returns the Unicode
replacement character \\uFFFD
.
If there is no match when parsing,
parseObject(String)
throws a ParseException
,
and parseObject(String, ParsePosition)
leaves the
ParsePosition
index
member unchanged and
returns null
.
Subclassing:
The JDK provides three concrete subclasses of Format
--
DateFormat
, MessageFormat
, and
NumberFormat
--for formatting dates, messages, and numbers,
respectively.
Concrete subclasses must implement these two methods:
format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
parseObject (String source, ParsePosition pos)
Most subclasses will also implement the following two methods:
getInstance
for getting a useful format object appropriate
for the current locale
getInstance(Locale)
for getting a useful format
object appropriate for the specified locale
getXxxxInstance
methods for more specialized control. For
example, the NumberFormat
class provides
getPercentInstance
and getCurrencyInstance
methods for getting specialized number formatters.
Subclasses of Format
that allow programmers to create objects
for locales (with getInstance(Locale)
for example)
must also implement the following class method:
public static Locale[] getAvailableLocales()
And finally subclasses may define a set of constants to identify the various
fields in the formatted output. These constants are used to create a FieldPosition
object which identifies what information is contained in the field and its
position in the formatted result. These constants should be named
item_FIELD
where item
identifies
the field. For examples of these constants, see ERA_FIELD
and its
friends in DateFormat
.
ParsePosition
,
FieldPosition
,
NumberFormat
,
DateFormat
,
MessageFormat
, Serialized FormConstructor Summary | |
Format()
|
Method Summary | |
Object |
clone()
Creates and returns a copy of this object. |
String |
format(Object obj)
Formats an object to produce a string. |
abstract StringBuffer |
format(Object obj,
StringBuffer toAppendTo,
FieldPosition pos)
Formats an object to produce a string. |
Object |
parseObject(String source)
Parses a string to produce an object. |
abstract Object |
parseObject(String source,
ParsePosition status)
Parses a string to produce an object. |
Methods inherited from class java.lang.Object |
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Constructor Detail |
public Format()
Method Detail |
public final String format(Object obj)
Subclasses will override the StringBuffer version of format.
obj
- The object to formatMessageFormat
,
format(Object, StringBuffer, FieldPosition)
public abstract StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
StringBuffer format (Number obj, StringBuffer toAppendTo) Number parse (String str)These general routines allow polymorphic parsing and formatting for objects such as the MessageFormat.
obj
- The object to formattoAppendTo
- where the text is to be appendedpos
- On input: an alignment field, if desired.
On output: the offsets of the alignment field.MessageFormat
,
FieldPosition
public abstract Object parseObject(String source, ParsePosition status)
String format (Number obj); String format (long obj); String format (double obj); Number parse (String str);
status
- Input-Output parameter.
Before calling, set status.index to the offset you want to start parsing at in the source. After calling, status.index is the end of the text you parsed. If error occurs, index is unchanged.
When parsing, leading whitespace is discarded (with successful parse), while trailing whitespace is left as is.
Example: Parsing "_12_xy" (where _ represents a space) for a number, with index == 0 will result in the number 12, with status.index updated to 3 (just before the second space). Parsing a second time will result in a ParseException since "xy" is not a number, and leave index at 3.
Subclasses will typically supply specific parse methods that return different types of values. Since methods can't overload on return types, these will typically be named "parse", while this polymorphic method will always be called parseObject. Any parse method that does not take a status should throw ParseException when no text in the required format is at the start position.
ParsePosition
public Object parseObject(String source) throws ParseException
public Object clone()
will be true, and that the expression:x.clone() != x
will be true, but these are not absolute requirements. While it is typically the case that:x.clone().getClass() == x.getClass()
will be true, this is not an absolute requirement. Copying an object will typically entail creating a new instance of its class, but it also may require copying of internal data structures as well. No constructors are called.x.clone().equals(x)
The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. The clone method is implemented by the class Object as a convenient, general utility for subclasses that implement the interface Cloneable, possibly also overriding the clone method, in which case the overriding definition can refer to this utility definition by the call:
super.clone()
Cloneable
interface. Subclasses
that override the clone
method can also
throw this exception to indicate that an instance cannot
be cloned.Cloneable
|
JavaTM 2 Platform Standard Edition |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |