add in memory map based crud implementation

This commit is contained in:
2020-10-14 14:22:58 +06:00
parent 88f442a16d
commit 0ba93d0e60
4 changed files with 142 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
package dev.inmo.micro_utils.pagination.utils
import dev.inmo.micro_utils.pagination.*
fun <T> Iterable<T>.paginate(with: Pagination): PaginationResult<T> {
var i = 0
val result = mutableListOf<T>()
val lowerIndex = with.firstIndex
val greatestIndex = with.lastIndex
for (item in this) {
when {
i < lowerIndex || i > greatestIndex -> i++
else -> {
result.add(item)
i++
}
}
}
return result.createPaginationResult(with, i.toLong())
}
fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
return subList(with.firstIndex, with.lastIndex + 1).createPaginationResult(
with,
size.toLong()
)
}
fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> {
return this.drop(with.firstIndex).take(with.size).createPaginationResult(
with,
size.toLong()
)
}