HibernateSimpleQueryUtils { public static String createLuceneQueryString(Collection<String> values, String operator, Function<String, String> function) { if(values.isEmpty()) { return ""; } var mappedValues = values.stream() .map(function) .distinct() .collect(Collectors.toList()); var mappedValueArr = mappedValues.toArray(String[]::new); var firstValue = mappedValueArr[0]; var fieldMatchStringBuilder = new StringBuilder(firstValue); for(var valueIndex = 1; valueIndex < mappedValueArr.length; valueIndex++) { fieldMatchStringBuilder .append(" ") .append(operator) .append(" ") .append(mappedValueArr[valueIndex]); } return fieldMatchStringBuilder.toString(); }  static String makeQueryPrefixAndFuzzy(@NonNull String simpleQuery); static String createLuceneQueryString(Collection<String> values, String operator,
                                                  Function<String, String> function); static String createLuceneQueryString(Collection<String> values, String operator);  }
@Test public void createLuceneQueryStringReturnsEmptyStringWhenValuesEmpty() { var actualQueryString = HibernateSimpleQueryUtils.createLuceneQueryString(List.of(), "+", Function.identity()); assertThat(actualQueryString).isEmpty(); }
@Test public void createLuceneQueryStringReturnsExpectedQueryAndUsesFunction() { var values = List.of("value1", "value2"); var operator = "|"; var expectedQueryString = "-value1 | -value2"; Function<String, String> function = (String value) -> '-' + value; var actualQueryString = HibernateSimpleQueryUtils.createLuceneQueryString(values, operator, function); assertThat(actualQueryString).isEqualTo(expectedQueryString); }