TextUnitBatchMatcher { Function<TextUnitForBatchMatcher, Optional<List<TextUnitDTO>>> createMatchByPluralPrefixAndUsed(List<TextUnitDTO> existingTextUnits) { logger.debug("Create the map to match by prefix and used text units"); Map<String, List<TextUnitDTO>> pluralPrefixToUsedTextUnitDTOsMap = existingTextUnits.stream() .filter(TextUnitDTO::isUsed) .filter(t -> t.getPluralForm() != null) .collect(groupingBy(o -> pluralNameParser.getPrefix(o.getPluralFormOther()))); Predicate<TextUnitDTO> byPluralPreifxNotAlreadyMatched = notAlreadyMatched("byPluralPrefixAndUsed"); logger.debug("createMatchByPluralPrefixAndUsed"); return (textUnitForBatchMatcher) -> { Optional<List<TextUnitDTO>> optionalTextUnitDTOS = Optional.empty(); if (textUnitForBatchMatcher.isNamePluralPrefix()) { List<TextUnitDTO> textUnitDTOS = Optional.ofNullable(pluralPrefixToUsedTextUnitDTOsMap.get(textUnitForBatchMatcher.getName())).orElseGet(ArrayList::new); List<TextUnitDTO> filtered = textUnitDTOS.stream() .filter(byPluralPreifxNotAlreadyMatched) .collect(Collectors.toList()); if (!filtered.isEmpty()) { optionalTextUnitDTOS = Optional.of(filtered); } } return optionalTextUnitDTOS; }; }  Function<TextUnitForBatchMatcher, Optional<TextUnitDTO>> match(List<TextUnitDTO> existingTextUnits); Function<TextUnitForBatchMatcher, List<TextUnitDTO>> matchByNameAndPluralPrefix(List<TextUnitDTO> existingTextUnits);  }
@Test public void testMatchByPluralPrefixNoPlural() { List<TextUnitDTO> existingTextUnitDTOs = Arrays.asList( createTextUnitDTO("name-0"), createTextUnitDTO("name-1")); Function<TextUnitForBatchMatcher, Optional<List<TextUnitDTO>>> matchByPluralPrefix = textUnitBatchMatcher.createMatchByPluralPrefixAndUsed(existingTextUnitDTOs); Optional<List<TextUnitDTO>> result = matchByPluralPrefix.apply(createPluralTextUnitForBatchMatcher("name-0")); assertFalse(result.isPresent()); }
@Test public void testMatchByPluralPrefix() { List<TextUnitDTO> existingTextUnitDTOs = Arrays.asList( createTextUnitDTO("name-0"), createTextUnitDTO("name-1"), createPluralTextUnitDTO("name-2", "other"), createPluralTextUnitDTO("name-2", "one"), createPluralTextUnitDTO("name-3", "zero"), createPluralTextUnitDTO("name-3", "one"), createPluralTextUnitDTO("name-3", "two"), createPluralTextUnitDTO("name-3", "few"), createPluralTextUnitDTO("name-3", "many"), createPluralTextUnitDTO("name-3", "other") ); Function<TextUnitForBatchMatcher, Optional<List<TextUnitDTO>>> matchByPluralPrefix = textUnitBatchMatcher.createMatchByPluralPrefixAndUsed(existingTextUnitDTOs); List<TextUnitDTO> name2 = matchByPluralPrefix.apply(createPluralTextUnitForBatchMatcher("name-2")).get(); assertEquals(2, name2.size()); assertEquals("name-2_other", name2.get(0).getName()); assertEquals("name-2_one", name2.get(1).getName()); List<TextUnitDTO> name3 = matchByPluralPrefix.apply(createPluralTextUnitForBatchMatcher("name-3")).get(); assertEquals(6, name3.size()); assertEquals("name-3_zero", name3.get(0).getName()); assertEquals("name-3_one", name3.get(1).getName()); assertEquals("name-3_two", name3.get(2).getName()); assertEquals("name-3_few", name3.get(3).getName()); assertEquals("name-3_many", name3.get(4).getName()); assertEquals("name-3_other", name3.get(5).getName()); Optional<List<TextUnitDTO>> name3SecondTime = matchByPluralPrefix.apply(createPluralTextUnitForBatchMatcher("name-3")); assertFalse(name3SecondTime.isPresent()); }