Skip to content

Commit

Permalink
JetBrains#25 Nullable sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
sedovalx committed May 19, 2017
1 parent 7bcf9b7 commit e8ab689
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
28 changes: 21 additions & 7 deletions src/main/kotlin/kotlinx.dnq/query/XdQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,43 @@ import kotlin.reflect.jvm.javaType

interface XdQuery<out T : XdEntity> {
val entityType: XdEntityType<T>
val entityIterable: Iterable<Entity>
val entityIterable: Iterable<Entity?>
}

class XdQueryImpl<out T : XdEntity>(
override val entityIterable: Iterable<Entity>,
override val entityIterable: Iterable<Entity?>,
override val entityType: XdEntityType<T>) : XdQuery<T>

private val <T : XdEntity> XdQuery<T>.queryEngine: QueryEngine
get() = entityType.entityStore.queryEngine

fun <T : XdEntity> Iterable<Entity>?.asQuery(entityType: XdEntityType<T>): XdQuery<T> {
fun <T : XdEntity> Iterable<Entity?>?.asQuery(entityType: XdEntityType<T>): XdQuery<T> {
return if (this != null) {
XdQueryImpl(this, entityType)
} else {
XdQueryImpl(EntityIterableBase.EMPTY, entityType)
XdQueryImpl(emptyList(), entityType)
}
}

fun <T : XdEntity> XdQuery<T>.asSequence(): Sequence<T> {
return entityIterable.asSequence().map { entityType.wrap(it) }
fun <T : XdEntity, TN : T?> XdQuery<T>.asGenericSequence(): Sequence<TN> {
return entityIterable.asSequence().map {
if (it == null) {
if (null is TN) {
null as TN
} else {
throw Exception()
}
} else {
entityType.wrap(it) as TN
}
}
}

operator fun <T : XdEntity> XdQuery<T>.iterator() = asSequence().iterator()
fun <T : XdEntity> XdQuery<T>.asNullSequence(): Sequence<T?> = this.asGenericSequence<T, T?>()

fun <T : XdEntity> XdQuery<T>.asSequence(): Sequence<T> = this.asGenericSequence()

operator fun <T : XdEntity> XdQuery<T>.iterator(): Iterator<T> = this.asSequence().iterator()

fun <T : XdEntity, C : MutableCollection<in T>> XdQuery<T>.toCollection(destination: C) = asSequence().toCollection(destination)

Expand Down
27 changes: 24 additions & 3 deletions src/test/kotlin/kotlinx.dnq/PropertiesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import jetbrains.exodus.database.exceptions.NullPropertyException
import jetbrains.exodus.database.exceptions.SimplePropertyValidationException
import jetbrains.exodus.database.exceptions.UniqueIndexViolationException
import jetbrains.exodus.entitystore.Entity
import kotlinx.dnq.query.eq
import kotlinx.dnq.query.first
import kotlinx.dnq.query.query
import kotlinx.dnq.query.*
import kotlinx.dnq.simple.regex
import kotlinx.dnq.simple.requireIf
import kotlinx.dnq.util.getOldValue
Expand Down Expand Up @@ -301,4 +299,27 @@ class PropertiesTest : DBTest() {
}
}

@Test
fun `query of nullable property should not throw on materialization`() {
val boss = store.transactional {
User.new {
login = "boss"
skill = 1
}
}
store.transactional {
User.new {
login = "slave"
supervisor = boss
skill = 1
}
}
store.transactional {
User.all().mapDistinct(User::supervisor).asNullSequence().forEach {
println(it?.login ?: "null")
}
}

}

}

0 comments on commit e8ab689

Please sign in to comment.