forked from Fascinated/Bat
add spotify feature
This commit is contained in:
@ -20,49 +20,49 @@ public final class TimeUtils {
|
||||
* @return the formatted time
|
||||
*/
|
||||
public static String format(long millis) {
|
||||
return format(millis, WildTimeUnit.FIT);
|
||||
return format(millis, BatTimeFormat.FIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a time in millis to a readable time format.
|
||||
*
|
||||
* @param millis the millis to format
|
||||
* @param millis the millis to format
|
||||
* @param timeUnit the time unit to format the millis to
|
||||
* @return the formatted time
|
||||
*/
|
||||
public static String format(long millis, WildTimeUnit timeUnit) {
|
||||
public static String format(long millis, BatTimeFormat timeUnit) {
|
||||
return format(millis, timeUnit, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a time in millis to a readable time format.
|
||||
*
|
||||
* @param millis the millis to format
|
||||
* @param millis the millis to format
|
||||
* @param timeUnit the time unit to format the millis to
|
||||
* @param compact whether to use a compact display
|
||||
* @param compact whether to use a compact display
|
||||
* @return the formatted time
|
||||
*/
|
||||
public static String format(long millis, WildTimeUnit timeUnit, boolean compact) {
|
||||
public static String format(long millis, BatTimeFormat timeUnit, boolean compact) {
|
||||
return format(millis, timeUnit, true, compact);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a time in millis to a readable time format.
|
||||
*
|
||||
* @param millis the millis to format
|
||||
* @param millis the millis to format
|
||||
* @param timeUnit the time unit to format the millis to
|
||||
* @param decimals whether to include decimals
|
||||
* @param compact whether to use a compact display
|
||||
* @param compact whether to use a compact display
|
||||
* @return the formatted time
|
||||
*/
|
||||
public static String format(long millis, WildTimeUnit timeUnit, boolean decimals, boolean compact) {
|
||||
public static String format(long millis, BatTimeFormat timeUnit, boolean decimals, boolean compact) {
|
||||
if (millis == -1L) { // Format permanent
|
||||
return "Perm" + (compact ? "" : "anent");
|
||||
}
|
||||
// Format the time to the best fitting time unit
|
||||
if (timeUnit == WildTimeUnit.FIT) {
|
||||
for (WildTimeUnit otherTimeUnit : WildTimeUnit.VALUES) {
|
||||
if (otherTimeUnit != WildTimeUnit.FIT && millis >= otherTimeUnit.getMillis()) {
|
||||
if (timeUnit == BatTimeFormat.FIT) {
|
||||
for (BatTimeFormat otherTimeUnit : BatTimeFormat.VALUES) {
|
||||
if (otherTimeUnit != BatTimeFormat.FIT && millis >= otherTimeUnit.getMillis()) {
|
||||
timeUnit = otherTimeUnit;
|
||||
break;
|
||||
}
|
||||
@ -74,7 +74,7 @@ public final class TimeUtils {
|
||||
}
|
||||
String formatted = time + (compact ? timeUnit.getSuffix() : " " + timeUnit.getDisplay()); // Append the time unit
|
||||
if (time != 1.0 && !compact) { // Pluralize the time unit
|
||||
formatted+= "s";
|
||||
formatted += "s";
|
||||
}
|
||||
return formatted;
|
||||
}
|
||||
@ -89,16 +89,16 @@ public final class TimeUtils {
|
||||
* @return the time in millis
|
||||
*/
|
||||
public static long fromString(String input) {
|
||||
Matcher matcher = WildTimeUnit.SUFFIX_PATTERN.matcher(input); // Match the given input
|
||||
Matcher matcher = BatTimeFormat.SUFFIX_PATTERN.matcher(input); // Match the given input
|
||||
long millis = 0; // The total millis
|
||||
|
||||
// Match corresponding suffixes and add up the total millis
|
||||
while (matcher.find()) {
|
||||
int amount = Integer.parseInt(matcher.group(1)); // The amount of time to add
|
||||
String suffix = matcher.group(2); // The unit suffix
|
||||
WildTimeUnit timeUnit = WildTimeUnit.fromSuffix(suffix); // The time unit to add
|
||||
BatTimeFormat timeUnit = BatTimeFormat.fromSuffix(suffix); // The time unit to add
|
||||
if (timeUnit != null) { // Increment the total millis
|
||||
millis+= amount * timeUnit.getMillis();
|
||||
millis += amount * timeUnit.getMillis();
|
||||
}
|
||||
}
|
||||
return millis;
|
||||
@ -107,9 +107,11 @@ public final class TimeUtils {
|
||||
/**
|
||||
* Represents a unit of time.
|
||||
*/
|
||||
@NoArgsConstructor @AllArgsConstructor
|
||||
@Getter(AccessLevel.PRIVATE) @ToString
|
||||
public enum WildTimeUnit {
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
@ToString
|
||||
public enum BatTimeFormat {
|
||||
FIT,
|
||||
YEARS("Year", "y", TimeUnit.DAYS.toMillis(365L)),
|
||||
MONTHS("Month", "mo", TimeUnit.DAYS.toMillis(30L)),
|
||||
@ -123,7 +125,7 @@ public final class TimeUtils {
|
||||
/**
|
||||
* Our cached unit values.
|
||||
*/
|
||||
public static final WildTimeUnit[] VALUES = values();
|
||||
public static final BatTimeFormat[] VALUES = values();
|
||||
|
||||
/**
|
||||
* Our cached suffix pattern.
|
||||
@ -152,8 +154,8 @@ public final class TimeUtils {
|
||||
* @return the time unit, null if not found
|
||||
*/
|
||||
@Nullable
|
||||
public static WildTimeUnit fromSuffix(String suffix) {
|
||||
for (WildTimeUnit unit : VALUES) {
|
||||
public static BatTimeFormat fromSuffix(String suffix) {
|
||||
for (BatTimeFormat unit : VALUES) {
|
||||
if (unit != FIT && unit.getSuffix().equals(suffix)) {
|
||||
return unit;
|
||||
}
|
||||
|
Reference in New Issue
Block a user