Skip to content

Commit

Permalink
Added .empty() to all Flows
Browse files Browse the repository at this point in the history
  • Loading branch information
fluidsonic committed May 29, 2020
1 parent 76594dd commit 6580554
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 0 deletions.
57 changes: 57 additions & 0 deletions sources/flows/AggregateFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package io.fluidsonic.mongo

import com.mongodb.client.model.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import org.bson.conversions.*
import java.util.concurrent.*
Expand Down Expand Up @@ -135,4 +137,59 @@ interface AggregateFlow<out TResult : Any> : Flow<TResult> {
* @since 1.8
*/
suspend fun firstOrNull(): TResult?


companion object {

fun <TResult : Any> empty(): AggregateFlow<TResult> =
Empty
}


private object Empty : AggregateFlow<Nothing> {

override fun allowDiskUse(allowDiskUse: Boolean?) =
this


override fun maxTime(maxTime: Long, timeUnit: TimeUnit) =
this


override fun maxAwaitTime(maxAwaitTime: Long, timeUnit: TimeUnit) =
this


override fun bypassDocumentValidation(bypassDocumentValidation: Boolean?) =
this


override suspend fun toCollection() =
Unit


override fun collation(collation: Collation?) =
this


override fun comment(comment: String?) =
this


override fun hint(hint: Bson?) =
this


override fun batchSize(batchSize: Int) =
this


override suspend fun firstOrNull() =
null


@InternalCoroutinesApi
override suspend fun collect(collector: FlowCollector<Nothing>) =
Unit
}
}
53 changes: 53 additions & 0 deletions sources/flows/ChangeStreamFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package io.fluidsonic.mongo

import com.mongodb.client.model.*
import com.mongodb.client.model.changestream.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import org.bson.*
import java.util.concurrent.*
Expand Down Expand Up @@ -132,4 +134,55 @@ interface ChangeStreamFlow<out TResult : Any> : Flow<ChangeStreamDocument<out TR
* @since 1.8
*/
suspend fun firstOrNull(): ChangeStreamDocument<out TResult>?


companion object {

fun <TResult : Any> empty(): ChangeStreamFlow<TResult> =
Empty
}


private object Empty : ChangeStreamFlow<Nothing> {

override fun fullDocument(fullDocument: FullDocument) =
this


override fun resumeAfter(resumeToken: BsonDocument) =
this


override fun startAtOperationTime(startAtOperationTime: BsonTimestamp) =
this


override fun startAfter(startAfter: BsonDocument) =
this


override fun maxAwaitTime(maxAwaitTime: Long, timeUnit: TimeUnit) =
this


override fun collation(collation: Collation?) =
this


override fun <TDocument : Any> withDocumentClass(clazz: KClass<out TDocument>) =
emptyFlow<TDocument>()


override fun batchSize(batchSize: Int) =
this


override suspend fun firstOrNull(): Nothing? =
null


@InternalCoroutinesApi
override suspend fun collect(collector: FlowCollector<ChangeStreamDocument<out Nothing>>) =
Unit
}
}
37 changes: 37 additions & 0 deletions sources/flows/DistinctFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package io.fluidsonic.mongo

import com.mongodb.client.model.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import org.bson.conversions.*
import java.util.concurrent.*
Expand Down Expand Up @@ -78,4 +80,39 @@ interface DistinctFlow<out TResult : Any> : Flow<TResult> {
* @since 1.8
*/
suspend fun firstOrNull(): TResult?


companion object {

fun <TResult : Any> empty(): DistinctFlow<TResult> =
Empty
}


private object Empty : DistinctFlow<Nothing> {

override fun filter(filter: Bson?) =
this


override fun maxTime(maxTime: Long, timeUnit: TimeUnit) =
this


override fun collation(collation: Collation?) =
this


override fun batchSize(batchSize: Int) =
this


override suspend fun firstOrNull() =
null


@InternalCoroutinesApi
override suspend fun collect(collector: FlowCollector<Nothing>) =
Unit
}
}
97 changes: 97 additions & 0 deletions sources/flows/FindFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package io.fluidsonic.mongo

import com.mongodb.*
import com.mongodb.client.model.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import org.bson.conversions.*
import java.util.concurrent.*
Expand Down Expand Up @@ -224,4 +226,99 @@ interface FindFlow<out TResult : Any> : Flow<TResult> {
* @since 1.8
*/
suspend fun firstOrNull(): TResult?


companion object {

fun <TResult : Any> empty(): FindFlow<TResult> =
Empty
}


private object Empty : FindFlow<Nothing> {

override fun filter(filter: Bson?) =
this


override fun limit(limit: Int) =
this


override fun skip(skip: Int) =
this


override fun maxTime(maxTime: Long, timeUnit: TimeUnit) =
this


override fun maxAwaitTime(maxAwaitTime: Long, timeUnit: TimeUnit) =
this


override fun projection(projection: Bson?) =
this


override fun sort(sort: Bson?) =
this


override fun noCursorTimeout(noCursorTimeout: Boolean) =
this


override fun oplogReplay(oplogReplay: Boolean) =
this


override fun partial(partial: Boolean) =
this


override fun cursorType(cursorType: CursorType) =
this


override fun collation(collation: Collation?) =
this


override fun comment(comment: String?) =
this


override fun hint(hint: Bson?) =
this


override fun max(max: Bson?) =
this


override fun min(min: Bson?) =
this


override fun returnKey(returnKey: Boolean) =
this


override fun showRecordId(showRecordId: Boolean) =
this


override fun batchSize(batchSize: Int) =
this


override suspend fun firstOrNull() =
null


@InternalCoroutinesApi
override suspend fun collect(collector: FlowCollector<Nothing>) =
Unit
}
}
33 changes: 33 additions & 0 deletions sources/flows/ListCollectionsFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.fluidsonic.mongo

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import org.bson.conversions.*
import java.util.concurrent.*
Expand Down Expand Up @@ -67,4 +69,35 @@ interface ListCollectionsFlow<out TResult : Any> : Flow<TResult> {
* @since 1.8
*/
suspend fun firstOrNull(): TResult?


companion object {

fun <TResult : Any> empty(): ListCollectionsFlow<TResult> =
Empty
}


private object Empty : ListCollectionsFlow<Nothing> {

override fun filter(filter: Bson?) =
this


override fun maxTime(maxTime: Long, timeUnit: TimeUnit) =
this


override fun batchSize(batchSize: Int) =
this


override suspend fun firstOrNull() =
null


@InternalCoroutinesApi
override suspend fun collect(collector: FlowCollector<Nothing>) =
Unit
}
}
37 changes: 37 additions & 0 deletions sources/flows/ListDatabasesFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.fluidsonic.mongo

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import org.bson.conversions.*
import java.util.concurrent.*
Expand Down Expand Up @@ -78,4 +80,39 @@ interface ListDatabasesFlow<out TResult : Any> : Flow<TResult> {
* @since 1.8
*/
suspend fun firstOrNull(): TResult?


companion object {

fun <TResult : Any> empty(): ListDatabasesFlow<TResult> =
Empty
}


private object Empty : ListDatabasesFlow<Nothing> {

override fun maxTime(maxTime: Long, timeUnit: TimeUnit) =
this


override fun filter(filter: Bson?) =
this


override fun nameOnly(nameOnly: Boolean?) =
this


override fun batchSize(batchSize: Int) =
this


override suspend fun firstOrNull() =
null


@InternalCoroutinesApi
override suspend fun collect(collector: FlowCollector<Nothing>) =
Unit
}
}
Loading

0 comments on commit 6580554

Please sign in to comment.