java.lang.Object
com.aoapps.lang.Coercion
Coerces objects to String compatible with JSP Expression Language (JSP EL)
and the Java Standard Taglib (JSTL). Also adds support for seamless output
of XML DOM nodes.
- Author:
- AO Industries, Inc.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
append
(Object value, Encoder encoder, Appendable out) Encodes an object's String representation, supporting streaming for specialized types.static void
append
(Object value, Encoder encoder, Appendable out, boolean outOptimized) Encodes an object's String representation, supporting streaming for specialized types.static void
append
(Object value, Appendable out) Appends an object's String representation, supporting streaming for specialized types.static void
append
(Object value, Appendable out, boolean outOptimized) Appends an object's String representation, supporting streaming for specialized types.static boolean
Checks if a value is null or empty.static Object
nullIfEmpty
(Object value) Returns the provided value (possibly converted to a different form, like String) or null if the value is empty.static Writer
Optimizes the given writer by passing throughCoercionOptimizer.optimize(java.io.Writer, com.aoapps.lang.io.Encoder)
on all registered coercion optimizers until there are no replacements.static Appendable
optimize
(Appendable out, Encoder encoder) Optimizes the given appendable by dispatching tooptimize(java.io.Writer, com.aoapps.lang.io.Encoder)
when it is aWriter
.static void
registerOptimizer
(CoercionOptimizer optimizer) Registers a new coercion optimizer.static String
Converts an object to a string.static Object
Returns the provided value trimmed, as per rules ofStrings.isWhitespace(int)
.static Object
trimNullIfEmpty
(Object value) Returns the provided value trimmed, as per rules ofStrings.isWhitespace(int)
, ornull
if the value is empty after trimming.static void
Encodes an object's String representation, supporting streaming for specialized types.static void
Encodes an object's String representation, supporting streaming for specialized types.static void
Writes an object's String representation, supporting streaming for specialized types.static void
Writes an object's String representation, supporting streaming for specialized types.
-
Method Details
-
toString
Converts an object to a string.- Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
return""
. - When
String
return directly. - When
Writable
returnWritable.toString()
. - When
Segment
orCharSequence
returnCharSequence.toString()
. - When
char[]
return""
when empty orString(char[])
. - When
Node
serialize the output asStandardCharsets.UTF_8
. - Otherwise return
value.toString()
.
- Any
-
registerOptimizer
Registers a new coercion optimizer. -
optimize
Optimizes the given writer by passing throughCoercionOptimizer.optimize(java.io.Writer, com.aoapps.lang.io.Encoder)
on all registered coercion optimizers until there are no replacements. -
optimize
Optimizes the given appendable by dispatching tooptimize(java.io.Writer, com.aoapps.lang.io.Encoder)
when it is aWriter
.There is currently no implementation of appendable-specific unwrapping, but this is here for consistency with the writer unwrapping.
- See Also:
-
write
Writes an object's String representation, supporting streaming for specialized types.- Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
do not write. - When
EncoderWriter
unwrap and dispatch towrite(java.lang.Object, com.aoapps.lang.io.Encoder, java.io.Writer)
. - When
String
write directly. - When
Writable
writeWritable.toString()
whenWritable.isFastToString()
or dispatch toWritable.writeTo(java.io.Writer)
. - When
Segment
writeSegment.array
. - When
CharSequence
append directly. - When
char[]
write directly. - When
Node
serialize the output asStandardCharsets.UTF_8
. - Otherwise write
value.toString()
.
- Throws:
IOException
- Any
-
write
Writes an object's String representation, supporting streaming for specialized types.- Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
do not write. - When
EncoderWriter
unwrap and dispatch towrite(java.lang.Object, com.aoapps.lang.io.Encoder, java.io.Writer, boolean)
. - When
String
write directly. - When
Writable
writeWritable.toString()
whenWritable.isFastToString()
or dispatch toWritable.writeTo(java.io.Writer)
. - When
Segment
writeSegment.array
. - When
CharSequence
append directly. - When
char[]
write directly. - When
Node
serialize the output asStandardCharsets.UTF_8
. - Otherwise write
value.toString()
.
- Parameters:
outOptimized
- Isout
already known to have been passed throughoptimize(java.io.Writer, com.aoapps.lang.io.Encoder)
(withencoder = null
)?- Throws:
IOException
- Any
-
write
Encodes an object's String representation, supporting streaming for specialized types.- When
encoder == null
dispatch towrite(java.lang.Object, java.io.Writer)
. - Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
do not encode. - When
String
encode directly. - When
Writable
encodeWritable.toString()
whenWritable.isFastToString()
or dispatch toWritable.writeTo(com.aoapps.lang.io.Encoder, java.io.Writer)
. - When
Segment
encodeSegment.array
. - When
CharSequence
encode directly. - When
char[]
encode directly. - When
Node
serialize the output asStandardCharsets.UTF_8
while encoding throughEncoderWriter
. - Otherwise encode
value.toString()
.
- Parameters:
encoder
- if null, no encoding is performed - write through- Throws:
IOException
- When
-
write
public static void write(Object value, Encoder encoder, Writer out, boolean outOptimized) throws IOException Encodes an object's String representation, supporting streaming for specialized types.- When
encoder == null
dispatch towrite(java.lang.Object, java.io.Writer, boolean)
. - Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
do not encode. - When
String
encode directly. - When
Writable
encodeWritable.toString()
whenWritable.isFastToString()
or dispatch toWritable.writeTo(com.aoapps.lang.io.Encoder, java.io.Writer)
. - When
Segment
encodeSegment.array
. - When
CharSequence
encode directly. - When
char[]
encode directly. - When
Node
serialize the output asStandardCharsets.UTF_8
while encoding throughEncoderWriter
. - Otherwise encode
value.toString()
.
- Parameters:
encoder
- if null, no encoding is performed - write throughoutOptimized
- Isout
already known to have been passed throughoptimize(java.io.Writer, com.aoapps.lang.io.Encoder)
?- Throws:
IOException
- When
-
append
Appends an object's String representation, supporting streaming for specialized types.- When
out
is aWriter
dispatch towrite(java.lang.Object, java.io.Writer)
. - Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
do not append. - When
String
append directly. - When
Writable
appendWritable.toString()
whenWritable.isFastToString()
or dispatch toWritable.appendTo(java.lang.Appendable)
. - When
Segment
orCharSequence
append directly. - When
char[]
append wrapped in newSegment
. - When
Node
serialize the output asStandardCharsets.UTF_8
. - Otherwise append
value.toString()
.
- Throws:
IOException
- When
-
append
Appends an object's String representation, supporting streaming for specialized types.- When
out
is aWriter
dispatch towrite(java.lang.Object, java.io.Writer, boolean)
. - Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
do not append. - When
String
append directly. - When
Writable
appendWritable.toString()
whenWritable.isFastToString()
or dispatch toWritable.appendTo(java.lang.Appendable)
. - When
Segment
orCharSequence
append directly. - When
char[]
append wrapped in newSegment
. - When
Node
serialize the output asStandardCharsets.UTF_8
. - Otherwise append
value.toString()
.
- Parameters:
outOptimized
- Isout
already known to have been passed throughoptimize(java.io.Writer, com.aoapps.lang.io.Encoder)
(withencoder = null
)?- Throws:
IOException
- When
-
append
Encodes an object's String representation, supporting streaming for specialized types.- When
encoder == null
dispatch toappend(java.lang.Object, java.lang.Appendable)
. - When
out
is aWriter
dispatch towrite(java.lang.Object, com.aoapps.lang.io.Encoder, java.io.Writer)
. - Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
do not encode. - When
String
encode directly. - When
Writable
encodeWritable.toString()
whenWritable.isFastToString()
or dispatch toWritable.appendTo(com.aoapps.lang.io.Encoder, java.lang.Appendable)
. - When
Segment
orCharSequence
encode directly. - When
char[]
encode wrapped in newSegment
. - When
Node
serialize the output asStandardCharsets.UTF_8
while encoding throughEncoderWriter
andAppendableWriter
. - Otherwise encode
value.toString()
.
- Parameters:
encoder
- if null, no encoding is performed - write through- Throws:
IOException
- When
-
append
public static void append(Object value, Encoder encoder, Appendable out, boolean outOptimized) throws IOException Encodes an object's String representation, supporting streaming for specialized types.- When
encoder == null
dispatch toappend(java.lang.Object, java.lang.Appendable, boolean)
. - When
out
is aWriter
dispatch towrite(java.lang.Object, com.aoapps.lang.io.Encoder, java.io.Writer, boolean)
. - Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
do not encode. - When
String
encode directly. - When
Writable
encodeWritable.toString()
whenWritable.isFastToString()
or dispatch toWritable.appendTo(com.aoapps.lang.io.Encoder, java.lang.Appendable)
. - When
Segment
orCharSequence
encode directly. - When
char[]
encode wrapped in newSegment
. - When
Node
serialize the output asStandardCharsets.UTF_8
while encoding throughEncoderWriter
andAppendableWriter
. - Otherwise encode
value.toString()
.
- Parameters:
encoder
- if null, no encoding is performed - write throughoutOptimized
- Isout
already known to have been passed throughoptimize(java.io.Writer, com.aoapps.lang.io.Encoder)
?- Throws:
IOException
- When
-
isEmpty
Checks if a value is null or empty.- Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
returntrue
. - When
String
returnString.isEmpty()
. - When
Writable
returnWritable.getLength() == 0
. - When
Segment
orCharSequence
returnCharSequence.length() == 0
. - When
char[]
returnvalue.length == 0
. - When
Node
returnfalse
. - Otherwise return
value.toString().isEmpty()
.
- Throws:
UncheckedIOException
- on any underlyingIOException
.
- Any
-
nullIfEmpty
Returns the provided value (possibly converted to a different form, like String) or null if the value is empty.- Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
returnnull
. - When
String
returnnull
whenString.isEmpty()
. - When
Writable
returnnull
whenWritable.getLength() == 0
. - When
Segment
orCharSequence
returnnull
whenCharSequence.length() == 0
. - When
char[]
returnnull
whenvalue.length == 0
. - When
Node
returnvalue
. - Otherwise return
Strings.nullIfEmpty(value.toString())
.
- Throws:
UncheckedIOException
- on any underlyingIOException
.- See Also:
- Any
-
trim
Returns the provided value trimmed, as per rules ofStrings.isWhitespace(int)
.- Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
returnnull
. - When
String
returnStrings.trim(java.lang.String)
. - When
Writable
returnStrings.trim(java.lang.String)
whenWritable.isFastToString()
otherwiseWritable.trim()
. - When
Segment
orCharSequence
returnStrings.trim(java.lang.CharSequence)
. - When
char[]
return""
whenvalue.length == 0
orStrings.trim(new Segment(value, 0, value.length)
) thenvalue
if nothing trimmed. - When
Node
returnvalue
. - Otherwise return
Strings.trim(value.toString())
.
- Returns:
- The original value (possibly of a different type even when nothing to trim),
a trimmed version of the value (possibly of a different type),
a trimmed
String
representation of the object, ornull
when the value isnull
. - Throws:
UncheckedIOException
- on any underlyingIOException
.
- Any
-
trimNullIfEmpty
Returns the provided value trimmed, as per rules ofStrings.isWhitespace(int)
, ornull
if the value is empty after trimming.- Any
Optional
is unwrapped (supporting any levels of nesting). - When
null
returnnull
. - When
String
returnStrings.trimNullIfEmpty(java.lang.String)
. - When
Writable
returnStrings.trimNullIfEmpty(java.lang.String)
whenWritable.isFastToString()
otherwiseWritable.trim()
thennull
if empty after trimming. - When
Segment
orCharSequence
returnStrings.trimNullIfEmpty(java.lang.CharSequence)
. - When
char[]
returnnull
whenvalue.length == 0
orStrings.trimNullIfEmpty(new Segment(value, 0, value.length)
) thenvalue
if nothing trimmed. - When
Node
returnvalue
. - Otherwise return
Strings.trimNullIfEmpty(value.toString())
.
- Returns:
- The original value (possibly of a different type even when nothing to trim),
a trimmed version of the value (possibly of a different type),
a trimmed
String
representation of the object, ornull
when the value isnull
or empty after trimming. - Throws:
UncheckedIOException
- on any underlyingIOException
.
- Any
-