Make replaceKey and removeKey functions rely on id's instead of instances

This commit is contained in:
Alexander Bakker 2018-01-02 14:36:56 +01:00
parent 7bc4f19cf0
commit e3024eda47
3 changed files with 36 additions and 30 deletions

View file

@ -66,17 +66,13 @@ public class Database {
}
public void removeKey(DatabaseEntry entry) {
entry = getKeyByID(entry.getID());
_entries.remove(entry);
}
public void replaceKey(DatabaseEntry newEntry) {
for (DatabaseEntry oldEntry : _entries) {
if (oldEntry.getID() == newEntry.getID()) {
_entries.set(_entries.indexOf(oldEntry), newEntry);
return;
}
}
throw new AssertionError("no entry found with the same id");
DatabaseEntry oldEntry = getKeyByID(newEntry.getID());
_entries.set(_entries.indexOf(oldEntry), newEntry);
}
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) {
@ -86,4 +82,13 @@ public class Database {
public List<DatabaseEntry> getKeys() {
return Collections.unmodifiableList(_entries);
}
private DatabaseEntry getKeyByID(long id) {
for (DatabaseEntry entry : _entries) {
if (entry.getID() == id) {
return entry;
}
}
throw new AssertionError("no entry found with the same id");
}
}