检查null的值



我有这个java代码,我提出将对象创建到java列表中:

ListIssues obj = new ListIssues(
                    get.getId(),
                    get.getUrl().toString(),
                    get.getTitle(),
                    get.getState().name(),
                    get.getBody(),
                    get.getMilestone().toString(),
                    get.getLabels().toArray(),
                    get.getCreatedAt(),
                    get.getClosedAt()
                );

,但有时我的值无效。我如何以某种智能的方式检查值?

,但有时我的值无效。我如何检查值 一些聪明的方式?

好的做法是,如果缺少任何强制性字段,则ListIssues构造函数应扔IllegalArgumentExcetion(而不是您在构造函数外明确检查null)。

另外,如果可以设置任何可选字段,则可以使用构建器模式,您可以在这里查看。

这很难,可以根据应用程序进行变化,您应该有一种验证方法,以相干的结果返回布尔值验证,我重点介绍了"连贯"一词,因为在某些元素中,如果 get.getLabels().toArray()像数组为空而不是null ...

,则可以更好地验证

无论如何是一个例子:

public static boolean isValid(Iget get){
    boolean ret = true;
    ret &= get.getId() !=null;
    ret &= get.getUrl().toString()!=null;
    ...
    ...
    return ret;
}
// Builder pattern for ListIssues class
public class ListIssues {
    private String id, title, url, name, body, milestone, createdAt, closedAt;
    private Array labels[];
    public static class Builder {
        private String id, title, url, name, body, milestone, createdAt, closedAt;
        private Array labels[];
        public Builder withId(String id) {
            this.id = id;
            return this;
        }
        public Builder withTitle(String title) {
            this.title = title;
            return this;
        }
        public Builder withURL(String url) {
            this.url = url;
            return this;
        }
        public Builder withName(String name) {
            this.name = name;
            return this;
        }
        public Builder withBody(String body) {
            this.body = body;
            return this;
        }
        public Builder withMilestone(String milestone) {
            this.milestone = milestone;
            return this;
        }
        public Builder withCreatedAt(String createdAt) {
            this.createdAt = createdAt;
            return this;
        }
        public Builder withClosedAt(String closedAt) {
            this.closedAt = closedAt;
            return this;
        }
        public Builder withLabels(Array[] labels) {
            this.labels = labels;
            return this;
        }
        public ListIssues build() {
            return new ListIssues(this);
        }
    }
    private ListIssues(Builder b) {
        this.id = b.id;
        this.title = b.title;
        this.url = b.url;
        this.name = b.name;
        this.body = b.body;
        this.milestone = b.milestone;
        this.createdAt = b.createdAt;
        this.closedAt = b.closedAt;
        this.labels = b.labels;
    }}
        // Now you can check for null input parameters using ternary operators before you create the object.
    ListIssues listIssues = new ListIssues.Builder()
    .withURL(get.getUrl() == null ? "" : get.getUrl().toString())
    .withMilestone(get.getMilestone() == null ? "":get.getMilestone().tpString())
.withBody(get.getBody())
    .build();

您的问题在某种程度上仍然不清楚,所以我不知道这是否回答,但是我想提一下,我通常会写下我的构造函数:

public ListIssues(String id, String url, String title, String state,
        Body body, String milestone, String[] labels, Instant createdAt,
        Instant closedAt) {
    super();
    this.id = Objects.requireNonNull(id);
    this.url = Objects.requireNonNull(url);
    this.title = Objects.requireNonNull(title);
    this.state = Objects.requireNonNull(state);
    this.body = Objects.requireNonNull(body);
    this.milestone = Objects.requireNonNull(milestone);
    this.labels = Objects.requireNonNull(labels);
    this.createdAt = Objects.requireNonNull(createdAt);
    this.closedAt = Objects.requireNonNull(closedAt);
}

requireNonNull()如果参数为null,则会抛出NullPointerException。如果允许一个参数为null,只需省略呼叫。

相关内容

  • 没有找到相关文章

最新更新