KeyFunctionMatch implements Match { @Override public void init(final Iterable matchCandidates) { if (matchCandidates == null) { throw new IllegalArgumentException(NULL_MATCH_CANDIDATES_ERROR_MESSAGE); } for (final Object matchCandidate : matchCandidates) { Object key = secondKeyFunction.apply(matchCandidate); List list = keyedMatchCandidates.get(key); if (list == null) { list = new ArrayList(); } list.add(matchCandidate); keyedMatchCandidates.put(key, list); } } KeyFunctionMatch();  KeyFunctionMatch(final Function firstKeyFunction, final Function secondKeyFunction); @Override void init(final Iterable matchCandidates); @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "class") Function getFirstKeyFunction(); void setFirstKeyFunction(final Function firstKeyFunction); @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "class") Function getSecondKeyFunction(); void setSecondKeyFunction(final Function secondKeyFunction); @Override List matching(final Object testObject); @Override int hashCode(); @Override boolean equals(final Object obj);  }
@Test public void shouldThrowExceptionFromFunctionIfInputIsInvalid() { List<Long> testList = Lists.newArrayList(100L, 200L, 300L, null); KeyFunctionMatch match = new KeyFunctionMatch.Builder() .firstKeyFunction(new FunctionComposite( Lists.newArrayList(new CallMethod("getValue"), new ToInteger()))) .secondKeyFunction(new FunctionComposite(Lists.newArrayList(new ToInteger(), new DivideBy(10), new FirstItem<>()))) .build(); try { match.init(testList); } catch (final IllegalArgumentException e) { assertEquals("Input cannot be null", e.getMessage()); } }
@Test public void shouldThrowExceptionIfListIsNull() { KeyFunctionMatch match = new KeyFunctionMatch.Builder().build(); try { match.init(null); } catch (IllegalArgumentException e) { assertEquals("Iterable of match candidates cannot be null", e.getMessage()); } }