-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Boolean.kt and created util folder.
- Loading branch information
Showing
9 changed files
with
125 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# ShoLib | ||
The shog.dev library. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/dev/shog/lib/Arrays.kt → src/main/java/dev/shog/lib/util/Arrays.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
2 changes: 1 addition & 1 deletion
2
src/main/java/dev/shog/lib/Files.kt → src/main/java/dev/shog/lib/util/Files.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.* | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
src/main/java/dev/shog/lib/Maps.kt → src/main/java/dev/shog/lib/util/Maps.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|