All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.tinkerpop.gremlin.ogm.extensions.MutableMaps.kt Maven / Gradle / Ivy

There is a newer version: 0.21.0
Show newest version
package org.apache.tinkerpop.gremlin.ogm.extensions

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal.Symbols.map

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) ?: kotlin.run {
            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