Skip to content

Commit

Permalink
Added Boolean.kt and created util folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkneisl committed Jan 12, 2020
1 parent dbc29ed commit f2d6bc9
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 36 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ShoLib
The shog.dev library.
30 changes: 0 additions & 30 deletions src/main/java/dev/shog/lib/Date.kt

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/dev/shog/lib/cache/Cache.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.shog.lib.cache

import dev.shog.lib.FileHandler
import dev.shog.lib.readObject
import dev.shog.lib.writeObject
import dev.shog.lib.util.readObject
import dev.shog.lib.util.writeObject
import java.io.File

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/shog/lib/token/TokenManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.shog.lib.token

import dev.shog.lib.asDate
import dev.shog.lib.util.asDate
import dev.shog.lib.cache.Cache
import kong.unirest.Unirest
import reactor.core.publisher.Mono
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.shog.lib
package dev.shog.lib.util

/**
* If a [Collection] contains any null values.
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/dev/shog/lib/util/Boolean.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.shog.lib.util

/**
* Like a ternary operator in Java.
* If the [Boolean] is true, return [valueOne]. However, if it's false, return [valueTwo].
*/
fun <T> Boolean.eitherOr(valueOne: T, valueTwo: T): T =
if (this) valueOne else valueTwo

/**
* Turn `true` into `enable` and `false` into `disable`.
*/
fun Boolean.toEnableDisable(): String =
eitherOr("enable", "disable")

/**
* Add a D onto [toEnableDisable]
*/
fun Boolean.toEnabledDisabled(): String =
"${toEnableDisable()}d"

/**
* Turn `true` into `yes` and `false` into `no`.
*/
fun Boolean.toYesNo(): String =
eitherOr("yes", "no")


89 changes: 89 additions & 0 deletions src/main/java/dev/shog/lib/util/Date.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package dev.shog.lib.util

import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.*

private val FORMATTER = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.LONG)
.withLocale(Locale.getDefault())
.withZone(ZoneId.systemDefault())

/**
* Get a [Long] as a [Date].
*/
fun Long.asDate(): Date =
Date.from(asInstant())

/**
* Get a [Long] as an [Instant].
*/
fun Long.asInstant(): Instant =
Instant.ofEpochMilli(this)

/**
* Format a [Long] using [FORMATTER].
*/
fun Long.defaultFormat(): String =
asInstant().defaultFormat()

/**
* Get the age of a [Long]
*/
fun Long.getAge(): Long =
System.currentTimeMillis() - this

/**
* Turn a [Long] into a fancy date.
*/
fun Long.fancyDate(): String {
var response = ""

val seconds = this / 1000

if (seconds <= 60) {
// Assuming there's multiple seconds
return "$seconds seconds"
}

val minutes = seconds / 60

if (minutes < 60)
return if (minutes > 1) "$minutes minutes ${seconds - minutes * 60} seconds" else "$minutes minute ${seconds - minutes * 60} seconds"

val hours = minutes / 60
val hoursMinutes = minutes - hours * 60

if (hours < 24) {
response += if (hours > 1) "$hours hours " else "$hours hour "
response += if (hoursMinutes > 1) "$hoursMinutes minutes" else "$hoursMinutes minute"

return response
}

val days = hours / 24
val hoursDays = hours - days * 24

if (days < 7) {
response += if (days > 1) "$days days " else "$days day "
response += if (hoursDays > 1) "$hoursDays hours" else "$hoursDays hour"

return response
}

val weeks = days / 7
val weekDays = days - weeks * 7

response += if (weeks > 1) "$weeks weeks " else "$weeks week "
response += if (weekDays > 1) "$weekDays days" else "$weekDays day"

return response
}

/**
* Format a [Instant] to date.
*/
fun Instant.defaultFormat(): String =
FORMATTER.format(this)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.shog.lib
package dev.shog.lib.util

import java.io.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.shog.lib
package dev.shog.lib.util

import java.lang.Exception

Expand Down

0 comments on commit f2d6bc9

Please sign in to comment.