|
|||||||||
| 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(Object o1,
Object o2)
Compares two objects for sorting purposes in the default locale. |
int |
compare(Object o1,
Object o2,
Locale locale)
Compares two objects for sorting purposes in the given locale. |
boolean |
equal(Object o1,
Object o2)
Returns true if and only if the two objects should be considered equal in the default locale. |
boolean |
equal(Object o1,
Object o2,
Locale locale)
Returns true if and only if the two objects should be considered equal in the given locale. |
String |
getFormat(Object object)
Returns a string representation of the given object in the default locale. |
String |
getFormat(Object object,
Locale locale)
Returns a string representation of the given object in the given locale. |
static String |
getFormatterClassForType(String type)
Returns the formatter class to use for the given type. |
static Formatter |
getFormatterForObject(Object object)
Returns a formatter instance for the given object based on its type. |
static Formatter |
getFormatterForType(String type)
Returns a formatter instance for the given type. |
Locale |
getLocale()
Returns the locale associated with this formatter. |
static String |
getStringValue(Object object)
Returns a formatted string for the given object in the default locale. |
static String |
getStringValue(Object object,
Locale locale)
Returns a formatted string for the given object in the given locale. |
Object |
getValue(Object object)
Returns an object of the appropriate type for this formatter derived from the given object. |
abstract Object |
getValue(Object object,
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 ParseException |
makeParseException(String errorKey,
int offset)
Convenience routine to get a ParseException with a localized message. |
static ParseException |
makeParseException(String errorKey,
String argument,
int offset)
Convenience routine to get a ParseException with a localized message. |
static boolean |
objectsAreEqual(Object o1,
Object o2)
Returns true if and only if the two objects should be considered equal in the default locale. |
static boolean |
objectsAreEqual(Object o1,
Object o2,
Locale locale)
Returns true if and only if the two objects should be considered equal in the given locale. |
Object |
parse(String string)
Tries to parse the given string into an object of the appropriate type for this formatter. |
Object |
parse(String string,
Locale locale)
Tries to parse the given string into an object of the appropriate type for this formatter. |
static Object |
parseString(String string,
String type)
Tries to parse a string to create an object of the given type using the default locale. |
static Object |
parseString(String string,
String type,
Locale locale)
Tries to parse a string to create an object of the given type using the given locale. |
int |
quickCompare(Object o1,
Object o2)
Compares two objects for sorting purposes in the default locale. |
int |
quickCompare(Object o1,
Object o2,
Locale locale)
Compares two objects for sorting purposes in the given locale. |
Object |
quickCompareValue(Object object,
Object previous)
Returns a converted value for the given object to be used by the quickCompare method above. |
static void |
registerFormatter(String type,
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 String getStringValue(Object object)
toString on the object and returns the resulting string.
object - the object to format into a string
public static String getStringValue(Object object,
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 Object parseString(String string,
String type)
throws ParseException
type cannot be created.
string - the string to parse into an objecttype - the Java type of the resulting object
type
ParseException - if the string can't be parsed to create
an object of the given type
public static Object parseString(String string,
String type,
Locale locale)
throws 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
ParseException - if the string can't be parsed to create
an object of the given type
public static boolean objectsAreEqual(Object o1,
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(Object o1,
Object o2,
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(Object object)
object - the object for which a formatter is required
public static Formatter getFormatterForType(String type)
type - the type for which a formatter is required
public static String getFormatterClassForType(String type)
type - The fully qualified type name whose formatter we are finding
public static void registerFormatter(String type,
String className)
type - the object type for the given formatter classclassName - the class of formatter to use for the given type
public static ParseException makeParseException(String errorKey,
int offset)
errorKey - - the key to the error messageoffset - - the offset in the string being parsed
public static ParseException makeParseException(String errorKey,
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 Locale getLocale()
public boolean isBidirectional()
public boolean canFormatNulls()
public final String getFormat(Object object)
object - the object to format as a string
public final String getFormat(Object object,
Locale locale)
object - the object to format as a stringlocale - the Locale to use for formatting
public final boolean handlesNulls()
public final Object parse(String string)
throws ParseException
parse in interface StringParserstring - the string to parse
ParseException - if the string can't be parsed to create
an object the appropriate type
public final Object parse(String string,
Locale locale)
throws ParseException
parse in interface StringParserstring - the string to parselocale - the Locale to use for parsing
ParseException - if the string can't be parsed to create
an object the appropriate typepublic final Object getValue(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 StringParserobject - the object to convert to the type for this formatter
public abstract Object getValue(Object object,
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(Object o1,
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(Object o1,
Object o2,
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(Object o1,
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 Compareo1 - the first object to compareo2 - the second object to compare
int value which determines how the two
objects should be ordered
public final int compare(Object o1,
Object o2,
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 ordered
public final int quickCompare(Object o1,
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(Object o1,
Object o2,
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 Object quickCompareValue(Object object,
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 | ||||||||