简体   繁体   English

如果列表为空,使用 lombok getter 初始化列表?

[英]Initialize list if list is null with lombok getter?

I am currently replacing all my standard POJO's to use Lombok for all the boilerplate code.我目前正在替换我所有的标准 POJO,以将 Lombok 用于所有样板代码。 I find myself keeping getters for lists because I want to return an empty list if the list has not been initialized.我发现自己为列表保留了 getter,因为如果列表尚未初始化,我想返回一个空列表。 That is, I don't want the getter to return null.也就是说,我不希望 getter 返回 null。 If there some lombok magic that I'm not aware of that can help me avoid doing this?如果有一些我不知道的龙目岛魔法可以帮助我避免这样做吗?

Example of generated code生成的代码示例

private List<Object> list;
public Object getList(){ return list; }

What I would like instead:我想要的是:

private List<Object> list;
public Object getList(){
    if (list == null) {
        return new ArrayList();
    }
    return list;
}

You can achieve this by declaring and initializing the fields.您可以通过声明和初始化字段来实现这一点。 The initialization will be done when the enclosing object is initialized.初始化将在初始化封闭对象时完成。

private List<Object> list = new ArrayList();

Lomboks @Getter annotation provides an attribute lazy which allows lazy initialization. Lomboks @Getter注释提供了一个lazy属性,它允许延迟初始化。

 @Getter(lazy=true) private final double[] cached = expensiveInitMethod();

Documentation文档

I had the same questions as of this one.我和这个有同样的问题。 Though, the above answers are useful in some ways, the exact solution is to use @Builder and @Singular annotations of Lombok API like below given code.虽然,上述答案在某些方面很有用,但确切的解决方案是使用 Lombok API 的@Builder@Singular注释,如下面给定的代码。

It worked superb for me.它对我来说效果很好。

@Builder
class MyClass{
    @Singular
    private List<Type> myList;
}

This will initialize myList with a non-null List object.这将用一个非空的 List 对象初始化 myList。 Though, this questions is an old one.虽然,这个问题是一个古老的问题。 But, still posting this answer to help someone like me who will refer this question in future.但是,仍然发布此答案以帮助像我这样将来会提及此问题的人。

That is, I don't want the getter to return null.也就是说,我不希望 getter 返回 null。 If there some lombok magic that I'm not aware of that can help me avoid doing this?如果有一些我不知道的龙目岛魔法可以帮助我避免这样做吗?

You don't need any magic to be happen.你不需要任何魔法就可以发生。 Just initialize the list .只需初始化list

Newest versions of lombok do not generate a getter method if a getter method is provided with the same name and number of parameters.如果提供具有相同名称和参数数量的 getter 方法,则最新版本的 lombok 不会生成 getter 方法。

With older versions of lombok, you can override the getter with anything you like by using AccessLevel.NONE on the field.使用旧版本的 lombok,您可以通过在字段上使用 AccessLevel.NONE 用您喜欢的任何内容覆盖 getter。

Note that merely initializing the field does not protect you from clients calling a constructor with nulls, or calling a setter with null (Still may be okay, depending on what you want).请注意,仅初始化该字段并不能保护您免受客户端调用具有空值的构造函数或调用具有空值的 setter 的影响(仍然可能没问题,这取决于您想要什么)。

Eg例如

// only necessary for older versions of lombok
@Getter(AccessLevel.NONE)
private Map<String, String> params;

public Map<String, String> getParams() {
    return (params == null) ? new HashMap<>() : params;
}

An old question, but this plugin does what you want:一个老问题,但这个插件可以满足您的需求:

https://github.com/dmak/jaxb-xew-plugin https://github.com/dmak/jaxb-xew-plugin

Using this plugin generated the getter as:使用此插件生成的 getter 为:

public List<BaseVariableType> getVariables() {
    if (variables == null) {
        variables = new ArrayList<BaseVariableType>();
    }
    return variables;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM