Introduce UUIDMap for storing objects that are keyed by a UUID

This patch introduces the new ``UUIDMap`` type, reducing code duplication and
making UUID lookups faster. We currently already use UUIDs as the identifier for
the ``DatabaseEntry`` and ``Slot`` types, but the way lookups by UUID work are
kind of ugly, as we simply iterate over the list until we find a match. As we're
probably going to have more types like this soon (groups and icons, for
example), I figured it'd be good to abstract this away into a separate type and
make it a map instead of a list.

The only thing that has gotten slower is the ``swap`` method. The internal
``LinkedHashMap`` retains insertion order with a linked list, but does not know
about the position of the values, so we basically have to copy the entire map to
simply swap two values. I don't think it's too big of a deal, because swap
operations still take less than a millisecond even with large vaults, but
suggestions for improving this are welcome.

I had to update gradle and JUnit to be able to use the new ``assertThrows``
assertion method, so this patch includes that as well.
This commit is contained in:
Alexander Bakker 2019-06-10 18:25:44 +02:00
parent 6769fefd00
commit 2323d89938
21 changed files with 375 additions and 243 deletions

View file

@ -38,6 +38,7 @@ import com.beemdevelopment.aegis.importers.DatabaseImporterException;
import com.beemdevelopment.aegis.services.NotificationService;
import com.beemdevelopment.aegis.ui.models.ImportEntry;
import com.beemdevelopment.aegis.ui.preferences.SwitchPreference;
import com.beemdevelopment.aegis.util.UUIDMap;
import com.takisoft.preferencex.PreferenceFragmentCompat;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.io.SuFile;
@ -76,7 +77,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat {
// keep a reference to the type of database converter the user selected
private Class<? extends DatabaseImporter> _importerType;
private AegisImporter.State _importerState;
private List<DatabaseEntry> _importerEntries;
private UUIDMap<DatabaseEntry> _importerEntries;
private SwitchPreference _encryptionPreference;
private SwitchPreference _fingerprintPreference;
@ -620,20 +621,10 @@ public class PreferencesFragment extends PreferenceFragmentCompat {
List<ImportEntry> selectedEntries = (ArrayList<ImportEntry>) data.getSerializableExtra("entries");
for (ImportEntry selectedEntry : selectedEntries) {
DatabaseEntry savedEntry = null;
for (DatabaseEntry entry : _importerEntries) {
if (entry.getUUID().equals(selectedEntry.getUUID())) {
savedEntry = entry;
break;
}
}
if (savedEntry == null) {
throw new RuntimeException();
}
DatabaseEntry savedEntry = _importerEntries.getByUUID(selectedEntry.getUUID());
// temporary: randomize the UUID of duplicate entries and add them anyway
if (_db.getEntryByUUID(savedEntry.getUUID()) != null) {
if (_db.isEntryDuplicate(savedEntry)) {
savedEntry.resetUUID();
}