Updated on 2026-08-14

This commit is contained in:
Tangem 2023-09-13 14:19:25 +03:00
parent 4b5adcd08a
commit 5ed381c263
56 changed files with 631 additions and 455 deletions

View file

@ -3,6 +3,8 @@ package com.tangem.utils.extensions
/**
* Removes an element from the collection based on the provided predicate.
*
* !!!This function is not thread-safe!!!
*
* @param predicate The condition to remove an element.
* @return [Boolean] indicating whether an element was removed.
*/
@ -33,7 +35,8 @@ inline fun <T> MutableList<T>.replaceBy(item: T, predicate: (T) -> Boolean): Boo
/**
* Adds the specified element to the list or replaces an existing element.
* The predicate defines the condition to replace the existing element.
*
* !!!This function is not thread-safe!!!
*
* @param item The element to be added or replace the existing one.
* @param predicate The condition to replace an existing element.

View file

@ -0,0 +1,39 @@
package com.tangem.utils.extensions
/**
* Replaces an element in the set with the provided item based on the predicate.
*
* !!!This function is not thread-safe!!!
*
* @param item The element to replace the existing one.
* @param predicate The condition to replace an existing element.
* @return [Boolean] indicating whether an element was replaced.
*/
inline fun <T> MutableSet<T>.replaceBy(item: T, predicate: (T) -> Boolean): Boolean {
val foundItem = firstOrNull(predicate) ?: return false
remove(foundItem)
add(item)
return true
}
/**
* Adds the specified element to the set or replaces an existing element.
*
* !!!This function is not thread-safe!!!
*
* @param item The element to be added or replace the existing one.
* @param predicate The condition to replace an existing element.
* @return The modified [Set] after adding or replacing the element.
*/
inline fun <T> Set<T>.addOrReplace(item: T, predicate: (T) -> Boolean): Set<T> {
val mutableList = this.toMutableSet()
val isReplaced = mutableList.replaceBy(item, predicate)
if (!isReplaced) {
mutableList.add(item)
}
return mutableList
}