TextUnitBatchMatcher { public Function<TextUnitForBatchMatcher, Optional<TextUnitDTO>> match(List<TextUnitDTO> existingTextUnits) { Function<TextUnitForBatchMatcher, Optional<TextUnitDTO>> matchByTmTextUnitId = createMatchByTmTextUnitId(existingTextUnits); Function<TextUnitForBatchMatcher, Optional<TextUnitDTO>> matchByNameAndUsed = createMatchByNameAndUsed(existingTextUnits); Function<TextUnitForBatchMatcher, Optional<TextUnitDTO>> matchByNameAndUnused = createMatchByNameAndUnused(existingTextUnits); Predicate<TextUnitDTO> notAlreadyMatched = notAlreadyMatched("global"); return textUnitForBatchMatcher -> { return Optionals.or(textUnitForBatchMatcher, matchByTmTextUnitId, matchByNameAndUsed, matchByNameAndUnused).filter(notAlreadyMatched); }; }  Function<TextUnitForBatchMatcher, Optional<TextUnitDTO>> match(List<TextUnitDTO> existingTextUnits); Function<TextUnitForBatchMatcher, List<TextUnitDTO>> matchByNameAndPluralPrefix(List<TextUnitDTO> existingTextUnits);  }
@Test public void testMatch() { List<TextUnitDTO> existingTextUnitDTOs = Arrays.asList( createTextUnitDTO("name-0"), createTextUnitDTO("name-1"), createTextUnitDTO("will map by tmTextUnit id", 3L), createUnusedTextUnitDTO("name-4")); Function<TextUnitForBatchMatcher, Optional<TextUnitDTO>> matchByPluralPrefix = textUnitBatchMatcher.match(existingTextUnitDTOs); TextUnitDTO name0 = matchByPluralPrefix.apply(createTextUnitForBatchMatcher("name-0")).get(); assertEquals("name-0", name0.getName()); Optional<TextUnitDTO> name0SecondTime = matchByPluralPrefix.apply(createTextUnitForBatchMatcher("name-0")); assertFalse(name0SecondTime.isPresent()); TextUnitDTO name1 = matchByPluralPrefix.apply(createTextUnitForBatchMatcher("name-1")).get(); assertEquals("name-1", name1.getName()); TextUnitDTO name3 = matchByPluralPrefix.apply(createTextUnitForBatchMatcher("will map tmTextUnit id (diff)", 3L)).get(); assertEquals("will map by tmTextUnit id", name3.getName()); TextUnitDTO name4 = matchByPluralPrefix.apply(createTextUnitForBatchMatcher("name-4")).get(); assertEquals("name-4", name4.getName()); }