-1

I have a simple core java question. I have a list which carries multiple response codes. I am returning messages based on the response codes in my list. For one of the scenarios I have to check if the list contains any one of the response codes in it. I have done it the following ways but it ignores it and the codes goes to the else block. So if the the code is ES03 or ES04 or any one of them I want to populate the emailValidationMessage variable but it doesn't do it.

List<String> messageCodes = this.getEmailValidationCode( eSignatureInTO );

    if( messageCodes.contains( "ES01" ) ) {
                IESignatureIntegrationOutDto eSignatureOutTO = getEsignService().resendDocuments( eSignatureInTO );
                eSignatureInTO.setResendDocs( eSignatureOutTO.isResendDocs() );

    } else if( messageCodes.contains( "EE01" ) ) {
                emailValidationMessage = UiIntegrationKeyConstants.EMAIL_FORMAT_ERROR_MESSAGE;

    } else if( messageCodes.contains( Arrays.asList( "ES02", "ES03", "ES04", "EE02", "EE03", "EE04" ) ) ) {
                emailValidationMessage = UiIntegrationKeyConstants.EMAIL_VALIDATION_ERROR_MESSAGE;

    } else {
                emailValidationMessage = UiIntegrationKeyConstants.EMAIL_VALIDATION_MESSAGE;
}

Thanks...

Assafs
  • 3,257
  • 4
  • 26
  • 39
Mike
  • 777
  • 3
  • 16
  • 41

3 Answers3

2

Change the contains expression:

List<String> messageCodes = this.getEmailValidationCode( eSignatureInTO );

if( messageCodes.contains( "ES01" ) ) {
    IESignatureIntegrationOutDto eSignatureOutTO = getEsignService().resendDocuments( eSignatureInTO );
    eSignatureInTO.setResendDocs( eSignatureOutTO.isResendDocs() );
} else if( messageCodes.contains( "EE01" ) ) {
    emailValidationMessage = UiIntegrationKeyConstants.EMAIL_FORMAT_ERROR_MESSAGE;
} else if( !Collections.disjoint(messageCodes,Arrays.asList( "ES02", "ES03", "ES04", "EE02", "EE03", "EE04" ) ) ) {
    emailValidationMessage = UiIntegrationKeyConstants.EMAIL_VALIDATION_ERROR_MESSAGE;
} else {
    emailValidationMessage = UiIntegrationKeyConstants.EMAIL_VALIDATION_MESSAGE;
}

The Collections.disjoint acts as a "contains any" comparison. This way if one or more of the error codes exists, it will go into the if clause.

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
Assafs
  • 3,257
  • 4
  • 26
  • 39
  • 1
    It worked the way I wanted it. Thank you so much. Code looks nice and clean this way. – Mike Aug 22 '17 at 17:40
  • But not efficient. It creates a new `Arrays.asList()` every time the test is run. A `private final static` field should be used to store the argument for `disjoint()`. Also, the argument should be a `Set`, not a `List` to speed up the `disjoint` check. For that matter, `Set` would be a better type for `messageCodes` as well, for the same reason. – AJNeufeld Aug 22 '17 at 17:45
1

So if the the code is ES03 or ES04 or any one of them I want to populate the emailValidationMessage variable but it doesn't do it.

Shouldn't you be doing something like :

if( messageCodes.contains("ES03") || messageCodes.contains("ES04")) { 
      emailValidationMessage = "whatever your value should be";
}
Naman
  • 27,789
  • 26
  • 218
  • 353
  • I could do it this way but I want to lower too many ||, so I thought if there is a way to do it in a shorter way. – Mike Aug 22 '17 at 17:31
1

In the second if, you're asking if there's a list stored inside your String array. That won't work. You should do a .contains for each of the individual values you're looking for.

mjuarez
  • 16,372
  • 11
  • 56
  • 73