-1

I need to get the enum itself first by using RegoDocumentType.getByValue(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.

So how do I refactor the code to only use one if statement to cater for both NRIC/11B and FIN smag value to rego value using RegoDocumentType enum?

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()); }
}
ginny
  • 1
  • 2
  • Did you mean `RegoDocumentType. getBySmagValue(createOrderRequest.getIdDocument().getIdType())` ? – Backflip Feb 07 '21 at 12:14
  • Oh yes, that's what I meant. Sorry about the typo. – ginny Feb 07 '21 at 12:17
  • Do you expect that `createOrderRequest.getIdDocument().getIdType()` can be null, or do you expect that `createOrderRequest.getIdDocument().getIdType()` value is not among the predefined enum values? – Backflip Feb 07 '21 at 12:21
  • I expect the createOrderRequest.getIdDocument().getIdType() can be null. – ginny Feb 07 '21 at 12:23
  • From your example, the logic you want is: `IF createOrderRequest.getOrderId() is not null AND createOrderRequest.getIdDocument().getIdType() is "FIN" THEN return "Employment Pass" ELSE return createOrderRequest.getIdDocument().getIdType() ` Is that what you need? What if `createOrderRequest.getIdDocument().getIdType()` is null? – Backflip Feb 07 '21 at 12:39

2 Answers2

0
    String something = "";
    if(something == null || something.isEmpty()) {
        // return A
    } else {
        // return B
    }

or

    String something = "";
    return something == null || something.isEmpty() ? [value A] : [value B];
codingR
  • 1
  • 1
0

createOrderRequest.getIdDocument().getIdType() seems to be a String in you case. You cannot return a string from getBySmagValue. Consider returning null or throwing an exception instead:

public static RegoDocumentType getBySmagValue(String smagValue) { 
    if (smagValue == null) reutrn null;
    return BY_SMAG_VALUE.get(smagValue.toLowerCase()); 
}

or

public static RegoDocumentType getBySmagValue(String smagValue) { 
    if (smagValue == null) throw new IllegalStateException();
    return BY_SMAG_VALUE.get(smagValue.toLowerCase()); 
}
Backflip
  • 232
  • 2
  • 9