Better search backup group implementation

This commit is contained in:
J-Jamet
2021-04-19 11:11:07 +02:00
parent e73b9b7f1c
commit 6397feffff
2 changed files with 22 additions and 13 deletions

View File

@@ -178,19 +178,9 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
* Retrieve backup group with his name * Retrieve backup group with his name
*/ */
private fun retrieveBackup(): GroupKDB? { private fun retrieveBackup(): GroupKDB? {
var backupGroup: GroupKDB? = null return rootGroup?.searchChildGroup {
val groupHandler = object: NodeHandler<GroupKDB>() { it.title.equals(BACKUP_FOLDER_TITLE, ignoreCase = true)
override fun operate(node: GroupKDB): Boolean {
return if (node.title.equals(BACKUP_FOLDER_TITLE, ignoreCase = true)) {
backupGroup = node
false
} else {
true
}
}
} }
rootGroup?.doForEachChild(null, groupHandler)
return backupGroup
} }
/** /**

View File

@@ -62,8 +62,27 @@ interface GroupVersionedInterface<Group: GroupVersionedInterface<Group, Entry>,
return false return false
} }
if (doActionForChild) if (doActionForChild)
group.doForEachChild(entryHandler, groupHandler) group.doForEachChild(entryHandler, groupHandler, stopIterationWhenGroupHandlerOperateFalse)
} }
return true return true
} }
fun searchChildGroup(criteria: (group: Group) -> Boolean): Group? {
return searchChildGroup(this, criteria)
}
private fun searchChildGroup(rootGroup: GroupVersionedInterface<Group, Entry>,
criteria: (group: Group) -> Boolean): Group? {
for (childGroup in rootGroup.getChildGroups()) {
if (criteria.invoke(childGroup)) {
return childGroup
} else {
val subGroup = searchChildGroup(childGroup, criteria)
if (subGroup != null) {
return subGroup
}
}
}
return null
}
} }