title: Optimizing multiple if-else condition in Java
body: <p>I have a REST API which will provide search results based on specific search criteria.
  User can search using Department Id, User Id and Joined Date.
  So here, user can search in 7 different combinations.</p>
  
  <ol>
  <li>Search only with Dept Id</li>
  <li>Search only with User Id</li>
  <li>Search only with Joined Date</li>
  <li>Search with Dept Id &amp; User Id</li>
  <li>Search with User Id &amp; Joined Date</li>
  <li>Search with Dept Id &amp; Joined Date</li>
  <li>Search will all 3 criteria (Dept Id, User Id &amp; Joined Date)</li>
  </ol>
  
  <p>According to our application's current design, For each criteria I would have to call different services in different order for information.</p>
  
  <p>For instance : We have an existing User service which has user details, a Department service which has details. So if user searches only for User ID, I would directly go to UserService. If user searches with Dept ID &amp; User I would first have to query Dept service and then the User service.</p>
  
  <p>My current code is:</p>
  
  <pre><code>if(Util.hasOnlyUserId(searchCriteria)) {
          searchResponse = searchWithUserId(searchRequest);
      } else if(Util.hasOnlyJoinedDate(searchCriteria)) {
          searchResponse = searchWithDate(searchRequest);
      } else if (Util.hasOnlyDeptID(searchCriteria)) {
          searchResponse = searchWithDeptId(searchRequest);
      } else if (Util.hasUserIdAndDeptId(searchCriteria)) {
          searchResponse = searchWithUserIdAndDeptId(searchRequest);
      } else if (Util.hasUserIdAndDate(searchCriteria)){
          searchResponse = searchWithUserIdAndDate(searchRequest);
      } else if (Util.hasDeptIdAndDate(searchCriteria)) {
          searchResponse = searchWithDeptIdAndDate(searchRequest);
      } else if (Util.hasAllCriteria(searchCriteria)) {
          searchResponse = searchWithAllCriteria(searchRequest);
      }
  </code></pre>
  
  <p>Here, searchCriteria is my request object (POJO) which has these search elements as private members.</p>
  
  <p>searchResponse here is a type of SearchResponse object which is the actual response of the API and it contains the user's details like name, id, position, lastSalaryDrawn, CommuteType, MaritalStatus and Last Promoted Date</p>
  
  <p>I have been trying to optimize this multiple if-else condition in other words, avoid having this multiple condition.
  I tried to separate each of them and put them into different methods. But this just isn't satisfying me and it seems like it is in an un-optimizable state.</p>
  
  <p>Any ideas on how I can go about doing this would be greatly appreciated.</p>
  
  <p>Edit : We are using JPA to interact with the MySQL.
  The problem here is I will not get the information in a straight forward manner from a DB. So building a dynamic query might not work.
  The information needs to be fetched from multiple micro services, which will in turn make a call to their native DB and send the required information as a response.
  I hope this make sense.</p>