initial commit
This commit is contained in:
98
core/build.gradle.kts
Normal file
98
core/build.gradle.kts
Normal file
@@ -0,0 +1,98 @@
|
||||
import org.gradle.kotlin.dsl.test
|
||||
import java.util.Properties
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.dokka)
|
||||
alias(libs.plugins.kotlin.jvm)
|
||||
alias(libs.plugins.maven.publish)}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
testImplementation(kotlin("test"))
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
kotlin {
|
||||
jvmToolchain(11)
|
||||
}
|
||||
|
||||
val javadocJar by tasks.register<Jar>("dokkaJavadocJar") {
|
||||
dependsOn(tasks.dokkaJavadoc)
|
||||
from(tasks.dokkaJavadoc.flatMap { it.outputDirectory })
|
||||
archiveClassifier.set("javadoc")
|
||||
}
|
||||
|
||||
val sourcesJar by tasks.register<Jar>("sourcesJar") {
|
||||
from(sourceSets.main.get().allSource)
|
||||
archiveClassifier.set("sources")
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("maven") {
|
||||
groupId = rootProject.group.toString()
|
||||
artifactId = "paging"
|
||||
version = rootProject.version.toString()
|
||||
from(components["java"])
|
||||
artifact(javadocJar)
|
||||
artifact(sourcesJar)
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
val secretFile = rootProject.file("secret.properties")
|
||||
if (secretFile.exists()) {
|
||||
val secret = Properties()
|
||||
secretFile.inputStream().also { secret.load(it) }
|
||||
|
||||
val mavenUrl = secret["neuon.maven.url"] as? String
|
||||
val mavenUsername = secret["neuon.maven.username"] as? String
|
||||
val mavenPassword = secret["neuon.maven.password"] as? String
|
||||
|
||||
if (mavenUrl != null && mavenUsername != null && mavenPassword != null) {
|
||||
maven {
|
||||
requireNotNull(mavenUrl) {
|
||||
"neuon.maven.url is missing from secret.properties"
|
||||
}
|
||||
requireNotNull(mavenUsername) {
|
||||
"neuon.maven.username is missing from secret.properties"
|
||||
}
|
||||
requireNotNull(mavenPassword) {
|
||||
"neuon.maven.password is missing from secret.properties"
|
||||
}
|
||||
name = "neuon"
|
||||
url = uri(mavenUrl)
|
||||
|
||||
credentials(PasswordCredentials::class) {
|
||||
username = mavenUsername
|
||||
password = mavenPassword
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val list = listOfNotNull(
|
||||
"neuon.maven.url".takeIf { mavenUrl == null },
|
||||
"neuon.maven.username".takeIf { mavenUsername == null },
|
||||
"neuon.maven.password".takeIf { mavenPassword == null },
|
||||
)
|
||||
val message = buildString {
|
||||
append("Missing Maven publication in secret.properties: ")
|
||||
append(list.joinToString())
|
||||
append(". Ignore this if you don't plan on publishing this library")
|
||||
}
|
||||
|
||||
logger.error(message)
|
||||
}
|
||||
} else {
|
||||
logger.error(
|
||||
buildString {
|
||||
append("Missing secret.properties. Ignore this if you don't plan on publishing this library")
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
11
core/src/main/kotlin/ai/neuon/utility/paging/PagingConfig.kt
Normal file
11
core/src/main/kotlin/ai/neuon/utility/paging/PagingConfig.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package ai.neuon.utility.paging
|
||||
|
||||
data class PagingConfig(
|
||||
val index: Int,
|
||||
val size: Int,
|
||||
) {
|
||||
init {
|
||||
check(index >= 0)
|
||||
check(size > 0)
|
||||
}
|
||||
}
|
||||
44
core/src/main/kotlin/ai/neuon/utility/paging/PagingData.kt
Normal file
44
core/src/main/kotlin/ai/neuon/utility/paging/PagingData.kt
Normal file
@@ -0,0 +1,44 @@
|
||||
package ai.neuon.utility.paging
|
||||
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.putJsonObject
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.max
|
||||
import kotlin.math.roundToLong
|
||||
|
||||
data class PagingData<T>(
|
||||
val i: Int, val pageSize: Int, val totalItemsCount: Long? = null,
|
||||
val list: List<T>,
|
||||
) {
|
||||
companion object {
|
||||
fun <T> empty(index: Int = 0, size: Int = 10): PagingData<T> {
|
||||
return PagingData(
|
||||
i = index,
|
||||
pageSize = size,
|
||||
list = emptyList(),
|
||||
)
|
||||
}
|
||||
|
||||
fun <T> empty(config: PagingConfig): PagingData<T> {
|
||||
return empty(index = config.index, size = config.size)
|
||||
}
|
||||
}
|
||||
|
||||
fun buildPagingJsonObject() = buildJsonObject {
|
||||
putJsonObject("config") {
|
||||
put("index", JsonPrimitive(i))
|
||||
put("size", JsonPrimitive(pageSize))
|
||||
}
|
||||
putJsonObject("page") {
|
||||
totalItemsCount?.also { itemCount ->
|
||||
max(
|
||||
1, ceil(
|
||||
itemCount.toFloat()
|
||||
.div(pageSize)
|
||||
).roundToLong()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user