public static String buildFromParamsMap(String paramsRule, Multimap<String, String> paramsMap) {
      if (paramsMap != null && !paramsMap.isEmpty()) {
         Multimap<String, String> criteriaMap = TreeMultimap.create(paramsMap);

         // Just appends sorted entries, separating them with ?.
         StringBuilder result = new StringBuilder();
         for (Map.Entry<String, String> criteria : criteriaMap.entries()) {
            /*
             * Check that criteria is embedded into the rule. Simply check word boundary with \b is not enough as - are
             * valid in params (according RFC 3986) but not included into word boundary - so "word-ext" string is
             * matching ".*\\bword\\b.*" We need to tweak it a bit to prevent matching when there's a - before or after
             * the criteria we're looking for (see
             * https://stackoverflow.com/questions/32380375/hyphen-dash-to-be-included-in-regex-word-boundary-b)
             */
            if (paramsRule.matches(".*(^|[^-])\\b" + criteria.getKey() + "\\b([^-]|$).*")) {
               result.append("?").append(criteria.getKey()).append("=").append(criteria.getValue());
            }
         }
         return result.toString();
      }
      return "";
   }
@Test
   void testBuildFromParamsMap() {
      Multimap<String, String> paramsMap = ArrayListMultimap.create();
      paramsMap.put("page", "1");
      paramsMap.put("limit", "20");
      paramsMap.put("limitation", "20");
      paramsMap.put("status", "available");

      // Only 1 parameter should be taken into account according to rules.
      String dispatchCriteria = DispatchCriteriaHelper.buildFromParamsMap("page", paramsMap);
      assertEquals("?page=1", dispatchCriteria);

      // 2 parameters should be considered and sorted according to rules.
      dispatchCriteria = DispatchCriteriaHelper.buildFromParamsMap("page && limit", paramsMap);
      assertEquals("?limit=20?page=1", dispatchCriteria);

      // 2 parameters should be considered and sorted according to rules with no inclusion of limit.
      dispatchCriteria = DispatchCriteriaHelper.buildFromParamsMap("page && limitation", paramsMap);
      assertEquals("?limitation=20?page=1", dispatchCriteria);
   }