- java.lang.Object
-
- com.aoapps.lang.Coercion
-
public final class Coercion extends Object
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
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidappend(Object value, Encoder encoder, Appendable out)Encodes an object's String representation, supporting streaming for specialized types.static voidappend(Object value, Encoder encoder, Appendable out, boolean outOptimized)Encodes an object's String representation, supporting streaming for specialized types.static voidappend(Object value, Appendable out)Appends an object's String representation, supporting streaming for specialized types.static voidappend(Object value, Appendable out, boolean outOptimized)Appends an object's String representation, supporting streaming for specialized types.static booleanisEmpty(Object value)Checks if a value is null or empty.static ObjectnullIfEmpty(Object value)Returns the provided value (possibly converted to a different form, like String) or null if the value is empty.static Writeroptimize(Writer out, Encoder encoder)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 Appendableoptimize(Appendable out, Encoder encoder)Optimizes the given appendable by dispatching tooptimize(java.io.Writer, com.aoapps.lang.io.Encoder)when it is aWriter.static voidregisterOptimizer(CoercionOptimizer optimizer)Registers a new coercion optimizer.static StringtoString(Object value)Converts an object to a string.static Objecttrim(Object value)Returns the provided value trimmed, as per rules ofStrings.isWhitespace(int).static ObjecttrimNullIfEmpty(Object value)Returns the provided value trimmed, as per rules ofStrings.isWhitespace(int), ornullif the value is empty after trimming.static voidwrite(Object value, Encoder encoder, Writer out)Encodes an object's String representation, supporting streaming for specialized types.static voidwrite(Object value, Encoder encoder, Writer out, boolean outOptimized)Encodes an object's String representation, supporting streaming for specialized types.static voidwrite(Object value, Writer out)Writes an object's String representation, supporting streaming for specialized types.static voidwrite(Object value, Writer out, boolean outOptimized)Writes an object's String representation, supporting streaming for specialized types.
-
-
-
Method Detail
-
toString
public static String toString(Object value)
Converts an object to a string.- Any
Optionalis unwrapped (supporting any levels of nesting). - When
nullreturn"". - When
Stringreturn directly. - When
WritablereturnWritable.toString(). - When
SegmentorCharSequencereturnCharSequence.toString(). - When
char[]return""when empty orString(char[]). - When
Nodeserialize the output asStandardCharsets.UTF_8. - Otherwise return
value.toString().
- Any
-
registerOptimizer
public static void registerOptimizer(CoercionOptimizer optimizer)
Registers a new coercion optimizer.
-
optimize
public static Writer optimize(Writer out, Encoder encoder)
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
public 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.There is currently no implementation of appendable-specific unwrapping, but this is here for consistency with the writer unwrapping.
-
write
public static void write(Object value, Writer out) throws IOException
Writes an object's String representation, supporting streaming for specialized types.- Any
Optionalis unwrapped (supporting any levels of nesting). - When
nulldo not write. - When
EncoderWriterunwrap and dispatch towrite(java.lang.Object, com.aoapps.lang.io.Encoder, java.io.Writer). - When
Stringwrite directly. - When
WritablewriteWritable.toString()whenWritable.isFastToString()or dispatch toWritable.writeTo(java.io.Writer). - When
SegmentwriteSegment.array. - When
CharSequenceappend directly. - When
char[]write directly. - When
Nodeserialize the output asStandardCharsets.UTF_8. - Otherwise write
value.toString().
- Throws:
IOException
- Any
-
write
public static void write(Object value, Writer out, boolean outOptimized) throws IOException
Writes an object's String representation, supporting streaming for specialized types.- Any
Optionalis unwrapped (supporting any levels of nesting). - When
nulldo not write. - When
EncoderWriterunwrap and dispatch towrite(java.lang.Object, com.aoapps.lang.io.Encoder, java.io.Writer, boolean). - When
Stringwrite directly. - When
WritablewriteWritable.toString()whenWritable.isFastToString()or dispatch toWritable.writeTo(java.io.Writer). - When
SegmentwriteSegment.array. - When
CharSequenceappend directly. - When
char[]write directly. - When
Nodeserialize the output asStandardCharsets.UTF_8. - Otherwise write
value.toString().
- Parameters:
outOptimized- Isoutalready known to have been passed throughoptimize(java.io.Writer, com.aoapps.lang.io.Encoder)(withencoder = null)?- Throws:
IOException
- Any
-
write
public static void write(Object value, Encoder encoder, Writer out) throws IOException
Encodes an object's String representation, supporting streaming for specialized types.- When
encoder == nulldispatch towrite(java.lang.Object, java.io.Writer). - Any
Optionalis unwrapped (supporting any levels of nesting). - When
nulldo not encode. - When
Stringencode directly. - When
WritableencodeWritable.toString()whenWritable.isFastToString()or dispatch toWritable.writeTo(com.aoapps.lang.io.Encoder, java.io.Writer). - When
SegmentencodeSegment.array. - When
CharSequenceencode directly. - When
char[]encode directly. - When
Nodeserialize the output asStandardCharsets.UTF_8while 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 == nulldispatch towrite(java.lang.Object, java.io.Writer, boolean). - Any
Optionalis unwrapped (supporting any levels of nesting). - When
nulldo not encode. - When
Stringencode directly. - When
WritableencodeWritable.toString()whenWritable.isFastToString()or dispatch toWritable.writeTo(com.aoapps.lang.io.Encoder, java.io.Writer). - When
SegmentencodeSegment.array. - When
CharSequenceencode directly. - When
char[]encode directly. - When
Nodeserialize the output asStandardCharsets.UTF_8while encoding throughEncoderWriter. - Otherwise encode
value.toString().
- Parameters:
encoder- if null, no encoding is performed - write throughoutOptimized- Isoutalready known to have been passed throughoptimize(java.io.Writer, com.aoapps.lang.io.Encoder)?- Throws:
IOException
- When
-
append
public static void append(Object value, Appendable out) throws IOException
Appends an object's String representation, supporting streaming for specialized types.- When
outis aWriterdispatch towrite(java.lang.Object, java.io.Writer). - Any
Optionalis unwrapped (supporting any levels of nesting). - When
nulldo not append. - When
Stringappend directly. - When
WritableappendWritable.toString()whenWritable.isFastToString()or dispatch toWritable.appendTo(java.lang.Appendable). - When
SegmentorCharSequenceappend directly. - When
char[]append wrapped in newSegment. - When
Nodeserialize the output asStandardCharsets.UTF_8. - Otherwise append
value.toString().
- Throws:
IOException
- When
-
append
public static void append(Object value, Appendable out, boolean outOptimized) throws IOException
Appends an object's String representation, supporting streaming for specialized types.- When
outis aWriterdispatch towrite(java.lang.Object, java.io.Writer, boolean). - Any
Optionalis unwrapped (supporting any levels of nesting). - When
nulldo not append. - When
Stringappend directly. - When
WritableappendWritable.toString()whenWritable.isFastToString()or dispatch toWritable.appendTo(java.lang.Appendable). - When
SegmentorCharSequenceappend directly. - When
char[]append wrapped in newSegment. - When
Nodeserialize the output asStandardCharsets.UTF_8. - Otherwise append
value.toString().
- Parameters:
outOptimized- Isoutalready known to have been passed throughoptimize(java.io.Writer, com.aoapps.lang.io.Encoder)(withencoder = null)?- Throws:
IOException
- When
-
append
public static void append(Object value, Encoder encoder, Appendable out) throws IOException
Encodes an object's String representation, supporting streaming for specialized types.- When
encoder == nulldispatch toappend(java.lang.Object, java.lang.Appendable). - When
outis aWriterdispatch towrite(java.lang.Object, com.aoapps.lang.io.Encoder, java.io.Writer). - Any
Optionalis unwrapped (supporting any levels of nesting). - When
nulldo not encode. - When
Stringencode directly. - When
WritableencodeWritable.toString()whenWritable.isFastToString()or dispatch toWritable.appendTo(com.aoapps.lang.io.Encoder, java.lang.Appendable). - When
SegmentorCharSequenceencode directly. - When
char[]encode wrapped in newSegment. - When
Nodeserialize the output asStandardCharsets.UTF_8while encoding throughEncoderWriterandAppendableWriter. - 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 == nulldispatch toappend(java.lang.Object, java.lang.Appendable, boolean). - When
outis aWriterdispatch towrite(java.lang.Object, com.aoapps.lang.io.Encoder, java.io.Writer, boolean). - Any
Optionalis unwrapped (supporting any levels of nesting). - When
nulldo not encode. - When
Stringencode directly. - When
WritableencodeWritable.toString()whenWritable.isFastToString()or dispatch toWritable.appendTo(com.aoapps.lang.io.Encoder, java.lang.Appendable). - When
SegmentorCharSequenceencode directly. - When
char[]encode wrapped in newSegment. - When
Nodeserialize the output asStandardCharsets.UTF_8while encoding throughEncoderWriterandAppendableWriter. - Otherwise encode
value.toString().
- Parameters:
encoder- if null, no encoding is performed - write throughoutOptimized- Isoutalready known to have been passed throughoptimize(java.io.Writer, com.aoapps.lang.io.Encoder)?- Throws:
IOException
- When
-
isEmpty
public static boolean isEmpty(Object value) throws IOException
Checks if a value is null or empty.- Any
Optionalis unwrapped (supporting any levels of nesting). - When
nullreturntrue. - When
StringreturnString.isEmpty(). - When
WritablereturnWritable.getLength() == 0. - When
SegmentorCharSequencereturnCharSequence.length() == 0. - When
char[]returnvalue.length == 0. - When
Nodereturnfalse. - Otherwise return
value.toString().isEmpty().
- Throws:
IOException
- Any
-
nullIfEmpty
public static Object nullIfEmpty(Object value) throws IOException
Returns the provided value (possibly converted to a different form, like String) or null if the value is empty.- Any
Optionalis unwrapped (supporting any levels of nesting). - When
nullreturnnull. - When
StringreturnnullwhenString.isEmpty(). - When
WritablereturnnullwhenWritable.getLength() == 0. - When
SegmentorCharSequencereturnnullwhenCharSequence.length() == 0. - When
char[]returnnullwhenvalue.length == 0. - When
Nodereturnvalue. - Otherwise return
Strings.nullIfEmpty(value.toString()).
- Throws:
IOException- See Also:
isEmpty(java.lang.Object)
- Any
-
trim
public static Object trim(Object value) throws IOException
Returns the provided value trimmed, as per rules ofStrings.isWhitespace(int).- Any
Optionalis unwrapped (supporting any levels of nesting). - When
nullreturnnull. - When
StringreturnStrings.trim(java.lang.String). - When
WritablereturnStrings.trim(java.lang.String)whenWritable.isFastToString()otherwiseWritable.trim(). - When
SegmentorCharSequencereturnStrings.trim(java.lang.CharSequence). - When
char[]return""whenvalue.length == 0orStrings.trim(new Segment(value, 0, value.length)) thenvalueif nothing trimmed. - When
Nodereturnvalue. - 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
Stringrepresentation of the object, ornullwhen the value isnull. - Throws:
IOException
- Any
-
trimNullIfEmpty
public static Object trimNullIfEmpty(Object value) throws IOException
Returns the provided value trimmed, as per rules ofStrings.isWhitespace(int), ornullif the value is empty after trimming.- Any
Optionalis unwrapped (supporting any levels of nesting). - When
nullreturnnull. - When
StringreturnStrings.trimNullIfEmpty(java.lang.String). - When
WritablereturnStrings.trimNullIfEmpty(java.lang.String)whenWritable.isFastToString()otherwiseWritable.trim()thennullif empty after trimming. - When
SegmentorCharSequencereturnStrings.trimNullIfEmpty(java.lang.CharSequence). - When
char[]returnnullwhenvalue.length == 0orStrings.trimNullIfEmpty(new Segment(value, 0, value.length)) thenvalueif nothing trimmed. - When
Nodereturnvalue. - 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
Stringrepresentation of the object, ornullwhen the value isnullor empty after trimming. - Throws:
IOException
- Any
-
-