SpringPage.kt 1.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package org.chronopolis.rest.models.page

/**
 * A wrapper around a Page from spring to avoid pulling in spring-data dependencies
 *
 * Since we don't make use of most of the Pageable/Slice interfaces, we can ignore them
 *
 * @author shake
 */
data class SpringPage<out T>(val content: List<T>,
11
                             val pageable: Pageable,
12 13 14
                             val last: Boolean,
                             val totalPages: Int,
                             val totalElements: Long,
15 16
                             val first: Boolean,
                             val sort: Sort,
17 18
                             val numberOfElements: Int,
                             val size: Int,
19 20
                             val number: Int,
                             val empty: Boolean) : Iterable<T> {
21 22 23 24 25 26
    override fun iterator(): Iterator<T> {
        return content.iterator()
    }
}

fun <T> List<T>.wrap(): SpringPage<T> {
27 28 29
    val sort = Sort(true, false, false)
    val page = Pageable(sort, this.size, 1, 0, false, true)
    return SpringPage(this, page, true, 1, 1L, true, sort, this.size, this.size, 1, this.isEmpty())
30 31
}

32 33 34 35 36 37 38 39 40 41
data class Pageable(val sort: Sort,
                    val pageSize: Int,
                    val pageNumber: Int,
                    val offset: Int,
                    val unpaged: Boolean,
                    val paged: Boolean)

data class Sort(val sorted: Boolean,
                val unsorted: Boolean,
                val empty: Boolean)