misc : introduce List.firstIfSingle extension

This commit is contained in:
ganfra
2024-12-09 11:18:04 +01:00
parent 98c0697a3f
commit 421bbba558
2 changed files with 18 additions and 11 deletions

View File

@@ -0,0 +1,15 @@
/*
* Copyright 2024 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only
* Please see LICENSE in the repository root for full details.
*/
package io.element.android.libraries.core.extensions
/**
* Returns the first element if the list contains exactly one element, otherwise returns null.
*/
inline fun <reified T> List<T>.firstIfSingle(): T? {
return if (size == 1) first() else null
}