@JsonCreator private static @Nonnull CommunityHalvesExpr create( @JsonProperty(PROP_LEFT) CommunityHalfExpr left, @JsonProperty(PROP_RIGHT) CommunityHalfExpr right) { return new CommunityHalvesExpr(requireNonNull(left), requireNonNull(right)); }
@Override public boolean matchCommunities(Environment environment, Set<Long> communitySetCandidate) { return communitySetCandidate.stream() .anyMatch(communityCandidate -> matchCommunity(environment, communityCandidate)); }
@Test public void testAsLiteralCommunities() { CommunityHalvesExpr r = new CommunityHalvesExpr(new LiteralCommunityHalf(1), new LiteralCommunityHalf(2)); _thrown.expect(UnsupportedOperationException.class); r.asLiteralCommunities(null); }
@Override public CommunitySetExpr toCommunitySetExpr() { if (_prefix instanceof LiteralCommunitySetElemHalf && _suffix instanceof LiteralCommunitySetElemHalf) { LiteralCommunitySetElemHalf prefix = (LiteralCommunitySetElemHalf) _prefix; LiteralCommunitySetElemHalf suffix = (LiteralCommunitySetElemHalf) _suffix; int prefixInt = prefix.getValue(); int suffixInt = suffix.getValue(); return new LiteralCommunity((((long) prefixInt) << 16) | suffixInt); } else { return new CommunityHalvesExpr(_prefix.toCommunityHalfExpr(), _suffix.toCommunityHalfExpr()); } } }
@Test public void testMatchCommunities() { Set<Long> matchingCommunitySetCandidate1 = ImmutableSet.of(communityStringToLong("1:1"), communityStringToLong("2:2")); Set<Long> matchingCommunitySetCandidate2 = ImmutableSet.of(communityStringToLong("1:1")); Set<Long> nonMatchingCommunitySetCandidate = ImmutableSet.of(communityStringToLong("2:2")); CommunityHalvesExpr expr = new CommunityHalvesExpr(new LiteralCommunityHalf(1), new LiteralCommunityHalf(1)); assertThat(expr, matchCommunities(null, matchingCommunitySetCandidate1)); assertThat(expr, matchCommunities(null, matchingCommunitySetCandidate2)); assertThat(expr, not(matchCommunities(null, nonMatchingCommunitySetCandidate))); }
@Test public void testMatchedCommunities() { CommunityHalvesExpr expr = new CommunityHalvesExpr(new LiteralCommunityHalf(1), new LiteralCommunityHalf(1)); Set<Long> communityCandidates = ImmutableSet.of(communityStringToLong("1:1"), 2L); assertThat( expr, matchedCommunities( null, communityCandidates, ImmutableSet.of(communityStringToLong("1:1")))); } }
@Test public void testMatchCommunity() { CommunityHalvesExpr expr = new CommunityHalvesExpr(new LiteralCommunityHalf(1), new LiteralCommunityHalf(1)); assertThat(expr, matchCommunity(null, communityStringToLong("1:1"))); assertThat(expr, not(matchCommunity(null, 2L))); }
@Test public void testMatchAnyCommunity() { CommunityHalvesExpr expr = new CommunityHalvesExpr(new LiteralCommunityHalf(1), new LiteralCommunityHalf(1)); Set<Long> communityCandidates = ImmutableSet.of(communityStringToLong("1:1"), 2L); assertThat(expr, matchAnyCommunity(null, communityCandidates)); }
@Test public void testEquals() { new EqualsTester() .addEqualityGroup( new CommunityHalvesExpr(new LiteralCommunityHalf(1), new LiteralCommunityHalf(2)), new CommunityHalvesExpr(new LiteralCommunityHalf(1), new LiteralCommunityHalf(2))) .addEqualityGroup( new CommunityHalvesExpr(new LiteralCommunityHalf(1), new LiteralCommunityHalf(3))) .testEquals(); }