@JsonCreator private static @Nonnull LiteralCommunity create(@JsonProperty(PROP_COMMUNITY) Long community) { return new LiteralCommunity(requireNonNull(community)); }
@Override public CommunityVar visitLiteralCommunity(LiteralCommunity literalCommunity) { return toCommunityVar(literalCommunity.getCommunity()); }
private static CommunityListLine toCommunityListLine(StandardCommunityListLine sclLine) { Collection<Long> lineCommunities = sclLine.getCommunities(); CommunitySetExpr expr = lineCommunities.size() == 1 ? new LiteralCommunity(lineCommunities.iterator().next()) : new LiteralCommunityConjunction(lineCommunities); return new CommunityListLine(sclLine.getAction(), expr); }
@Override public RegexCommunitySet visitLiteralCommunity(LiteralCommunity literalCommunity) { return new RegexCommunitySet( String.format("^%s$", CommonUtil.longToCommunity(literalCommunity.getCommunity()))); }
@Test public void testMatchedCommunities() { LiteralCommunity l = new LiteralCommunity(1L); Set<Long> communityCandidates = ImmutableSet.of(1L, 2L); assertThat(l, matchedCommunities(null, communityCandidates, ImmutableSet.of(1L))); } }
@Test public void testReject() { CommunityList cl = new CommunityList( NAME, ImmutableList.of( new CommunityListLine(LineAction.DENY, new LiteralCommunity(COMMUNITY1))), false); assertThat(cl.matchCommunity(null, COMMUNITY1), equalTo(false)); } }
@Test public void testMatchAnyCommunity() { LiteralCommunity l = new LiteralCommunity(1L); Set<Long> communityCandidates = ImmutableSet.of(1L, 2L); assertThat(l, matchAnyCommunity(null, communityCandidates)); }
@Test public void testMatchedCommunitiesSupported() { CommunityList accepting = new CommunityList( "", ImmutableList.of(CommunityListLine.accepting(new LiteralCommunity(1L))), false); Set<Long> communityCandidates = ImmutableSet.of(1L, 2L); assertThat(accepting, matchedCommunities(null, communityCandidates, ImmutableSet.of(1L))); }
@Test public void testAsLiteralCommunities() { long val = 1L; LiteralCommunity l = new LiteralCommunity(val); assertThat(l, asLiteralCommunities(null, equalTo(ImmutableSet.of(val)))); }
@Test public void testPermit() { CommunityList cl = new CommunityList( NAME, ImmutableList.of( new CommunityListLine(LineAction.PERMIT, new LiteralCommunity(COMMUNITY1))), false); assertThat(cl.matchCommunity(null, COMMUNITY1), equalTo(true)); }
@Test public void testMatchCommunities() { Set<Long> matchingCommunitySetCandidate1 = ImmutableSet.of(1L, 2L); Set<Long> matchingCommunitySetCandidate2 = ImmutableSet.of(1L); Set<Long> nonMatchingCommunitySetCandidate = ImmutableSet.of(2L); LiteralCommunity l = new LiteralCommunity(1L); assertThat(l, matchCommunities(null, matchingCommunitySetCandidate1)); assertThat(l, matchCommunities(null, matchingCommunitySetCandidate2)); assertThat(l, not(matchCommunities(null, nonMatchingCommunitySetCandidate))); }
@Test public void testEquals() { new EqualsTester() .addEqualityGroup(new LiteralCommunity(1L), new LiteralCommunity(1L)) .addEqualityGroup(new LiteralCommunity(2L)) .testEquals(); }
@Test public void testAsLiteralCommunitiesSupported() { CommunityList expr = new CommunityList( "", ImmutableList.of(CommunityListLine.accepting(new LiteralCommunity(1L))), false); assertThat(expr, asLiteralCommunities(null, equalTo(ImmutableSet.of(1L)))); }
@Test public void testMatchAnyCommunity() { CommunityList accepting = new CommunityList( "", ImmutableList.of(CommunityListLine.accepting(new LiteralCommunity(1L))), false); Set<Long> communityCandidates = ImmutableSet.of(1L, 2L); assertThat(accepting, matchAnyCommunity(null, communityCandidates)); }
@Test public void testMatchCommunity() { LiteralCommunity l = new LiteralCommunity(1L); assertThat(l, matchCommunity(null, 1L)); assertThat(l, not(matchCommunity(null, 2L))); }
@Test public void testMatchCommunities() { Set<Long> matchingCommunitySetCandidate1 = ImmutableSet.of(1L, 2L); Set<Long> matchingCommunitySetCandidate2 = ImmutableSet.of(1L); Set<Long> nonMatchingCommunitySetCandidate = ImmutableSet.of(2L); LiteralCommunity l = new LiteralCommunity(1L); assertThat(l, matchCommunities(null, matchingCommunitySetCandidate1)); assertThat(l, matchCommunities(null, matchingCommunitySetCandidate2)); assertThat(l, not(matchCommunities(null, nonMatchingCommunitySetCandidate))); }
@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 testEquals() { new EqualsTester() .addEqualityGroup( new CommunityList( "", ImmutableList.of(CommunityListLine.accepting(new LiteralCommunity(1L))), false), new CommunityList( "", ImmutableList.of(CommunityListLine.accepting(new LiteralCommunity(1L))), false)) .addEqualityGroup( new CommunityList( "a", ImmutableList.of(CommunityListLine.accepting(new LiteralCommunity(1L))), false)) .addEqualityGroup(new CommunityList("", ImmutableList.of(), false)) .addEqualityGroup(new CommunityList("", ImmutableList.of(), true)) .testEquals(); }
CommunityList accepting = new CommunityList( "", ImmutableList.of(CommunityListLine.accepting(new LiteralCommunity(1L))), false); CommunityList acceptingSecond = new CommunityList( ImmutableList.of( new CommunityListLine(LineAction.DENY, EmptyCommunitySetExpr.INSTANCE), CommunityListLine.accepting(new LiteralCommunity(1L))), false); CommunityList rejecting = new CommunityList( "", ImmutableList.of(new CommunityListLine(LineAction.DENY, new LiteralCommunity(1L))), false); CommunityList empty = new CommunityList("", ImmutableList.of(), false);
@Test public void testMatchCommunity() { CommunityList accepting = new CommunityList( "", ImmutableList.of(CommunityListLine.accepting(new LiteralCommunity(1L))), false); CommunityList acceptingSecond = new CommunityList( "", ImmutableList.of( new CommunityListLine(LineAction.DENY, EmptyCommunitySetExpr.INSTANCE), CommunityListLine.accepting(new LiteralCommunity(1L))), false); CommunityList rejecting = new CommunityList( "", ImmutableList.of(new CommunityListLine(LineAction.DENY, new LiteralCommunity(1L))), false); CommunityList empty = new CommunityList("", ImmutableList.of(), false); assertThat(accepting, matchCommunity(null, 1L)); assertThat(accepting, not(matchCommunity(null, 2L))); assertThat(acceptingSecond, matchCommunity(null, 1L)); assertThat(acceptingSecond, not(matchCommunity(null, 2L))); assertThat(rejecting, not(matchCommunity(null, 1L))); assertThat(rejecting, not(matchCommunity(null, 2L))); assertThat(empty, not(matchCommunity(null, 1L))); assertThat(empty, not(matchCommunity(null, 2L))); }