android: Add ability to remove a range set from another

This commit is contained in:
Tobias Brunner 2017-06-21 16:22:11 +02:00
parent 78b20efb29
commit 54714331e4
2 changed files with 36 additions and 0 deletions

View File

@ -108,6 +108,22 @@ public class IPRangeSet
mRanges.addAll(additions);
}
/**
* Remove the given ranges from ranges in this set.
*/
public void remove(IPRangeSet ranges)
{
if (ranges == this)
{
mRanges.clear();
return;
}
for (IPRange range : ranges.mRanges)
{
remove(range);
}
}
/**
* Returns the subnets derived from all the ranges in this set.
*/

View File

@ -166,6 +166,26 @@ public class IPRangeSetTest
assertSubnets(set.getSubnets(), new IPRange("192.168.1.0/25"), new IPRange("192.168.2.128/25"));
}
@Test
public void testRemoveRangesIdent() throws UnknownHostException
{
IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 192.168.4.0/24");
set.remove(set);
assertEquals("size", 0, set.size());
assertSubnets(set.getSubnets());
}
@Test
public void testRemoveRanges() throws UnknownHostException
{
IPRangeSet set = IPRangeSet.fromString("192.168.0.0/16");
IPRangeSet remove = IPRangeSet.fromString("192.168.1.0/24 192.168.3.0/24 192.168.16.0-192.168.255.255");
set.remove(remove);
assertEquals("size", 3, set.size());
assertSubnets(set.getSubnets(), new IPRange("192.168.0.0/24"), new IPRange("192.168.2.0/24"),
new IPRange("192.168.4.0/22"), new IPRange("192.168.8.0/21"));
}
@Test
public void testFromStringSingle() throws UnknownHostException
{