Invalid Java MessageFormat Values
What is the best way to handle null values ββin Java MessageFormat
MessageFormat.format("Value: {0}",null); => Value: null but actually "Value:" would be nice.
Same with date
MessageFormat.format("Value: {0,date,medium}",null); => Value: null a "Value:" to be much more appreciated.
Is there any way to do this? I tried the choice
{0,choice,null#|notnull#{0,date,dd.MM.yyyy β HH:mm:ss}} what leads to the wrong choice of format, what should I check for "null" or "not null"?
MessageFormat is only zero tolerant; that is, it will handle the null argument. If you want the default value to be displayed instead if the value you are working with is null, you have two options:
You can make a triple ...
MessageFormat.format("Value: {0}", null == value ? "" : value)); ... or use StringUtils.defaultIfBlank() instead of commons-lang:
MessageFormat.format("Value: {0}", StringUtils.defaultIfBlank(value, "")); Yes you canβt. Take a look at javadoc. Unfortunately, it does not work with NULL.
Try using the option
Optional.ofNullable(value).orElse(0) Or see an example using ChoiceFormat and MessageFormat.
For more sophisticated patterns, you can use a ChoiceFormat to produce correct forms for singular and plural:
MessageFormat form = new MessageFormat ("The disk \" {1} \ "contains {0}.");
double [] filelimits = {0,1,2};
String [] filepart = {"no files", "one file", "{0, number} files"};
ChoiceFormat fileform = new ChoiceFormat (filelimits, filepart);
form.setFormatByArgumentIndex (0, fileform);
int fileCount = 1273;
String diskName = "MyDisk";
Object [] testArgs = {new Long (fileCount), diskName};
System.out.println (form.format (testArgs));
The output with different values ββfor fileCount:
The disk "MyDisk" contains no files.
The disk "MyDisk" contains one file.
The disk "MyDisk" contains 1,273 files.
You can create the ChoiceFormat programmatically, as in the above example, or by using a pattern. See ChoiceFormat for more information.
form.applyPattern (
"There {0, choice, 0 # are no files | 1 # is one file | 1 I need a mask generator in my class now.
Reason: The user can save a mask with several types: "{0} {1, number, 000} {2, date, MMyyyy}, and the user has data where there can be NULL values, for the result I use the MessageFormat class and I want null ". Zero checking is not so simple, because it means replacing the template that is used for many records (and not just for one). And an empty default value does not exist for a number or date.
So, if someone still needs a solution. I give mine.
Add these methods / classes (I have everything in one class)
private static Object[] replaceNulls2NullValues( Object[] values ) { for ( int i = 0; i < values.length; i++ ) if ( values[i] == null ) values[i] = NullFormatValue.NULL_FORMAT_VALUE; return values; } private static MessageFormat modifyFormaterFormats( MessageFormat formater ) { formater.setFormats( Arrays.stream( formater.getFormats() ).map( ( f ) -> ( f != null ) ? new NullHandlingFormatWrapper( f ) : null ).toArray( ( l ) -> new Format[l] ) ); return formater; } private static final class NullFormatValue { static final Object NULL_FORMAT_VALUE = new NullFormatValue(); private NullFormatValue() { } @Override public String toString() { return ""; } } private static class NullHandlingFormatWrapper extends Format { protected Format wrappedFormat; public NullHandlingFormatWrapper( Format format ) { wrappedFormat = format; } @Override public StringBuffer format( Object obj, StringBuffer toAppendTo, FieldPosition pos ) { if ( !( obj instanceof NullFormatValue ) ) wrappedFormat.format( obj, toAppendTo, pos ); return toAppendTo; } @Override public Object parseObject( String source, ParsePosition pos ) { return wrappedFormat.parseObject( source, pos ); } } and to call the result
modifyFormaterFormats( new MessageFormat( pattern ) ).format( replaceNulls2NullValues( parameters ) );