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:
Lorenzo Colitti
2021-01-27 01:15:03 +00:00
committed by Automerger Merge Worker
2 changed files with 18 additions and 0 deletions

View File

@@ -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());

View File

@@ -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.