instance

public class CooldownManager {
private final Map<UUID, Instant> map = new HashMap<>();

// Set cooldown
public void setCooldown(UUID key, Duration duration) {
map.put(key, Instant.now().plus(duration));
}

// Check if cooldown has expired
public boolean hasCooldown(UUID key) {
Instant cooldown = map.get(key);
return cooldown != null && Instant.now().isBefore(cooldown);
}
public class CooldownManager {
private final Map<UUID, Instant> map = new HashMap<>();

// Set cooldown
public void setCooldown(UUID key, Duration duration) {
map.put(key, Instant.now().plus(duration));
}

// Check if cooldown has expired
public boolean hasCooldown(UUID key) {
Instant cooldown = map.get(key);
return cooldown != null && Instant.now().isBefore(cooldown);
}
I was reading on how to make a cooldown when i stumbled across this block of code. I was asking myslef if the last line should be
!Instant.now().isBefore(cooldown);
!Instant.now().isBefore(cooldown);
since without it, we would be checking if the current time is before cooldown (which we dont) so we should neagte that. right?
10 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @Omaster! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st3w ago
the current code without the negation is checking if the current time is before the cooldown in other words if the cooldown ends in the future (or now) i.e. it is active if it ends in the future
FirasRG
FirasRG3w ago
hello the method name hasCooldown isn't clear enough imo id name it isCooldownEpired
dan1st
dan1st3w ago
world That's the opposite of what the method is currently doing
FirasRG
FirasRG3w ago
ye they can reverse the return value. im looking for a way to make it more readable
dan1st
dan1st3w ago
hasCooldown <-> hasActiveCooldown <-> is the current time before the expiration time of the cooldown <-> Does the cooldown expire in the future? if they care about equality ("the cooldown should still be active in the last millisecond of the cooldown"), that would change behavior there is isBefore and isAfter but if you want isAfterOrEqual, you'd need to use isBefore and switch the receiver and parameter
FirasRG
FirasRG3w ago
i think that the word "cooldown" is not suitable to this imo it refers to a number of seconds like 10, 5, 2 it would be better to say timeout now().isBefore(timeout) it makes sense this way ig
dan1st
dan1st3w ago
tbh I'd say it's fine, especially if it's also called cooldown in interactions with other systems (e.g. telling the user) But I'd just wait for the question author - I think discussing these things without the doesn't make much sense
FirasRG
FirasRG3w ago
got it
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?