9

在 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”的正确方法是什么?

4

3 回答 3

3

MessageFormat仅容零;也就是说,它将处理一个空参数。如果您希望在使用的值为 null 的情况下显示默认值而不是某些内容,则有两种选择:

你可以做一个三元...

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

...或使用StringUtils.defaultIfBlank()commons-lang 代替:

MessageFormat.format("Value: {0}", StringUtils.defaultIfBlank(value, ""));
于 2014-07-08T23:57:29.200 回答
-1

是的,你不能。看看javadoc。不幸的是,它不适用于 NULL。

尝试使用可选

    Optional.ofNullable(value).orElse(0)

或查看如何使用 ChoiceFormat 和 MessageFormat 的示例。


    对于更复杂的模式,您可以使用 ChoiceFormat 为单数和复数生成正确的形式:

     MessageFormat form = new MessageFormat("磁盘\"{1}\"包含{0}。");
     双 [] 文件限制 = {0,1,2};
     String[] filepart = {"没有文件","一个文件","{0,number} 个文件"};
     ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
     form.setFormatByArgumentIndex(0, fileform);

     int 文件计数 = 1273;
     字符串磁盘名称 = "我的磁盘";
     Object[] testArgs = {new Long(fileCount), diskName};

     System.out.println(form.format(testArgs));

    fileCount 具有不同值的输出:
     磁盘“MyDisk”不包含文件。
     磁盘“MyDisk”包含一个文件。
     磁盘“MyDisk”包含 1,273 个文件。

    您可以通过编程方式创建 ChoiceFormat,如上例所示,或使用模式。有关详细信息,请参阅选择格式。


     form.applyPattern(
        "有{0,choice,0#没有文件|1#是一个文件|1
于 2015-01-30T11:29:59.260 回答
-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 ) );
于 2018-02-02T13:00:49.113 回答