Getting Started - ghasemdev/kpy-core GitHub Wiki
If you're using of Gradle Groovy, add the following to your build.gradle
file.
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation "com.github.jakode2020:kpy-core:$kpy_version"
}
If you're using of Gradle Kotlin, you can add the following to your build.gradle.kts.
dependencies {
implementation("com.github.jakode2020:kpy-core:$kpy_version")
}
If you're using of Maven, add the following to your build.gradle
file.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then add the dependency
<dependency>
<groupId>com.github.jakode2020</groupId>
<artifactId>kpy-core</artifactId>
<version>kpy_version</version>
</dependency>
Kpy include some structure for validation and design pattern. more details on Structure
Password validation is one of the most widely used structures in programming. we can implement this structure easily with builder and observable pattern
val password = password {
isValidLength { println("isValidLength :$it") }
includeDigit { println("includeDigit :$it") }
includeLowerCase { println("includeLowerCase :$it") }
includeUpperCase { println("includeUpperCase :$it") }
includeSpecialChar { println("includeSpecialChar :$it") }
excludeWhitespace { println("excludeWhitespace :$it") }
}
password.value = "Shgktl125&**#jhSS"
println()
password.value = " "
Output :
isValidLength :true
includeDigit :true
includeLowerCase :true
includeUpperCase :true
includeSpecialChar :true
excludeWhitespace :true
isValidLength :false
includeDigit :false
includeLowerCase :false
includeUpperCase :false
includeSpecialChar :false
excludeWhitespace :false
The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand.
abstract class ObjectPool<T> {
protected val objects = hashMapOf<String, T>()
val poolSize: Int get() = objects.size
abstract fun getObject(key: String? = null): T
abstract fun putObject(key: String, `object`: T)
}
object DefaultPool : ObjectPool<Any>() {
@Throws(IllegalArgumentException::class)
override fun getObject(key: String?): Any {
require(poolSize > 0) { "Pool is empty" }
return key.ifNotNull { objects[key] }.orDefault(objects.values.last())
}
override fun putObject(key: String, `object`: Any) {
objects[key] = `object`
}
}
fun main() {
println(DefaultPool.poolSize) // output : 0
DefaultPool.putObject("one", 1)
println(DefaultPool.poolSize) // output : 1
println(DefaultPool.getObject()) // output : 1
DefaultPool.putObject("two", 2)
println(DefaultPool.poolSize) // output : 2
println(DefaultPool.getObject("two")) // output : 2
}
Extension functions are one of the attractive features of Kotlin and other languages such as Swift, Dart, C# and so on. This feature works by adding a function outside of its class. The Kpy library will provide you with a large number of these functions.
val myList = listOf(1..10)
print(myList, myList.lastIndex, separator = ", ", end = "\n") // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 9
val choice = Random.choice(myList)
println(choice) // 4
val choices = Random.choices(list = listOf(1..5), weights = listOf(1, 1, 10, 1, 1), length = 7)
println(choices) // [3, 5, 1, 3, 3, 3, 3]
println("#FFFFFF".hexToRGB) // (255, 255, 255)
println(16.99.formatPrice()) // $16.99
val stringDate = Date().toString(format = "HH:MM:ss")
println(stringDate) // 18:03:19
print(16.00.removeDecimalPart, 16.25.removeDecimalPart) // 16 16.25
val formattedNumber = 123456789.123.separate(pattern = "#,###.##")
println(formattedNumber) // 123,456,789.12
val nullableText: String? = null
val text = nullableText.ifNotNull { "not null" }.orDefault("null")
println(text) // null
println(26.24985.round(digits = 3)) // 26.25
println("^" * 5) // ^^^^^
println("Hello2021".isAlphanumeric) // true
println("Hello".isDigit) // false
val twoDayAfter = (now() + 2.day + 5.hour + 54.minute).asDate.toString("dd/MM/yyyy-HH:mm:ss")
println(twoDayAfter) // 29/08/2021-00:38:32
val timeAgo = (now() - 8.hour).timeAgo()
println(timeAgo) // 8 hours ago
More on Extension Function
To log in to the console, we can use the Log class and the result can be seen in the image
Log.i("This is a test")
Log.d("This is a test")
Log.w("This is a test")
Log.e(Exception("This is a test"))
Log.i("This is a test", tag = "someTag")
Log.d("This is a test", tag = "someTag")
Log.w("This is a test", tag = "someTag")
Log.e(Exception("This is a test"), tag = "someTag")
More on Logging
In Persian language, when using the Itext7 library to support right alignment, you must have a license.
But there is a way around it and that is to use Unicoed.
println(UnicodeConverter.toPersianUnicode("سال 1400 مبارک")) // ﮎﺭﺎﺒﻣ ۱۴۰۰ ﻝﺎﺳ
More on Itext7
Or... back to Introduction