Add an isEmpty convenience method to BpfMap. am: 77262cea76
Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1560402 MUST ONLY BE SUBMITTED BY AUTOMERGER Change-Id: I41ee1a37f33950d725c3349a149eaa6c6ce2c355
This commit is contained in:
@@ -134,6 +134,11 @@ public class BpfMap<K extends Struct, V extends Struct> implements AutoCloseable
|
||||
return deleteMapEntry(mMapFd, key.writeToBytes());
|
||||
}
|
||||
|
||||
/** Returns {@code true} if this map contains no elements. */
|
||||
public boolean isEmpty() throws ErrnoException {
|
||||
return getFirstKey() == null;
|
||||
}
|
||||
|
||||
private K getNextKeyInternal(@Nullable K key) throws ErrnoException {
|
||||
final byte[] rawKey = getNextRawKey(
|
||||
key == null ? null : key.writeToBytes());
|
||||
|
||||
@@ -91,6 +91,7 @@ public final class BpfMapTest {
|
||||
}
|
||||
});
|
||||
assertNull(mTestMap.getFirstKey());
|
||||
assertTrue(mTestMap.isEmpty());
|
||||
}
|
||||
|
||||
private TetherDownstream6Key createTetherDownstream6Key(long iif, String address)
|
||||
@@ -135,6 +136,18 @@ public final class BpfMapTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmpty() throws Exception {
|
||||
assertNull(mTestMap.getFirstKey());
|
||||
assertTrue(mTestMap.isEmpty());
|
||||
|
||||
mTestMap.insertEntry(mTestData.keyAt(0), mTestData.valueAt(0));
|
||||
assertFalse(mTestMap.isEmpty());
|
||||
|
||||
mTestMap.deleteEntry((mTestData.keyAt(0)));
|
||||
assertTrue(mTestMap.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFirstKey() throws Exception {
|
||||
// getFirstKey on an empty map returns null.
|
||||
|
||||
Reference in New Issue
Block a user