first init and commit project for utils use for every projects
This commit is contained in:
5
src/main/kotlin/com/cubetiqs/common/static/Constants.kt
Normal file
5
src/main/kotlin/com/cubetiqs/common/static/Constants.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.cubetiqs.common.static
|
||||
|
||||
object Constants {
|
||||
const val DELETED_TEXT = " - (Deleted)"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.cubetiqs.common.utils.math
|
||||
|
||||
import com.cubetiqs.common.utils.number.toDecimalPrecision
|
||||
import java.math.RoundingMode
|
||||
|
||||
/**
|
||||
* Math Float Round for Double.
|
||||
* Example: 9.999999 ... n => 10
|
||||
* 9.99 => 9.99
|
||||
*/
|
||||
fun Double?.fround(precision: Int = 5, roundingMode: RoundingMode = RoundingMode.HALF_DOWN): Double {
|
||||
if (this == null) return 0.0
|
||||
return this.toDecimalPrecision(precision, roundingMode).toDouble()
|
||||
}
|
||||
|
||||
fun Number?.getPrecisionValue(): Double {
|
||||
if (this == null) return 0.0
|
||||
return ((this.toDouble()) - (this.toInt()).toDouble())
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.cubetiqs.common.utils.number
|
||||
|
||||
import java.math.RoundingMode
|
||||
import java.text.DecimalFormat
|
||||
|
||||
fun Number.toStringDecimal(pattern: String = "#.##"): String {
|
||||
return DecimalFormat(pattern).format(this)
|
||||
}
|
||||
|
||||
fun Number.toDecimalPrecision(precision: Int? = 2, roundingMode: RoundingMode = RoundingMode.HALF_EVEN): String {
|
||||
var pattern = "##0"
|
||||
if (precision ?: 0 > 0) {
|
||||
pattern += "."
|
||||
}
|
||||
repeat(precision ?: 2) {
|
||||
pattern += "0"
|
||||
}
|
||||
val df = DecimalFormat(pattern)
|
||||
df.roundingMode = roundingMode
|
||||
return df.format(this)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.cubetiqs.common.utils.string
|
||||
|
||||
import com.cubetiqs.common.static.Constants
|
||||
|
||||
fun String?.getOrEmpty(defaultValue: String = ""): String {
|
||||
if (this.isNullOrEmpty()) return defaultValue
|
||||
return this
|
||||
}
|
||||
|
||||
fun String?.ifEmptyNull(): String? {
|
||||
if (this.isNullOrEmpty()) return null
|
||||
return this
|
||||
}
|
||||
|
||||
fun Any?.asString(defaultValue: String = ""): String {
|
||||
if (this == null) return defaultValue
|
||||
if (this is String) return this
|
||||
return this.toString()
|
||||
}
|
||||
|
||||
fun String?.getAndPlus(text: String = ""): String {
|
||||
if (this == null) return text
|
||||
return this.plus(text)
|
||||
}
|
||||
|
||||
fun String?.addDeletedText(): String? {
|
||||
if (this == null) return null
|
||||
return this.plus(Constants.DELETED_TEXT)
|
||||
}
|
||||
|
||||
fun String?.minusDeletedText(): String? {
|
||||
if (this == null) return null
|
||||
return this.split("-")[0].trim()
|
||||
}
|
||||
|
||||
fun String?.querySearch(prefix: String = "%", suffix: String = "%"): String {
|
||||
return "$prefix${this?.toLowerCase()?.trim()}$suffix"
|
||||
}
|
||||
|
||||
private val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
|
||||
|
||||
fun String?.randomString(): String {
|
||||
val strLen = this?.length ?: 2
|
||||
return (1..strLen)
|
||||
.map { kotlin.random.Random.nextInt(0, charPool.size) }
|
||||
.map(charPool::get)
|
||||
.joinToString("")
|
||||
}
|
||||
|
||||
fun String.toMutableSet(splitter: Char): Set<String> = this.trim().split(splitter).toMutableSet()
|
||||
|
||||
fun String.isAllCaps(): Boolean = !this.matches(Regex(".*[a-z].*"))
|
||||
|
||||
/**
|
||||
* Replace space with underscore and to upper case.
|
||||
*/
|
||||
fun String.fromSpaceToUnderScoreAndUpperCase() = this.replace(' ', '_').toUpperCase()
|
||||
Reference in New Issue
Block a user