PersistentSuggestionSource implements SuggestionSource { @SafeVarargs public final void add(T... values) { final List<T> loadedValues = loadValues(); final List<T> toAdd = Arrays.asList(values); loadedValues.removeAll(toAdd); List<T> result = new ArrayList<T>(toAdd); result.addAll(loadedValues); LinkedHashSet<T> r = new LinkedHashSet<T>(result); final List<String> collect = r.stream() .map(serializer) .map(StringEscapeUtils::escapeCsv) .collect(toList()); try { final StringWriter out = new StringWriter(); CSVFormat.DEFAULT.print(out).printRecords(collect); final String content = out.toString(); persistService.persist(persistKey, content, (s) -> s); } catch (Exception e) { LOGGER.error("Can't store suggestions for " + persistKey, e); } } PersistentSuggestionSource(
      String persistKey,
      PersistService persistService,
      BiFunction<T, SuggestionQuery, Boolean> filterFunction,
      Function<T, String> serializer,
      Function<String, T> deserializer); @Override List<T> getSuggestions(SuggestionQuery query); @SafeVarargs final void add(T... values);  }
@Test public void testAdd() throws Exception { when(persistService.load(eq("key"), eq(""), any(Deserializer.class))).thenReturn("1111\n1112\n3232"); suggestionSource.add(1134, 5454); final List<Integer> suggestions = suggestionSource.getSuggestions(new SuggestionQuery("11", 0)); verify(persistService, times(1)).persist(eq("key"), eq("1134\r\n5454\r\n1111\r\n1112\r\n3232\r\n"), any(Serializer.class)); }