MatcherUtilsImpl implements MatcherUtils { @Override @Nonnull public <T, M extends Matchable<T>, Group extends Iterable<M>> Optional<Group> getMostSpecificMatchingGroup(@Nonnull Iterable<Group> groups, @Nonnull T target) { checkNotNull(groups, "groups is null"); checkNotNull(target, "target is null"); Optional<Group> argMax = absent(); Optional<Double> maxSpecificity = absent(); for (Group group : groups) { Optional<Double> specificity = getMostSpecificMatchSpecificity(group, target); if (specificity.isPresent() && (!maxSpecificity.isPresent() || specificity.get() > maxSpecificity.get())) { maxSpecificity = specificity; argMax = of(group); } } return argMax; }  @Override @Nonnull Optional<Group> getMostSpecificMatchingGroup(@Nonnull Iterable<Group> groups, @Nonnull T target); @Override Optional<Double> getMostSpecificMatchSpecificity(@Nonnull Iterable<M> matchables, @Nonnull T target); @Override Optional<M> getMostSpecificMatch(@Nonnull Iterable<M> matchables, @Nonnull T target); @Override Optional<Double> getMatchSpecificity(@Nonnull Matcher<T> matcher, @Nonnull T target);  }
@Test(expected = NullPointerException.class) public void givenGroupsIsNull_whenGetMostSpecificMatchingGroup_thenThrowsNPE() { List<List<Matchable<String>>> groups = null; String agentString = "googlebot"; utilities.getMostSpecificMatchingGroup(groups, agentString); }
@Test(expected = NullPointerException.class) public void givenAgentIsNull_whenGetMostSpecificMatchingGroup_thenThrowsNPE() { List<List<Matchable<String>>> groups = Collections.emptyList(); String agentString = null; utilities.getMostSpecificMatchingGroup(groups, agentString); }
@Test public void givenGroupsAreEmpty_whenGetMostSpecificMatchingGroup_thenReturnsAbsent() { List<List<Matchable<String>>> groups = Collections.emptyList(); String agentString = "googlebot"; Optional<List<Matchable<String>>> result = utilities.getMostSpecificMatchingGroup(groups, agentString); assertThat(result, equalTo(Optional.<List<Matchable<String>>>absent())); }