Java MessageFormat Null值

时间:2014-07-08 22:59:16

标签: java messageformat

在Java MessageFormat中处理空值的最佳方法是什么

MessageFormat.format("Value: {0}",null);

=> Value: null

但实际上是“价值:”会很好。

与日期相同

MessageFormat.format("Value: {0,date,medium}",null);

=> Value: null

“价值:”会更受欢迎。

有没有办法做到这一点?我尝试了选择

{0,choice,null#|notnull#{0,date,dd.MM.yyyy – HH:mm:ss}}

会导致选择格式无效,检查“null”或“not null”是正确的吗?

3 个答案:

答案 0 :(得分:1)

MessageFormat只能容忍null;也就是说,它将处理一个null参数。如果您希望显示默认值而不是某个值(如果您使用的值为null),则有两个选项:

你可以做三元...

MessageFormat.format("Value: {0}", null == value ? "" : value));

...或者使用来自commons-lang的StringUtils.defaultIfBlank()

MessageFormat.format("Value: {0}", StringUtils.defaultIfBlank(value, ""));

答案 1 :(得分:-1)

是的,你不能。看看javadoc。不幸的是,它无法使用NULL。

尝试使用可选项

    Optional.ofNullable(value).orElse(0)

或者查看示例如何使用ChoiceFormat和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

答案 2 :(得分:-1)

我现在需要通过面具在我的生成器类中。

原因: 用户可以使用多种类型保存掩码,例如" {0} {1,number,000} {2,date,MMyyyy}。并且用户拥有可以为空的数据。为了结果我使用MessageFormat类。并希望空字符串没有默认值' null'文本。 空检查并不容易,因为它将意味着替换用于许多记录的模式(不仅仅是一个)。数字或日期不存在默认空值。

所以,如果有人仍然需要解决方案。我给了我。

添加此方法/类(我在一个类中都有)

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 );
    }
}

和结果调用

modifyFormaterFormats( new MessageFormat( pattern ) ).format( replaceNulls2NullValues( parameters ) );