MatcherUtilsImpl implements MatcherUtils { @Override public <T, M extends Matchable<T>> Optional<Double> getMostSpecificMatchSpecificity(@Nonnull Iterable<M> matchables, @Nonnull T target) { checkNotNull(matchables, "matchables is null"); checkNotNull(target, "target is null"); Optional<Double> maxSpecificity = absent(); for (M matchable : matchables) { Optional<Double> specificity = getMatchSpecificity(matchable.getMatcher(), target); if (specificity.isPresent() && (!maxSpecificity.isPresent() || specificity.get() > maxSpecificity.get())) { maxSpecificity = specificity; } } return maxSpecificity; }  @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 givenGroupIsNull_whenGetMostSpecificMatchSpecificity_thenThrowsNPE() { List<Matchable<String>> group = null; String agent = "googlebot"; utilities.getMostSpecificMatchSpecificity(group, agent); }
@Test(expected = NullPointerException.class) public void givenAgentIsNull_whenGetMostSpecificMatchSpecificity_thenThrowsNPE() { List<Matchable<String>> group = ImmutableList.of(); String agent = null; utilities.getMostSpecificMatchSpecificity(group, agent); }
@Test public void givenGroupIsEmpty_whenGetMostSpecificMatchSpecificity_thenReturnsAbsent() { List<Matchable<String>> group = ImmutableList.of(); String agent = "googlebot"; Optional<Double> longestMatch = utilities.getMostSpecificMatchSpecificity(group, agent); assertThat(longestMatch, equalTo(Optional.<Double>absent())); }
@Test public void givenSingltonWildcardGroup_whenGetMostSpecificMatchSpecificity_thenReturnsZero() { List<Matchable<String>> group = ImmutableList .<Matchable<String>>builder() .add(new Matchable<String>() { @Override public Matcher<String> getMatcher() { return newPatternMatcher("*"); } }) .build(); String agent = "googlebot"; Optional<Double> longestMatch = utilities.getMostSpecificMatchSpecificity(group, agent); assertThat(longestMatch.get(), equalTo(0.0)); }