QuerySuggestionSource implements SuggestionSource<SearchSuggestion> { @Override public List<SearchSuggestion> getSuggestions(SuggestionQuery q) { String s = q.getValue(); if (s.trim().length() == 0) { final List<SearchSuggestion> fields = this.fields.stream().map(f -> new SearchSuggestion(f, f + " ")).collect(Collectors.toList()); final List<SearchSuggestion> h = history.stream() .map(SuggestionQuery::getValue) .map(x -> new SearchSuggestion(x, x)).collect(Collectors.toList()); fields.addAll(h); return fields; } List<ExpectedType> expectedType = getExpectedType(s); ArrayList<SearchSuggestion> result = new ArrayList<>(); if (expectedType.contains(ExpectedType.FIELD)) { final List<SearchSuggestion> collect = fields.stream() .map(f -> new SearchSuggestion(f, s + f + " ")) .collect(Collectors.toList()); result.addAll(collect); } final String fieldHead = getLastNotFinishedCondition(s); if (expectedType.contains(ExpectedType.FIELD_TAIL)) { final List<SearchSuggestion> collect = fields.stream() .filter(f -> fieldHead.length() > 0) .filter(f -> f.startsWith(fieldHead)) .map(f -> new SearchSuggestion(f, s + f.substring(fieldHead.length()) + " ")) .collect(Collectors.toList()); result.addAll(collect); } if (expectedType.contains(ExpectedType.OPERATOR)) { result.addAll(operators.stream().map(o -> new SearchSuggestion(o, s + " " + o + " ")).collect(Collectors.toList())); } if (expectedType.contains(ExpectedType.VALUE_MSG)) { final Set<String> message = getValuesForFieldFromHistory("message", history); result.addAll(message.stream().map(x -> new SearchSuggestion(x, s + x)).collect(Collectors.toList())); } if (expectedType.contains(ExpectedType.VALUE_DATE)) { final Set<String> message = getValuesForFieldFromHistory("date", history); result.addAll(message.stream().map(x -> new SearchSuggestion(x, s + x)).collect(Collectors.toList())); final List<SimpleDateFormat> formats = new ArrayList<>(); formats.addAll(Arrays.asList(TimestampInequalityRule.DATE_FORMATS)); formats.addAll(Arrays.asList(TimestampInequalityRule.TIME_FORMATS)); final Date now = new Date(); final List<SearchSuggestion> timeDateInfo = formats.stream() .map(x -> new SearchSuggestion( String.format("Date in format %s, i.e.: %s", x.toPattern(), x.format(now)), s + "'" + x.format(now) + "' ")) .collect(Collectors.toList()); result.addAll(timeDateInfo); } if (expectedType.contains(ExpectedType.VALUE_MARK)) { final MarkerColors[] values = MarkerColors.values(); final List<MarkerColors> colors = Arrays.asList(values); final List<String> mark = getFieldValues("mark", s); final List<MarkerColors> suggestions = colors.stream() .filter(x -> mark.stream().filter(colorPart -> x.name().startsWith(colorPart)).findAny().isPresent()) .collect(Collectors.toList()); suggestions.stream() .map(c -> new SearchSuggestion(c.name(), s + c.name() + " ")); } if (expectedType.contains(ExpectedType.PROPERTY)) { result.addAll(Collections.singletonList(new SearchSuggestion(")", s + ") "))); } if (expectedType.contains(ExpectedType.VALUE_LEVEL)) { final List<String> levels = Arrays.asList("FINEST FINER FINE CONFIG INFO WARN SEVERE".split("\\s")); final List<String> typedLevels = getFieldValues("level", fieldHead); final String typedLevel = typedLevels.size() > 0 ? typedLevels.get(0) : ""; final Stream<String> levelsStream = levels.stream().filter(l -> l.startsWith(typedLevel)); result.addAll(levelsStream.map(l -> new SearchSuggestion(l, s + l.substring(typedLevel.length()) + " ")).collect(Collectors.toList())); } if (expectedType.contains(ExpectedType.JOIN)) { final String s1 = s.replaceFirst("(.*[^&\\|])([&\\|]\\s*)", "$1"); result.addAll(joins.stream().map(l -> new SearchSuggestion(l, s1 + l + " ")).collect(Collectors.toList())); } final List<SearchSuggestion> matchingHistory = history .stream() .map(SuggestionQuery::getValue) .filter(x -> x.contains(s)) .filter(x -> !x.equals(s)) .map(x -> new SearchSuggestion(x, x)) .collect(Collectors.toList()); result.addAll(matchingHistory); return result; } QuerySuggestionSource(List<SuggestionQuery> history); @Override List<SearchSuggestion> getSuggestions(SuggestionQuery q); String getLastValue(String s); static void main(String[] args);  }
@Test(enabled = false) public void testGetSuggestions() throws Exception { Assert.fail("Not implemented"); }