org.apache.tinkerpop.gremlin.ogm.extensions.MutableMaps.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-gremlin-ogm Show documentation
Show all versions of kotlin-gremlin-ogm Show documentation
The Object Graph Mapping Library for Kotlin and Gremlin
package org.apache.tinkerpop.gremlin.ogm.extensions
internal fun MutableMap.mapValuesInPlace(transform: (Map.Entry) -> V) =
entries.forEach { entry ->
entry.setValue(transform(entry))
}
internal fun Sequence>.toMultiMap(requireKeys: Iterable = emptyList()): Map> {
val remainingRequiredKeys = requireKeys.toMutableSet()
val map = mutableMapOf>()
forEach {
remainingRequiredKeys.remove(it.first)
map[it.first]?.add(it.second) ?: {
map[it.first] = mutableListOf(it.second)
}()
}
remainingRequiredKeys.forEach {
map[it] = mutableListOf()
}
return map
}
internal fun Sequence>.toOptionalMap(requireKeys: Iterable = emptyList()): Map {
val remainingRequiredKeys = requireKeys.toMutableSet()
val map = associateTo(mutableMapOf()) {
remainingRequiredKeys.remove(it.first)
it
}
remainingRequiredKeys.forEach {
map[it] = null
}
return map
}
internal fun Sequence>.toSingleMap(requireKeys: Iterable = emptyList()): Map {
val remainingRequiredKeys = requireKeys.toMutableSet()
val map = associateTo(mutableMapOf()) {
remainingRequiredKeys.remove(it.first)
it
}
if (remainingRequiredKeys.isNotEmpty()) {
throw NoSuchElementException("Sequence is missing pair for keys $remainingRequiredKeys")
}
return map
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy