Add removeAll to preferences (#987)

This removes all keys except for .type.
This commit is contained in:
Austin Shalit
2018-05-16 22:50:35 -04:00
committed by Peter Johnson
parent 2e0709f05b
commit f90e429bf9
4 changed files with 43 additions and 0 deletions

View File

@@ -222,3 +222,14 @@ bool Preferences::ContainsKey(wpi::StringRef key) {
* @param key the key
*/
void Preferences::Remove(wpi::StringRef key) { m_table->Delete(key); }
/**
* Remove all preferences.
*/
void Preferences::RemoveAll() {
for (auto preference : GetKeys()) {
if (preference != ".type") {
Remove(preference);
}
}
}

View File

@@ -52,6 +52,7 @@ class Preferences : public ErrorBase {
void PutLong(wpi::StringRef key, int64_t value);
bool ContainsKey(wpi::StringRef key);
void Remove(wpi::StringRef key);
void RemoveAll();
protected:
Preferences();

View File

@@ -173,6 +173,17 @@ public class Preferences {
m_table.delete(key);
}
/**
* Remove all preferences.
*/
public void removeAll() {
for (String key : m_table.getKeys()) {
if (!".type".equals(key)) {
remove(key);
}
}
}
/**
* Returns the string at the given key. If this table does not have a value for that position,
* then the given backup value will be returned.

View File

@@ -7,6 +7,7 @@
package edu.wpi.first.wpilibj;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
@@ -88,6 +89,25 @@ public class PreferencesTest extends AbstractComsSetup {
m_pref.putBoolean("checkedValueBoolean", true);
}
/**
* Just checking to make sure our helper method works.
*/
@Test
public void testRemove() {
remove();
assertEquals("Preferences was not empty! Preferences in table: "
+ Arrays.toString(m_pref.getKeys().toArray()),
1, m_pref.getKeys().size());
}
@Test
public void testRemoveAll() {
m_pref.removeAll();
assertEquals("Preferences was not empty! Preferences in table: "
+ Arrays.toString(m_pref.getKeys().toArray()),
1, m_pref.getKeys().size());
}
@Test
public void testAddRemoveSave() {
assertEquals(m_pref.getLong("checkedValueLong", 0), 172L);