Multiple if conditions in one block - looking for faster and more efficient code i am looking for a faster and more efficient way of this code. What the code does is simply compares message context of (user, application, service and operation) and compares it with some rules.

private boolean match(Context messageContext, ContextRule contextRule) {
        if(contextRule.getMessageContext().getUser().equals(ContextRuleEvaluator.WILDCARD)
            || (contextRule.getMessageContext().getUser().equals(messageContext.getUser()))) {
          if(contextRule.getMessageContext().getApplication().equals(ContextRuleEvaluator.WILDCARD)
              || (contextRule.getMessageContext().getApplication().equals(messageContext.getApplication()))) {
            if(contextRule.getMessageContext().getService().equals(ContextRuleEvaluator.WILDCARD)
                || (contextRule.getMessageContext().getService().equals(messageContext.getService()))) {
              if(contextRule.getMessageContext().getOperation().equals(ContextRuleEvaluator.WILDCARD)
                  || (contextRule.getMessageContext().getOperation().equals(messageContext.getOperation()))) {
                return true;
              }
            }
          }
        }

        return false;
      } 


Edit: Context and ContextRule are an interfaces 

public interface Context {    
   public String getUser();      
   public void setUser(String user);      
   public String getApplication();      
   public void setApplication(String application);      
   public String getService();      
   public void setService(String service);      
   public String getOperation();      
   public void setOperation(String operation);
}   

public interface ContextRule {    
  public Context getMessageContext();      
  public int getAllowedConcurrentRequests();      
}


Any suggestions appreciated :)
First of all, your formatting seems messy, please fix it.



Did you rip it out or is there really zero documentation in there (JavaDoc)? Consider adding some, it will make your life and that of others easier.



First step would be to add an overload to match() which accepts two strings, like this:

private boolean match(Context messageContext, ContextRule contextRule) {
    return match(contextRule.getMessageContext().getUser(), messageContext.getUser())
            &amp;&amp; match(contextRule.getMessageContext().getApplication(), messageContext.getApplication())
            &amp;&amp; match(contextRule.getMessageContext().getService(), messageContext.getService())
            &amp;&amp; match(contextRule.getMessageContext().getOperation(), messageContext.getOperation());
}

private boolean match(String value, String contextValue) {
    return value.equals(ContextRuleEvaluator.WILDCARD) || value.equals(contextValue);
}


This makes it already a lot easier to read. Naming of the variables is a little bit suboptimal, that's because I could not come up with better names right now.



Ultimately you should see if you can't extend and change Context to contain a match() function. This would require to change Context into an abstract class, so it might not be possible.

public abstract class Context {

    public abstract String getUser();

    public abstract void setUser(String user);

    public abstract String getApplication();

    public abstract void setApplication(String application);

    public abstract String getService();

    public abstract void setService(String service);

    public abstract String getOperation();

    public abstract void setOperation(String operation);

    /**
     * Matches this context against the given Context.
     *
     * @param context
     * @return
     */
    public boolean match(Context context) {
        return match(getUser(), context.getUser())
                &amp;&amp; match(getApplication(), context.getApplication())
                &amp;&amp; match(getService(), context.getService())
                &amp;&amp; match(getOperation(), context.getOperation());
    }

    private boolean match(String value, String otherValue) {
        return value.equals(ContextRuleEvaluator.WILDCARD) || value.equals(otherValue);
    }
}


That would allow you to do this in your code:

return contextRule.getMessageContext().match(messageContext);


Which is very readable, even if you don't pack it into a function but call it directly.