|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectariba.util.formatter.Formatter
public abstract class Formatter
Formatter
and its subclasses (StringFormatter
,
IntegerFormatter
, etc.) are responsible for formatting raw
objects into strings and parsing strings back into objects. They also
provide methods for comparing pairs of objects for sorting purposes.
Formatter
instances can be used with the Sort
class, since all Formatters
implement the
Compare
interface.
The simplest way to use a formatter is in code like:
String foo = Formatter.stringValue(object);
Object bar = Formatter.parseString(string, type);
Clients may also get a specific formatter for a given object or type by
calling one of the static methods formatterForObject
or
formatterForType
.
For improved runtime performance, this class maintains a cache of formatter instances, keyed by type.
Field Summary |
---|
Fields inherited from interface ariba.util.core.Compare |
---|
EqualTo, GreaterThan, LessThan |
Constructor Summary | |
---|---|
Formatter()
Creates a new Formatter . |
Method Summary | |
---|---|
boolean |
canFormatNulls()
Check whether this formatter handles null values. Most formatters do not handle null values, and so the user of the formatter is expected to handle the null value themselves. |
int |
compare(java.lang.Object o1,
java.lang.Object o2)
Compares two objects for sorting purposes in the default locale. |
int |
compare(java.lang.Object o1,
java.lang.Object o2,
java.util.Locale locale)
Compares two objects for sorting purposes in the given locale. |
boolean |
equal(java.lang.Object o1,
java.lang.Object o2)
Returns true if and only if the two objects should be considered equal in the default locale. |
boolean |
equal(java.lang.Object o1,
java.lang.Object o2,
java.util.Locale locale)
Returns true if and only if the two objects should be considered equal in the given locale. |
java.lang.String |
getFormat(java.lang.Object object)
Returns a string representation of the given object in the default locale. |
java.lang.String |
getFormat(java.lang.Object object,
java.util.Locale locale)
Returns a string representation of the given object in the given locale. |
static java.lang.String |
getFormatterClassForType(java.lang.String type)
Returns the formatter class to use for the given type. |
static Formatter |
getFormatterForObject(java.lang.Object object)
Returns a formatter instance for the given object based on its type. |
static Formatter |
getFormatterForType(java.lang.String type)
Returns a formatter instance for the given type. |
java.util.Locale |
getLocale()
Returns the locale associated with this formatter. |
static java.lang.String |
getStringValue(java.lang.Object object)
Returns a formatted string for the given object in the default locale. |
static java.lang.String |
getStringValue(java.lang.Object object,
java.util.Locale locale)
Returns a formatted string for the given object in the given locale. |
java.lang.Object |
getValue(java.lang.Object object)
Returns an object of the appropriate type for this formatter derived from the given object. |
abstract java.lang.Object |
getValue(java.lang.Object object,
java.util.Locale locale)
Returns an object of the appropriate type for this formatter based on the given object. |
boolean |
handlesNulls()
Check whether this class handles null values. |
boolean |
isBidirectional()
Check whether this formatter guarantees text->object conversion. All formatters can convert from object to string, but not all can do the reverse conversion. |
static java.text.ParseException |
makeParseException(java.lang.String errorKey,
int offset)
Convenience routine to get a ParseException with a localized message. |
static java.text.ParseException |
makeParseException(java.lang.String errorKey,
java.lang.String argument,
int offset)
Convenience routine to get a ParseException with a localized message. |
static java.text.ParseException |
makeParseException(java.lang.String errorKey,
java.lang.String argument,
int offset,
java.util.Locale locale)
|
static java.lang.String |
makeParseExceptionMessage(java.lang.String errorKey)
Convenience routine to get a localized message for a ParseException. |
static java.lang.String |
makeParseExceptionMessage(java.lang.String errorKey,
java.lang.String argument)
Convenience routine to get a localized message for a ParseException. |
static boolean |
objectsAreEqual(java.lang.Object o1,
java.lang.Object o2)
Returns true if and only if the two objects should be considered equal in the default locale. |
static boolean |
objectsAreEqual(java.lang.Object o1,
java.lang.Object o2,
java.util.Locale locale)
Returns true if and only if the two objects should be considered equal in the given locale. |
java.lang.Object |
parse(java.lang.String string)
Tries to parse the given string into an object of the appropriate type for this formatter. |
java.lang.Object |
parse(java.lang.String string,
java.util.Locale locale)
Tries to parse the given string into an object of the appropriate type for this formatter. |
static java.lang.Object |
parseString(java.lang.String string,
java.lang.String type)
Tries to parse a string to create an object of the given type using the default locale. |
static java.lang.Object |
parseString(java.lang.String string,
java.lang.String type,
java.util.Locale locale)
Tries to parse a string to create an object of the given type using the given locale. |
int |
quickCompare(java.lang.Object o1,
java.lang.Object o2)
Compares two objects for sorting purposes in the default locale. |
int |
quickCompare(java.lang.Object o1,
java.lang.Object o2,
java.util.Locale locale)
Compares two objects for sorting purposes in the given locale. |
java.lang.Object |
quickCompareValue(java.lang.Object object,
java.lang.Object previous)
Returns a converted value for the given object to be used by the quickCompare method above. |
static void |
registerFormatter(java.lang.String type,
java.lang.String className)
Registers the given formatter class name to be associated with the give type. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Formatter()
Formatter
. Formatters are stateless and
thus can be cached for use on multiple values of the same type.
Method Detail |
---|
public static java.lang.String getStringValue(java.lang.Object object)
toString
on the object and returns the resulting string.
object
- the object to format into a string
public static java.lang.String getStringValue(java.lang.Object object, java.util.Locale locale)
toString
on the object and returns the resulting string.
object
- the object to format into a stringlocale
- the Locale
to use for formatting
public static java.lang.Object parseString(java.lang.String string, java.lang.String type) throws java.text.ParseException
type
cannot be created.
string
- the string to parse into an objecttype
- the Java type of the resulting object
type
java.text.ParseException
- if the string can't be parsed to create
an object of the given type
public static java.lang.Object parseString(java.lang.String string, java.lang.String type, java.util.Locale locale) throws java.text.ParseException
type
cannot be created.
string
- the string to parse into an objecttype
- the Java type of the resulting objectlocale
- the Locale
to use for parsing
type
java.text.ParseException
- if the string can't be parsed to create
an object of the given type
public static boolean objectsAreEqual(java.lang.Object o1, java.lang.Object o2)
If the first object is non-null, retrieves a formatter instance based
on its type; otherwise, uses the second object. If a formatter can't
be found for either object, or the types of the two objects don't
match, resorts to the equals
method on the first non-null
object. If both objects are null, returns true.
o1
- the first object to test for equalityo2
- the second object to test for equality
true
if the two objects are equal;
false
otherwise.public static boolean objectsAreEqual(java.lang.Object o1, java.lang.Object o2, java.util.Locale locale)
If the first object is non-null, retrieves a formatter instance based
on its type; otherwise, uses the second object. If a formatter can't
be found for either object, or the types of the two objects don't
match, resorts to the equals
method on the first non-null
object. If both objects are null, returns true.
o1
- the first object to test for equalityo2
- the second object to test for equalitylocale
- the Locale
to use for equality testing
true
if the two objects are equal;
false
otherwise.public static Formatter getFormatterForObject(java.lang.Object object)
object
- the object for which a formatter is required
public static Formatter getFormatterForType(java.lang.String type)
type
- the type for which a formatter is required
public static java.lang.String getFormatterClassForType(java.lang.String type)
type
- The fully qualified type name whose formatter we are finding
public static void registerFormatter(java.lang.String type, java.lang.String className)
type
- the object type for the given formatter classclassName
- the class of formatter to use for the given typepublic static java.lang.String makeParseExceptionMessage(java.lang.String errorKey)
errorKey
- - the key to the error message
public static java.lang.String makeParseExceptionMessage(java.lang.String errorKey, java.lang.String argument)
errorKey
- - the key to the error messageargument
- - an argument to the error key
public static java.text.ParseException makeParseException(java.lang.String errorKey, int offset)
errorKey
- - the key to the error messageoffset
- - the offset in the string being parsed
public static java.text.ParseException makeParseException(java.lang.String errorKey, java.lang.String argument, int offset)
errorKey
- - the key to the error messageargument
- - an argument to the error keyoffset
- - the offset in the string being parsed
public static java.text.ParseException makeParseException(java.lang.String errorKey, java.lang.String argument, int offset, java.util.Locale locale)
public java.util.Locale getLocale()
public boolean isBidirectional()
public boolean canFormatNulls()
public final java.lang.String getFormat(java.lang.Object object)
object
- the object to format as a string
public final java.lang.String getFormat(java.lang.Object object, java.util.Locale locale)
object
- the object to format as a stringlocale
- the Locale
to use for formatting
public final boolean handlesNulls()
public final java.lang.Object parse(java.lang.String string) throws java.text.ParseException
parse
in interface StringParser
string
- the string to parse
java.text.ParseException
- if the string can't be parsed to create
an object the appropriate typepublic final java.lang.Object parse(java.lang.String string, java.util.Locale locale) throws java.text.ParseException
parse
in interface StringParser
string
- the string to parselocale
- the Locale
to use for parsing
java.text.ParseException
- if the string can't be parsed to create
an object the appropriate typepublic final java.lang.Object getValue(java.lang.Object object)
The type of the given object can by anything; it is up to each specific implementation to do the appropriate conversion, parsing, etc. to create a value of the appropriate type.
The return value may be null depending on the specific implementation for a given formatter.
getValue
in interface StringParser
object
- the object to convert to the type for this formatter
public abstract java.lang.Object getValue(java.lang.Object object, java.util.Locale locale)
The type of the given object can by anything; it is up to each specific implementation to do the appropriate conversion, parsing, etc. to create a value of the appropriate type.
The return value may be null depending on the specific implementation for a given formatter.
Subclasses must define this method to provide type-specific conversion.
object
- the object to convert to the type for this formatterlocale
- the locale used when converting the object
public final boolean equal(java.lang.Object o1, java.lang.Object o2)
objectsEqual
to determine if the two (non-null) objects
are equal.
o1
- the first object to test for equalityo2
- the second object to test for equality
true
if the two objects are equal;
false
otherwise.public final boolean equal(java.lang.Object o1, java.lang.Object o2, java.util.Locale locale)
objectsEqual
to determine if the two (non-null) objects
are equal.
o1
- the first object to test for equalityo2
- the second object to test for equalitylocale
- the Locale
to use for equality testing
true
if the two objects are equal;
false
otherwise.public final int compare(java.lang.Object o1, java.lang.Object o2)
int
value which is less than, equal to, or
greater than zero depending on whether the first object sorts before,
the same, or after the second object.
Returns zero if the two objects are, in fact, the same object or both
are null. Otherwise, arbitrarily returns -1 if the first object is
null, or 1 if the second is null. Otherwise, calls the protected
method compareObjects
to compare the two (non-null)
objects.
compare
in interface Compare
o1
- the first object to compareo2
- the second object to compare
int
value which determines how the two
objects should be orderedpublic final int compare(java.lang.Object o1, java.lang.Object o2, java.util.Locale locale)
int
value which is less than, equal to, or
greater than zero depending on whether the first object sorts before,
the same, or after the second object.
Returns zero if the two objects are, in fact, the same object or both
are null. Otherwise, arbitrarily returns -1 if the first object is
null, or 1 if the second is null. Otherwise, calls the protected
method compareObjects
to compare the two (non-null)
objects.
o1
- the first object to compareo2
- the second object to comparelocale
- the Locale
to use for comparison
int
value which determines how the two
objects should be orderedpublic final int quickCompare(java.lang.Object o1, java.lang.Object o2)
This method is a performance optimzation for sorting. If comparing
two values of a given type involves some conversion or processing that
is too expensive to do for each comparison, this method and the
quickCompareValue
method below can be used to precompute
the values to use for sorting. Any code which needs to sort these
types should call this method rather than compare
public final int quickCompare(java.lang.Object o1, java.lang.Object o2, java.util.Locale locale)
This method is a performance optimzation for sorting. If comparing
two values of a given type involves some conversion or processing that
is too expensive to do for each comparison, this method and the
quickCompareValue
method below can be used to precompute
the values to use for sorting. Any code which needs to sort these
types should call this method rather than compare
public java.lang.Object quickCompareValue(java.lang.Object object, java.lang.Object previous)
quickCompare
method above. The previous object is the
previous value that was passed to quickCompareValue
(pre-converted).
The default implementation is to just return object itself.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |