使用Java Optionals防止级联空指针

时间:2018-03-16 14:52:09

标签: java java-8 optional

我试图从嵌套对象中解析一些值,同时尝试通过使用Optional类来防止任何内部对象为null。如下所示:

Optional<Dobule> pdl1 = Optional.ofNullable(molecularFindings.get(0).getPDL1().getEdit().getValue());

有时我会在getPDL1()getEdit()返回null时看到异常,因为我的Optional仅包装getValue()函数的结果Double值。

我已经研究过一些解决方案,例如在Optional类中包装我的每个对象,如本article中所述。但是,我更喜欢一种解决方案,它不会强迫我更改MolecularFindingPDL1Edit的基础类,因为它们是从JSON反序列化创建的,我不喜欢不相信将一切包装在Optional中是一个很好的解决方案。

作为参考,我的POJO目前定义如下:

package com.genomics.model.statistics;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.genomics.model.resource.Resource;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "TMB",
        "config",
        "WTCandidates"
})
public class MolecularFinding extends Resource{

    @JsonProperty("PDL1")
    private PDL1 pDL1;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();


    @JsonProperty("PDL1")
    public PDL1 getPDL1() {
        return pDL1;
    }

    @JsonProperty("PDL1")
    public void setPDL1(PDL1 pDL1) {
        this.pDL1 = pDL1;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

package com.genomics.model.statistics;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "edit"
})
public class PDL1 {

    @JsonProperty("edit")
    private Edit edit;
    @JsonProperty("value")
    private Double value;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("edit")
    public Edit getEdit() {
        return edit;
    }

    @JsonProperty("edit")
    public void setEdit(Edit edit) {
        this.edit = edit;
    }

    @JsonProperty("value")
    public Double getValue() {
        return value;
    }

    @JsonProperty("value")
    public void setValue(Double value) {
        this.value = value;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

package com.genomics.model.statistics;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "level",
        "value",
        "abs_qty",
        "exist_in_pop_qty",
        "recovered_from_COSMIC"
})
public class Edit {

    @JsonProperty("level")
    private String level;
    @JsonProperty("value")
    private Double value;
    @JsonProperty("abs_qty")
    private Integer absQty;
    @JsonProperty("exist_in_pop_qty")
    private Integer existInPopQty;
    @JsonProperty("recovered_from_COSMIC")
    private Integer recoveredFromCOSMIC;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("level")
    public String getLevel() {
        return level;
    }

    @JsonProperty("level")
    public void setLevel(String level) {
        this.level = level;
    }

    @JsonProperty("value")
    public Double getValue() {
        return value;
    }

    @JsonProperty("value")
    public void setValue(Double value) {
        this.value = value;
    }

    @JsonProperty("abs_qty")
    public Integer getAbsQty() {
        return absQty;
    }

    @JsonProperty("abs_qty")
    public void setAbsQty(Integer absQty) {
        this.absQty = absQty;
    }

    @JsonProperty("exist_in_pop_qty")
    public Integer getExistInPopQty() {
        return existInPopQty;
    }

    @JsonProperty("exist_in_pop_qty")
    public void setExistInPopQty(Integer existInPopQty) {
        this.existInPopQty = existInPopQty;
    }

    @JsonProperty("recovered_from_COSMIC")
    public Integer getRecoveredFromCOSMIC() {
        return recoveredFromCOSMIC;
    }

    @JsonProperty("recovered_from_COSMIC")
    public void setRecoveredFromCOSMIC(Integer recoveredFromCOSMIC) {
        this.recoveredFromCOSMIC = recoveredFromCOSMIC;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

有没有人有解决方案?或者最好的事情就是将我的POJO的每个变量包装在一个Optional中? 感谢您抽出宝贵时间提供帮助!

0 个答案:

没有答案