@Test public void testLimitSuggestionsByType() { List<AutocompleteSuggestion> orderedSuggestions = ImmutableList.of( AutocompleteSuggestion.builder() .setText("t1s1") .setSuggestionType(SuggestionType.CONSTANT) .build(), AutocompleteSuggestion.builder() .setText("t1s2") .setSuggestionType(SuggestionType.CONSTANT) .build(), AutocompleteSuggestion.builder() .setText("t1s3") .setSuggestionType(SuggestionType.CONSTANT) .build(), AutocompleteSuggestion.builder() .setText("t2s1") .setSuggestionType(SuggestionType.NAME_LITERAL) .build(), AutocompleteSuggestion.builder() .setText("t2s2") .setSuggestionType(SuggestionType.NAME_LITERAL) .build(), AutocompleteSuggestion.builder() .setText("t3s1") .setSuggestionType(SuggestionType.FUNCTION) .build(), AutocompleteSuggestion.builder() .setText("t3s2") .setSuggestionType(SuggestionType.FUNCTION) .build()); assertThat( AutoCompleteUtils.limitSuggestionsByType(orderedSuggestions, 4, 1).stream() .map(AutocompleteSuggestion::getText) .collect(ImmutableList.toImmutableList()), equalTo(ImmutableList.of("t1s1", "t2s1", "t3s1", "t1s2"))); }
@VisibleForTesting static List<AutocompleteSuggestion> limitSuggestionsByType( List<AutocompleteSuggestion> orderedSuggestions, int maxSuggestions, int maxSuggestionsPerType) { if (orderedSuggestions.size() <= maxSuggestions) { return orderedSuggestions; } Map<SuggestionType, Integer> suggestionTypeCounts = new TreeMap<>(); List<AutocompleteSuggestion> limitedSuggestions = new ArrayList<>(); List<AutocompleteSuggestion> leftOverSuggestions = new ArrayList<>(); for (AutocompleteSuggestion suggestion : orderedSuggestions) { if (limitedSuggestions.size() == maxSuggestions) { return limitedSuggestions; } Integer count = suggestionTypeCounts.getOrDefault(suggestion.getSuggestionType(), 0); if (count < maxSuggestionsPerType) { limitedSuggestions.add(suggestion); suggestionTypeCounts.put(suggestion.getSuggestionType(), count + 1); } else { leftOverSuggestions.add(suggestion); } } int suggestionsToAdd = maxSuggestions - limitedSuggestions.size(); if (suggestionsToAdd > 0) { limitedSuggestions.addAll( leftOverSuggestions.subList( 0, Integer.min(suggestionsToAdd, leftOverSuggestions.size()))); } return limitedSuggestions; }
AutoCompleteUtils { @VisibleForTesting static List<AutocompleteSuggestion> limitSuggestionsByType( List<AutocompleteSuggestion> orderedSuggestions, int maxSuggestions, int maxSuggestionsPerType) { if (orderedSuggestions.size() <= maxSuggestions) { return orderedSuggestions; } Map<SuggestionType, Integer> suggestionTypeCounts = new TreeMap<>(); List<AutocompleteSuggestion> limitedSuggestions = new ArrayList<>(); List<AutocompleteSuggestion> leftOverSuggestions = new ArrayList<>(); for (AutocompleteSuggestion suggestion : orderedSuggestions) { if (limitedSuggestions.size() == maxSuggestions) { return limitedSuggestions; } Integer count = suggestionTypeCounts.getOrDefault(suggestion.getSuggestionType(), 0); if (count < maxSuggestionsPerType) { limitedSuggestions.add(suggestion); suggestionTypeCounts.put(suggestion.getSuggestionType(), count + 1); } else { leftOverSuggestions.add(suggestion); } } int suggestionsToAdd = maxSuggestions - limitedSuggestions.size(); if (suggestionsToAdd > 0) { limitedSuggestions.addAll( leftOverSuggestions.subList( 0, Integer.min(suggestionsToAdd, leftOverSuggestions.size()))); } return limitedSuggestions; } }
AutoCompleteUtils { @VisibleForTesting static List<AutocompleteSuggestion> limitSuggestionsByType( List<AutocompleteSuggestion> orderedSuggestions, int maxSuggestions, int maxSuggestionsPerType) { if (orderedSuggestions.size() <= maxSuggestions) { return orderedSuggestions; } Map<SuggestionType, Integer> suggestionTypeCounts = new TreeMap<>(); List<AutocompleteSuggestion> limitedSuggestions = new ArrayList<>(); List<AutocompleteSuggestion> leftOverSuggestions = new ArrayList<>(); for (AutocompleteSuggestion suggestion : orderedSuggestions) { if (limitedSuggestions.size() == maxSuggestions) { return limitedSuggestions; } Integer count = suggestionTypeCounts.getOrDefault(suggestion.getSuggestionType(), 0); if (count < maxSuggestionsPerType) { limitedSuggestions.add(suggestion); suggestionTypeCounts.put(suggestion.getSuggestionType(), count + 1); } else { leftOverSuggestions.add(suggestion); } } int suggestionsToAdd = maxSuggestions - limitedSuggestions.size(); if (suggestionsToAdd > 0) { limitedSuggestions.addAll( leftOverSuggestions.subList( 0, Integer.min(suggestionsToAdd, leftOverSuggestions.size()))); } return limitedSuggestions; }  }
AutoCompleteUtils { @VisibleForTesting static List<AutocompleteSuggestion> limitSuggestionsByType( List<AutocompleteSuggestion> orderedSuggestions, int maxSuggestions, int maxSuggestionsPerType) { if (orderedSuggestions.size() <= maxSuggestions) { return orderedSuggestions; } Map<SuggestionType, Integer> suggestionTypeCounts = new TreeMap<>(); List<AutocompleteSuggestion> limitedSuggestions = new ArrayList<>(); List<AutocompleteSuggestion> leftOverSuggestions = new ArrayList<>(); for (AutocompleteSuggestion suggestion : orderedSuggestions) { if (limitedSuggestions.size() == maxSuggestions) { return limitedSuggestions; } Integer count = suggestionTypeCounts.getOrDefault(suggestion.getSuggestionType(), 0); if (count < maxSuggestionsPerType) { limitedSuggestions.add(suggestion); suggestionTypeCounts.put(suggestion.getSuggestionType(), count + 1); } else { leftOverSuggestions.add(suggestion); } } int suggestionsToAdd = maxSuggestions - limitedSuggestions.size(); if (suggestionsToAdd > 0) { limitedSuggestions.addAll( leftOverSuggestions.subList( 0, Integer.min(suggestionsToAdd, leftOverSuggestions.size()))); } return limitedSuggestions; }  @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> limitSuggestionsByType( List<AutocompleteSuggestion> orderedSuggestions, int maxSuggestions, int maxSuggestionsPerType) { if (orderedSuggestions.size() <= maxSuggestions) { return orderedSuggestions; } Map<SuggestionType, Integer> suggestionTypeCounts = new TreeMap<>(); List<AutocompleteSuggestion> limitedSuggestions = new ArrayList<>(); List<AutocompleteSuggestion> leftOverSuggestions = new ArrayList<>(); for (AutocompleteSuggestion suggestion : orderedSuggestions) { if (limitedSuggestions.size() == maxSuggestions) { return limitedSuggestions; } Integer count = suggestionTypeCounts.getOrDefault(suggestion.getSuggestionType(), 0); if (count < maxSuggestionsPerType) { limitedSuggestions.add(suggestion); suggestionTypeCounts.put(suggestion.getSuggestionType(), count + 1); } else { leftOverSuggestions.add(suggestion); } } int suggestionsToAdd = maxSuggestions - limitedSuggestions.size(); if (suggestionsToAdd > 0) { limitedSuggestions.addAll( leftOverSuggestions.subList( 0, Integer.min(suggestionsToAdd, leftOverSuggestions.size()))); } return limitedSuggestions; }  @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);  }