#1359: CollectionUtils.kt
projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt Kotlin-Klasse, projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt 169 Zeilen · 105 Code · 51 Kommentare · 13 leer
Zweck: Quelldatei: framework/persistence/utils/CollectionUtils.kt. CollectionUtils.kt ist Teil der Open-Source-Projektmanagement-Anwendung ProjectForge.
Quelle (erste 100 Zeilen)
/////////////////////////////////////////////////////////////////////////////
//
// Project ProjectForge Community Edition
// www.projectforge.org
//
// Copyright (C) 2001-2026 Micromata GmbH, Germany (www.micromata.com)
//
// ProjectForge ist dual-lizenziert.
//
// Diese Community-Edition ist freie Software; Sie können sie weiterverbreiten und/oder
// modifizieren unter den Bedingungen der GNU General Public License, wie sie von der
// Free Software Foundation veröffentlicht wurde; Version 3 der Lizenz.
//
// Diese Community-Edition wird in der Hoffnung verteilt, dass sie nützlich sein wird,
// aber OHNE JEGLICHE GEWÄHRLEISTUNG; ohne sogar die stillschweigende Garantie der
// MARKTGÄNGIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK. Siehe die GNU General
// Public License für weitere Details.
//
// Sie sollten eine Kopie der GNU General Public License zusammen mit
// diesem Programm erhalten haben; falls nicht, siehe http://www.gnu.org/licenses/.
//
/////////////////////////////////////////////////////////////////////////////
package org.projectforge.framework.persistence.utils
import org.projectforge.framework.persistence.api.IdObject
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.jvm.jvmErasure
object CollectionUtils {
class CompareCollectionsResult<T>(
/** Neue Einträge in src, noch nicht in dest enthalten. */
val added: Collection<T>? = null,
/** Entfernte Einträge in src, die in dest gelöscht werden sollen. */
val removed: Collection<T>? = null,
/** Einträge in src und dest. */
val kept: Collection<T>? = null,
) {
/**
* Gibt den ersten Eintrag von hinzugefügten, entfernten oder beibehaltenen Einträgen zurück.
* Wird benötigt, um zu prüfen, ob die Einträge historisiert werden sollen oder nicht.
*/
val anyOrNull: T?
get() = added?.firstOrNull() ?: removed?.firstOrNull() ?: kept?.firstOrNull()
}
fun getTypeClassOfEntries(col: Collection<*>?): Class<*> {
return getTypeClassOfEntriesOrNull(col)!!
}
fun getTypeClassOfEntriesOrNull(col: Collection<*>?): Class<*>? {
col ?: return null
return col.firstOrNull()?.javaClass
}
fun isCollection(property: kotlin.reflect.KProperty1<*, *>): Boolean {
return property.returnType.jvmErasure.isSubclassOf(Collection::class)
}
/**
* Verbindet die IDs der angegebenen Sammlung zu einem CSV-String. Die Einträge werden nach ID sortiert.
* Wird z. B. in Verlaufseinträgen verwendet, um die entfernten und hinzugefügten Einträge einer Sammlung darzustellen.
*/
fun joinToStringOfIds(col: Collection<IdObject<Long>>?, filterNotNull: Boolean = true): String? {
col ?: return null
return joinToString(col, filterNotNull, idOnly = true)
}
/**
* Verbindet die angegebene Sammlung zu einem CSV-String. Die Einträge werden sortiert.
*/
fun joinToString(col: Collection<Any?>?, filterNotNull: Boolean = true, idOnly: Boolean = false): String? {
col ?: return null
var filtered = if (filterNotNull) col.filterNotNull() else col
if (filtered.isEmpty()) {
return ""
}
val first = filtered.first()
if (first is Comparable<*>) { // Sortierung ist möglich:
filtered = filtered.sortedWith(compareBy { it as Comparable<*> })
} else if (first is IdObject<*> && first.id is Comparable<*>) { // Sortierung ist möglich:
filtered = filtered.sortedWith(compareBy { ((it as IdObject<*>).id as? Comparable<*>) })
}
return filtered.joinToString(separator = ",") { if (idOnly) toString(it) else it.toString() }
}
private fun toString(entry: Any?): String {
return if (entry == null) {
"null"
} else if (entry is IdObject<*>) {
entry.id.toString()
} else {
entry.toString()
}
}
/**
* Vergleicht zwei Sammlungen und gibt die hinzugefügten, entfernten und beibehaltenen Einträge zurück.
* @param src Quellsammlung.
* @param dest Zielsammmlung.
Git-Verlauf
868d6abb7 2025 -> 2026
48a93dedb Farbige Konsolenausgabe. UserGroupCache-Export zum Debuggen und Vergleichen funktioniert jetzt. CollectionUtil verbessert. KotlinStringExtension.shortenMiddle() hinzugefügt.
63081666f Quelldatei-Header: 2024 -> 2025.
61f05ce90 Migration läuft...
5989b32fd BaseDao: Mechanismus von onChangeLister umgestaltet.
868d6abb7
2025 -> 2026868d6abb75cd191a892911ac8e45058932cf9074
diff --git a/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt b/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
index 7ca7f9101..52deaa7d3 100644
--- a/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
+++ b/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
@@ -3,7 +3,7 @@
// Project ProjectForge Community Edition
// www.projectforge.org
//
-// Copyright (C) 2001-2025 Micromata GmbH, Germany (www.micromata.com)
+// Copyright (C) 2001-2026 Micromata GmbH, Germany (www.micromata.com)
//
// ProjectForge is dual-licensed.
//
48a93dedb
Farbige Konsolenausgabe. UserGroupCache-Export zum Debuggen und Vergleichen funktioniert jetzt. CollectionUtil verbessert. KotlinStringExtension.shortenMiddle() hinzugefügt.48a93dedb542a7f4100f86f6f4c9c1b8b8f41027
diff --git a/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt b/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
index 91ad1f77c..7ca7f9101 100644
--- a/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
+++ b/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
@@ -61,17 +61,37 @@ object CollectionUtils {
* Joins the id's of the given collection to a csv string. The entries are sorted by id.
* Used for example in history entries, representing the removed and added entries of a collection.
*/
- fun joinToStringOfIds(col: Collection<IdObject<Long>>?): String? {
+ fun joinToStringOfIds(col: Collection<IdObject<Long>>?, filterNotNull: Boolean = true): String? {
col ?: return null
- return joinToString(col.map { it.id })
+ return joinToString(col, filterNotNull, idOnly = true)
}
/**
* Joins the given collection to a csv string. The entries are sorted.
*/
- fun <T : Comparable<T>> joinToString(col: Collection<T?>?): String? {
+ fun joinToString(col: Collection<Any?>?, filterNotNull: Boolean = true, idOnly: Boolean = false): String? {
col ?: return null
- return col.filterNotNull().sorted().joinToString(separator = ",")
+ var filtered = if (filterNotNull) col.filterNotNull() else col
+ if (filtered.isEmpty()) {
+ return ""
+ }
+ val first = filtered.first()
+ if (first is Comparable<*>) { // Sorting is possible:
+ filtered = filtered.sortedWith(compareBy { it as Comparable<*> })
+ } else if (first is IdObject<*> && first.id is Comparable<*>) { // Sorting is possible:
+ filtered = filtered.sortedWith(compareBy { ((it as IdObject<*>).id as? Comparable<*>) })
+ }
+ return filtered.joinToString(separator = ",") { if (idOnly) toString(it) else it.toString() }
+ }
+
+ private fun toString(entry: Any?): String {
+ return if (entry == null) {
+ "null"
+ } else if (entry is IdObject<*>) {
+ entry.id.toString()
+ } else {
+ entry.toString()
+ }
}
/** 63081666f
Quelldatei-Header: 2024 -> 2025.63081666f620fb87315f01b817e560e0b2f6a33a
diff --git a/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt b/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
index 795d4b20b..91ad1f77c 100644
--- a/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
+++ b/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
@@ -3,7 +3,7 @@
// Project ProjectForge Community Edition
// www.projectforge.org
//
-// Copyright (C) 2001-2024 Micromata GmbH, Germany (www.micromata.com)
+// Copyright (C) 2001-2025 Micromata GmbH, Germany (www.micromata.com)
//
// ProjectForge is dual-licensed.
//
61f05ce90
Migration läuft...61f05ce90d175668026491c93a758f1ec6524c32
diff --git a/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt b/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
index 9ea824f86..795d4b20b 100644
--- a/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
+++ b/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
@@ -23,7 +23,6 @@
package org.projectforge.framework.persistence.utils
-import kotlinx.collections.immutable.toImmutableList
import org.projectforge.framework.persistence.api.IdObject
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.jvm.jvmErasure
@@ -92,10 +91,10 @@ object CollectionUtils {
if (useDest.isNullOrEmpty()) {
return CompareCollectionsResult()
}
- return CompareCollectionsResult(removed = useDest.toImmutableList())
+ return CompareCollectionsResult(removed = useDest.toList())
}
if (useDest.isNullOrEmpty()) {
- return CompareCollectionsResult(added = useSrc.toImmutableList())
+ return CompareCollectionsResult(added = useSrc.toList())
}
val added = getAddedEntries(src = useSrc, dest = useDest)
val removed = getAddedEntries(src = useDest, dest = useSrc) 5989b32fd
BaseDao: Mechanismus von onChangeLister umgestaltet.5989b32fdbe63a7ade7eabba11a8c2b042ecd66b
diff --git a/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt b/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
index 332777d2b..9ea824f86 100644
--- a/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
+++ b/projectforge-business/src/main/kotlin/org/projectforge/framework/persistence/utils/CollectionUtils.kt
@@ -25,6 +25,8 @@ package org.projectforge.framework.persistence.utils
import kotlinx.collections.immutable.toImmutableList
import org.projectforge.framework.persistence.api.IdObject
+import kotlin.reflect.full.isSubclassOf
+import kotlin.reflect.jvm.jvmErasure
object CollectionUtils {
class CompareCollectionsResult<T>(
@@ -52,6 +54,10 @@ object CollectionUtils {
return col.firstOrNull()?.javaClass
}
+ fun isCollection(property: kotlin.reflect.KProperty1<*, *>): Boolean {
+ return property.returnType.jvmErasure.isSubclassOf(Collection::class)
+ }
+
/**
* Joins the id's of the given collection to a csv string. The entries are sorted by id.
* Used for example in history entries, representing the removed and added entries of a collection.