dimanche 7 février 2021

How do I check a string is empty or null using if statement in Java?

So I need to get the enum itself first by using

RegoDocumentType.getBySmagValue(createOrderRequest.getIdDocument().getIdType())

Then check for null value, if it is not null, it will return the enum value. Else, it will return the

createOrderRequest.getIdDocument().getIdType()* as default.

I tried to do it this way but didn't work, this is kinda like the idea

private String RegoDocumentType.getByValue(createOrderRequest.getIdDocument().getIdType()) {
        if (!StringUtils.isEmpty(createOrderRequest.getOrderId()) &&
           "FIN".equalsIgnoreCase(createOrderRequest.getIdDocument().getIdType()){
            return RegoDocumentType.FIN.toValue();
        }
        return createOrderRequest.getIdDocument().getIdType();
    }

Here's my enum:

public enum RegoDocumentType {
    
    NRIC_11B("NRIC/11B", IdentityType.NRIC.toValue()),
    FIN("Employment Pass", IdentityType.EM_PASS.toValue()),
    ;
    private static final Map<String, RegoDocumentType> BY_SMAG_VALUE = new HashMap<>();
    static {
        for (RegoDocumentType identityType : values()) {
            BY_SMAG_VALUE.put(identityType.getSmagValue().toLowerCase(), identityType);
        }
    }
    private final String smagValue;
    private final String regoValue;
    RegoDocumentType(String smagValue, String regoValue) {
        this.smagValue = smagValue;
        this.regoValue = regoValue;
    }
    public String getSmagValue() {
        return smagValue;
    }
    public String getRegoValue() {
        return regoValue;
    }
    public static RegoDocumentType getBySmagValue(String smagValue)
    { return BY_SMAG_VALUE.get(smagValue.toLowerCase()); }
}

Aucun commentaire:

Enregistrer un commentaire