QuerySuggestionSource implements SuggestionSource<SearchSuggestion> { protected String getLastNotFinishedCondition(String s) { if (s.matches("\\s*(&&\\|\\|)?\\s*\\(.*")) { final int closingIndex = closingParenthesisPosition(s); if (closingIndex > 0) { String substring = s.substring(closingIndex + 1); if (substring.matches("\\s*(&&\\|\\|)?\\s*(.*)")) { substring = substring.replaceFirst("\\s*(&&|\\|\\|)?\\s*(.*)", "$2"); } return getLastNotFinishedCondition(substring); } } final String fieldOperatorValue = fieldsPattern + "\\s*" + operatorsPattern + "\\s*(\\S+.*)"; final Pattern pattern = Pattern.compile(fieldOperatorValue); String rest = s; if (rest.matches(fieldOperatorValue)) { final Matcher matcher = pattern.matcher(rest); matcher.find(); for (int i = 0; i <= matcher.groupCount(); i++) { System.out.printf("Group %d=\"%s\"%n", i, matcher.group(i)); } String group3 = matcher.group(3); group3 = trimLeading(group3); if (isLastValue(group3)) { return rest; } String anotherQuery = ""; if (group3.matches("\\w+")) { anotherQuery = ""; } else if (group3.matches("\\w+\\s*(\\S+.*)")) { anotherQuery = group3.replaceFirst("\\w+\\s*(\\S+.*\\s)", "$1"); } else if (group3.matches("'.+?'(.*)")) { anotherQuery = group3.replaceFirst("'.+?'(.*)", "$1"); } else if (group3.matches("\".+?\"(.*)")) { anotherQuery = group3.replaceFirst("\".+?\"(.*)", "$1"); } rest = anotherQuery; rest = trimLeading(rest); boolean operatorFound = StringUtils.startsWithAny(rest, new String[]{"&&", "||"}); if (operatorFound) { rest = rest.substring(2); rest = trimLeading(rest); } rest = getLastNotFinishedCondition(rest); } return rest; } QuerySuggestionSource(List<SuggestionQuery> history); @Override List<SearchSuggestion> getSuggestions(SuggestionQuery q); String getLastValue(String s); static void main(String[] args);  }
@Test(dataProvider = "getLastNotFinishedCondition", enabled = false) public void testGetLastNotFinishedCondition(String query, String rest) { Assert.assertEquals(underTest.getLastNotFinishedCondition(query), rest); }