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

org.apache.tinkerpop.gremlin.ogm.reflection.CachedGraphDescription.kt Maven / Gradle / Ivy

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

import org.apache.tinkerpop.gremlin.ogm.elements.Edge
import org.apache.tinkerpop.gremlin.ogm.elements.Vertex
import org.apache.tinkerpop.gremlin.ogm.exceptions.ObjectDescriptionMissing
import org.apache.tinkerpop.gremlin.ogm.exceptions.PropertyMapperMissing
import org.apache.tinkerpop.gremlin.ogm.exceptions.UnregisteredClass
import org.apache.tinkerpop.gremlin.ogm.exceptions.UnregisteredLabel
import org.apache.tinkerpop.gremlin.ogm.extensions.filterNullValues
import org.apache.tinkerpop.gremlin.ogm.mappers.PropertyBiMapper
import org.apache.tinkerpop.gremlin.ogm.mappers.SerializedProperty
import org.apache.tinkerpop.gremlin.ogm.mappers.scalar.InstantPropertyMapper
import org.apache.tinkerpop.gremlin.ogm.mappers.scalar.UUIDPropertyMapper
import org.apache.tinkerpop.gremlin.ogm.mappers.scalar.identity.*
import org.apache.tinkerpop.gremlin.ogm.paths.relationships.Relationship
import java.time.Instant
import java.util.*
import kotlin.reflect.KClass

open class CachedGraphDescription (
        vertices: Set>,
        relationships: Map, KClass>?> = mapOf(),
        objectProperties: Set> = setOf(),
        scalarProperties: Map, PropertyBiMapper<*, *>> = emptyMap()
) : GraphDescription {

    private val scalarMappers = builtinScalarPropertyMappers + scalarProperties

    private val vertexDescriptionsByClass = vertices
            .associate { vertexClass ->
                vertexClass to VertexDescription(vertexClass)
            }

    private val vertexDescriptionsByLabel = vertexDescriptionsByClass
            .mapKeys {
                it.value.label
            }

    private val edgeDescriptionsByClass = relationships
            .filterNullValues()
            .entries
            .associate { (relationship, edgeClass) ->
                edgeClass to EdgeDescription(relationship, edgeClass)
            }

    private val edgeDescriptionsByLabel = edgeDescriptionsByClass
            .mapKeys {
                it.value.label
            }

    private val objectPropertyDescriptionsByClass = objectProperties
            .associate { objectPropertyClass ->
                objectPropertyClass to ObjectPropertyDescription(objectPropertyClass)
            }

    private val relationshipsByLabel = relationships
            .keys
            .associateBy { relationship ->
                relationship.name
            }

    override val vertexClasses get() = vertexDescriptionsByClass.keys

    override val vertexLabels get() = vertexDescriptionsByLabel.keys

    override val edgeClasses get() = edgeDescriptionsByClass.keys

    override val edgeLabels get() = relationshipsByLabel.keys

    override val objectPropertyClasses get() = objectPropertyDescriptionsByClass.keys

    override val scalarPropertyClasses get() = scalarMappers.keys

    @Suppress("UNCHECKED_CAST")
    override fun  getVertexDescription(vertexClass: KClass): VertexDescription =
            vertexDescriptionsByClass[vertexClass] as? VertexDescription ?: throw UnregisteredClass(vertexClass)

    @Suppress("UNCHECKED_CAST")
    override fun  getVertexDescription(vertexLabel: String): VertexDescription =
            vertexDescriptionsByLabel[vertexLabel] as? VertexDescription ?: throw UnregisteredLabel(vertexLabel)

    @Suppress("UNCHECKED_CAST")
    override fun > getEdgeDescription(edgeClass: KClass): EdgeDescription =
            edgeDescriptionsByClass[edgeClass] as? EdgeDescription ?: throw UnregisteredClass(edgeClass)

    @Suppress("UNCHECKED_CAST")
    override fun  getEdgeRelationship(edgeLabel: String): Relationship =
            relationshipsByLabel[edgeLabel] as? Relationship ?: throw UnregisteredLabel(edgeLabel)

    @Suppress("UNCHECKED_CAST")
    override fun > getEdgeDescription(edgeLabel: String): EdgeDescription? =
            edgeDescriptionsByLabel[edgeLabel] as? EdgeDescription

    @Suppress("UNCHECKED_CAST")
    override fun  getObjectPropertyDescription(objectPropertyClass: KClass): ObjectDescription =
            objectPropertyDescriptionsByClass[objectPropertyClass] as? ObjectDescription ?: throw ObjectDescriptionMissing(objectPropertyClass)

    @Suppress("UNCHECKED_CAST")
    override fun  getScalarPropertyMapper(scalarClass: KClass): PropertyBiMapper =
            scalarMappers[scalarClass] as? PropertyBiMapper ?:
            throw PropertyMapperMissing(scalarClass)

    companion object {

        internal val builtinScalarPropertyMappers = mapOf, PropertyBiMapper<*, *>>(
                String::class to StringPropertyMapper,
                Byte::class to BytePropertyMapper,
                Float::class to FloatPropertyMapper,
                Double::class to DoublePropertyManager,
                Long::class to LongPropertyMapper,
                Int::class to IntegerPropertyMapper,
                Boolean::class to BooleanPropertyMapper,
                Instant::class to InstantPropertyMapper,
                UUID::class to UUIDPropertyMapper)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy