Skip to content

Commit

Permalink
Refactor duration checks
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Apr 13, 2024
1 parent b1b86b9 commit 827de6c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public MessagePollBuilder setDuration(@Nonnull Duration duration)
{
Checks.notNull(duration, "Duration");
Checks.positive(duration.toHours(), "Duration");
Checks.check(duration.toHours() <= MessagePoll.MAX_DURATION_HOURS, "Duration may not be longer than 168 hours (= 7 days). Provided: %d hours", duration.toHours());
Checks.notLonger(duration, Duration.ofHours(MessagePoll.MAX_DURATION_HOURS), TimeUnit.HOURS, "Duration");

this.duration = duration;
return this;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/utils/Checks.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import org.intellij.lang.annotations.PrintFormat;
import org.jetbrains.annotations.Contract;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -212,6 +214,18 @@ public static void notNegative(final long n, final String name)
throw new IllegalArgumentException(name + " may not be negative");
}

public static void notLonger(final Duration duration, final Duration maxDuration, final TimeUnit resolutionUnit, final String name)
{
notNull(duration, name);
check(
duration.compareTo(maxDuration) <= 0,
"%s may not be longer than %s. Provided: %s",
name,
JDALogger.getLazyString(() -> Helpers.durationToString(maxDuration, resolutionUnit)),
JDALogger.getLazyString(() -> Helpers.durationToString(duration, resolutionUnit))
);
}

// Unique streams checks

public static <T> void checkUnique(Stream<T> stream, String format, BiFunction<Long, T, Object[]> getArgs)
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/utils/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
Expand Down Expand Up @@ -316,4 +317,27 @@ public static boolean hasCause(Throwable throwable, Class<? extends Throwable> c
{
return Collector.of(DataArray::empty, DataArray::add, DataArray::addAll);
}

public static String durationToString(Duration duration, TimeUnit resolutionUnit)
{
long actual = resolutionUnit.convert(duration.getSeconds(), TimeUnit.SECONDS);
String raw = actual + " " + resolutionUnit.toString().toLowerCase(Locale.ROOT);

long days = duration.toDays();
long hours = duration.toHours() % 24;
long minutes = duration.toMinutes() % 60;
long seconds = duration.getSeconds() - TimeUnit.DAYS.toSeconds(days) - TimeUnit.HOURS.toSeconds(hours) - TimeUnit.MINUTES.toSeconds(minutes);

StringJoiner joiner = new StringJoiner(" ");
if (days > 0)
joiner.add(days + " days");
if (hours > 0)
joiner.add(hours + " hours");
if (minutes > 0)
joiner.add(minutes + " minutes");
if (seconds > 0)
joiner.add(seconds + " seconds");

return raw + " (" + joiner + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import org.junit.jupiter.api.function.ThrowingConsumer;

import java.time.Duration;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import static net.dv8tion.jda.internal.utils.Helpers.durationToString;
import static net.dv8tion.jda.test.ChecksHelper.isNegativeError;
import static net.dv8tion.jda.test.ChecksHelper.notPositiveError;

Expand All @@ -42,4 +45,13 @@ public DurationChecksAssertions checksPositive()
throwsFor(Duration.ZERO, notPositiveError(name));
return this;
}

public DurationChecksAssertions checksNotLonger(Duration maxDuration, TimeUnit resolution)
{
Duration input = maxDuration.plusSeconds(resolution.toSeconds(1));
throwsFor(input,
String.format(Locale.ROOT, "%s may not be longer than %s. Provided: %s",
name, durationToString(maxDuration, resolution), durationToString(input, resolution)));
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ void testInvalidInputs()
assertDurationChecks("Duration", builder::setDuration)
.checksNotNull()
.checksPositive()
.throwsFor(Duration.ofHours(500), "Duration may not be longer than 168 hours (= 7 days). Provided: 500 hours");
.checksNotLonger(Duration.ofHours(7 * 24), TimeUnit.HOURS);

ChecksHelper.<TimeUnit>assertChecks("TimeUnit", (unit) -> builder.setDuration(1, unit))
.checksNotNull();

assertLongChecks("Duration", (duration) -> builder.setDuration(duration, TimeUnit.SECONDS))
.checksPositive()
.throwsFor(TimeUnit.DAYS.toSeconds(8), "Duration may not be longer than 168 hours (= 7 days). Provided: 192 hours");
.throwsFor(TimeUnit.DAYS.toSeconds(8), "Duration may not be longer than 168 hours (7 days). Provided: 192 hours (8 days)");

assertStringChecks("Answer title", builder::addAnswer)
.checksNotNull()
Expand Down

0 comments on commit 827de6c

Please sign in to comment.