@Test public void testOrderingSuggestionsText() { String query = "125"; AutocompleteSuggestion s1 = AutocompleteSuggestion.builder() .setText("123") .setSuggestionType(SuggestionType.CONSTANT) .build(); AutocompleteSuggestion s2 = AutocompleteSuggestion.builder() .setText("234") .setSuggestionType(SuggestionType.CONSTANT) .build(); assertThat( orderSuggestions(query, ImmutableList.of(s1, s2)), equalTo(ImmutableList.of(s1, s2))); assertThat( orderSuggestions(query, ImmutableList.of(s2, s1)), equalTo(ImmutableList.of(s1, s2))); }
@VisibleForTesting static List<AutocompleteSuggestion> orderSuggestions( String query, List<AutocompleteSuggestion> suggestions) { final LevenshteinDistance distance = new LevenshteinDistance(); return suggestions.stream() .sorted( Comparator.comparing(AutocompleteSuggestion::getSuggestionType) .thenComparing( s -> -1 * StringUtils.getCommonPrefix( query.toLowerCase(), (query.substring(0, s.getInsertionIndex()) + s.getText())) .toLowerCase() .length()) .thenComparing( s -> distance.apply( query.toLowerCase(), (query.substring(0, s.getInsertionIndex()) + s.getText()) .toLowerCase()))) .collect(ImmutableList.toImmutableList()); }
AutoCompleteUtils { @VisibleForTesting static List<AutocompleteSuggestion> orderSuggestions( String query, List<AutocompleteSuggestion> suggestions) { final LevenshteinDistance distance = new LevenshteinDistance(); return suggestions.stream() .sorted( Comparator.comparing(AutocompleteSuggestion::getSuggestionType) .thenComparing( s -> -1 * StringUtils.getCommonPrefix( query.toLowerCase(), (query.substring(0, s.getInsertionIndex()) + s.getText())) .toLowerCase() .length()) .thenComparing( s -> distance.apply( query.toLowerCase(), (query.substring(0, s.getInsertionIndex()) + s.getText()) .toLowerCase()))) .collect(ImmutableList.toImmutableList()); } }
AutoCompleteUtils { @VisibleForTesting static List<AutocompleteSuggestion> orderSuggestions( String query, List<AutocompleteSuggestion> suggestions) { final LevenshteinDistance distance = new LevenshteinDistance(); return suggestions.stream() .sorted( Comparator.comparing(AutocompleteSuggestion::getSuggestionType) .thenComparing( s -> -1 * StringUtils.getCommonPrefix( query.toLowerCase(), (query.substring(0, s.getInsertionIndex()) + s.getText())) .toLowerCase() .length()) .thenComparing( s -> distance.apply( query.toLowerCase(), (query.substring(0, s.getInsertionIndex()) + s.getText()) .toLowerCase()))) .collect(ImmutableList.toImmutableList()); }  }
AutoCompleteUtils { @VisibleForTesting static List<AutocompleteSuggestion> orderSuggestions( String query, List<AutocompleteSuggestion> suggestions) { final LevenshteinDistance distance = new LevenshteinDistance(); return suggestions.stream() .sorted( Comparator.comparing(AutocompleteSuggestion::getSuggestionType) .thenComparing( s -> -1 * StringUtils.getCommonPrefix( query.toLowerCase(), (query.substring(0, s.getInsertionIndex()) + s.getText())) .toLowerCase() .length()) .thenComparing( s -> distance.apply( query.toLowerCase(), (query.substring(0, s.getInsertionIndex()) + s.getText()) .toLowerCase()))) .collect(ImmutableList.toImmutableList()); }  @Nonnull static List<AutocompleteSuggestion> autoComplete(
      Variable.Type completionType, String query, int maxSuggestions); @Nonnull static List<AutocompleteSuggestion> autoComplete(
      @Nullable String network,
      @Nullable String snapshot,
      Variable.Type completionType,
      String query,
      int maxSuggestions,
      @Nullable CompletionMetadata completionMetadata,
      @Nullable NodeRolesData nodeRolesData,
      @Nullable ReferenceLibrary referenceLibrary); @Nonnull static List<AutocompleteSuggestion> autoComplete(
      @Nullable String network,
      @Nullable String snapshot,
      Variable.Type completionType,
      String query,
      int maxSuggestions,
      @Nullable CompletionMetadata completionMetadata,
      @Nullable NodeRolesData nodeRolesData,
      @Nullable ReferenceLibrary referenceLibrary,
      boolean fuzzyMatching); @Nonnull static List<AutocompleteSuggestion> baseAutoComplete(
      @Nullable String query, Set<String> allProperties); @Nonnull static List<AutocompleteSuggestion> stringAutoComplete(
      @Nullable String query, Set<String> strings); @Nonnull static List<AutocompleteSuggestion> stringAutoComplete(
      @Nullable String query, Map<String, Optional<String>> stringsWithDescriptions); @Nonnull static ImmutableList<AutocompleteSuggestion> ipStringAutoComplete(
      @Nullable String query, Map<Ip, IpCompletionMetadata> ips); }
AutoCompleteUtils { @VisibleForTesting static List<AutocompleteSuggestion> orderSuggestions( String query, List<AutocompleteSuggestion> suggestions) { final LevenshteinDistance distance = new LevenshteinDistance(); return suggestions.stream() .sorted( Comparator.comparing(AutocompleteSuggestion::getSuggestionType) .thenComparing( s -> -1 * StringUtils.getCommonPrefix( query.toLowerCase(), (query.substring(0, s.getInsertionIndex()) + s.getText())) .toLowerCase() .length()) .thenComparing( s -> distance.apply( query.toLowerCase(), (query.substring(0, s.getInsertionIndex()) + s.getText()) .toLowerCase()))) .collect(ImmutableList.toImmutableList()); }  @Nonnull static List<AutocompleteSuggestion> autoComplete(
      Variable.Type completionType, String query, int maxSuggestions); @Nonnull static List<AutocompleteSuggestion> autoComplete(
      @Nullable String network,
      @Nullable String snapshot,
      Variable.Type completionType,
      String query,
      int maxSuggestions,
      @Nullable CompletionMetadata completionMetadata,
      @Nullable NodeRolesData nodeRolesData,
      @Nullable ReferenceLibrary referenceLibrary); @Nonnull static List<AutocompleteSuggestion> autoComplete(
      @Nullable String network,
      @Nullable String snapshot,
      Variable.Type completionType,
      String query,
      int maxSuggestions,
      @Nullable CompletionMetadata completionMetadata,
      @Nullable NodeRolesData nodeRolesData,
      @Nullable ReferenceLibrary referenceLibrary,
      boolean fuzzyMatching); @Nonnull static List<AutocompleteSuggestion> baseAutoComplete(
      @Nullable String query, Set<String> allProperties); @Nonnull static List<AutocompleteSuggestion> stringAutoComplete(
      @Nullable String query, Set<String> strings); @Nonnull static List<AutocompleteSuggestion> stringAutoComplete(
      @Nullable String query, Map<String, Optional<String>> stringsWithDescriptions); @Nonnull static ImmutableList<AutocompleteSuggestion> ipStringAutoComplete(
      @Nullable String query, Map<Ip, IpCompletionMetadata> ips);  }