Class CooldownMap<T>

java.lang.Object
dev.demeng.pluginbase.cooldown.CooldownMap<T>
Type Parameters:
T - The type

public class CooldownMap<T> extends Object
A self-populating map of cooldown instances.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> @NotNull CooldownMap<T>
    create(@NotNull Cooldown base)
    Creates a new collection with the cooldown properties defined by the base instance.
    long
    elapsed(T key)
    Returns the number of milliseconds that have elapsed since the cooldown for the given key was last tested.
    @NotNull Cooldown
    get(T key)
    Gets the internal cooldown instance associated with the given key.
    @NotNull Map<T,Cooldown>
    Gets the cooldowns contained within this collection.
    @NotNull OptionalLong
    Returns the timestamp at which the cooldown for the given key was last tested, as milliseconds since the epoch.
    void
    put(T key, @NotNull Cooldown cooldown)
    Stores a custom cooldown instance for the given key, replacing any existing entry.
    long
    Returns the number of milliseconds remaining on the cooldown for the given key.
    long
    remainingTime(T key, @NotNull TimeUnit unit)
    Returns the remaining cooldown time for the given key expressed in the specified time unit.
    void
    reset(T key)
    Resets the cooldown for the given key so that it behaves as if it was never tested.
    void
    setLastTested(T key, long time)
    Manually sets the last tested timestamp for the cooldown associated with the given key.
    boolean
    test(T key)
    Tests whether the given key is off cooldown and, if so, records the current time as the last tested instant.
    boolean
    Tests whether the given key is off cooldown without recording the current time.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      @NotNull public static <T> @NotNull CooldownMap<T> create(@NotNull @NotNull Cooldown base)
      Creates a new collection with the cooldown properties defined by the base instance.
      Parameters:
      base - The cooldown to base off
      Returns:
      A new collection
    • get

      @NotNull public @NotNull Cooldown get(@NotNull T key)
      Gets the internal cooldown instance associated with the given key.

      The inline Cooldown methods in this class should be used to access the functionality of the cooldown as opposed to calling the methods directly via the instance returned by this method.

      Parameters:
      key - The key
      Returns:
      A cooldown instance
    • put

      public void put(@NotNull T key, @NotNull @NotNull Cooldown cooldown)
      Stores a custom cooldown instance for the given key, replacing any existing entry.

      The provided cooldown must have the same duration as the base cooldown, since the expiring cache is configured around that duration.

      Parameters:
      key - The key to associate the cooldown with
      cooldown - The cooldown instance to store
      Throws:
      IllegalArgumentException - If the cooldown's duration differs from the base cooldown's duration
    • getAll

      @NotNull public @NotNull Map<T,Cooldown> getAll()
      Gets the cooldowns contained within this collection.
      Returns:
      The backing map
    • test

      public boolean test(@NotNull T key)
      Tests whether the given key is off cooldown and, if so, records the current time as the last tested instant. Delegates to Cooldown.test().
      Parameters:
      key - The key to test
      Returns:
      true if the key is not currently on cooldown
    • testSilently

      public boolean testSilently(@NotNull T key)
      Tests whether the given key is off cooldown without recording the current time. Use this when you need to check cooldown status without consuming it. Delegates to Cooldown.testSilently().
      Parameters:
      key - The key to test
      Returns:
      true if the key is not currently on cooldown
    • elapsed

      public long elapsed(@NotNull T key)
      Returns the number of milliseconds that have elapsed since the cooldown for the given key was last tested. Delegates to Cooldown.elapsed().
      Parameters:
      key - The key to check
      Returns:
      Milliseconds elapsed since the last test
    • reset

      public void reset(@NotNull T key)
      Resets the cooldown for the given key so that it behaves as if it was never tested. Delegates to Cooldown.reset().
      Parameters:
      key - The key whose cooldown should be reset
    • remainingMillis

      public long remainingMillis(@NotNull T key)
      Returns the number of milliseconds remaining on the cooldown for the given key. Returns 0 if the key is not currently on cooldown. Delegates to Cooldown.remainingMillis().
      Parameters:
      key - The key to check
      Returns:
      Remaining cooldown time in milliseconds, or 0 if not on cooldown
    • remainingTime

      public long remainingTime(@NotNull T key, @NotNull @NotNull TimeUnit unit)
      Returns the remaining cooldown time for the given key expressed in the specified time unit. Returns 0 if the key is not currently on cooldown. Delegates to Cooldown.remainingTime(TimeUnit).
      Parameters:
      key - The key to check
      unit - The time unit to express the result in
      Returns:
      Remaining cooldown time in the given unit, or 0 if not on cooldown
    • getLastTested

      @NotNull public @NotNull OptionalLong getLastTested(@NotNull T key)
      Returns the timestamp at which the cooldown for the given key was last tested, as milliseconds since the epoch. Returns an empty OptionalLong if the cooldown has never been tested. Delegates to Cooldown.getLastTested().
      Parameters:
      key - The key to check
      Returns:
      The last tested timestamp, or an empty optional if never tested
    • setLastTested

      public void setLastTested(@NotNull T key, long time)
      Manually sets the last tested timestamp for the cooldown associated with the given key. Delegates to Cooldown.setLastTested(long).
      Parameters:
      key - The key whose cooldown should be updated
      time - The timestamp to set, as milliseconds since the epoch