Class LifecycleMap<T>

java.lang.Object
xyz.gmitch215.socketmc.util.LifecycleMap<T>
Type Parameters:
T - The type of object to store
All Implemented Interfaces:
Iterable<T>

public class LifecycleMap<T> extends Object implements Iterable<T>
A map that contains objects for a certain amount of time.
  • Constructor Details

    • LifecycleMap

      public LifecycleMap()
      Constructs a new, empty LifecycleMap.
    • LifecycleMap

      public LifecycleMap(@Nullable @Nullable LifecycleMap<T> map)
      Constructs a new LifecycleMap with the same mappings as the specified map.
      Parameters:
      map - The map whose mappings are to be placed in this map
  • Method Details

    • run

      public void run()
      Runs the map, removing any expired values.
    • put

      @NotNull public @NotNull Identifier put(@NotNull @NotNull Identifier id, @NotNull T key, long startMillis, long durationMillis)
      Associates the specified value with the specified key in this map.
      Parameters:
      id - The identifier for the key
      key - The key with which the specified value is to be associated
      startMillis - The start time of the value, in milliseconds
      durationMillis - The duration of the value, in milliseconds (use -1 for infinite)
      Returns:
      The identifier of the key
    • put

      @NotNull public @NotNull Identifier put(@NotNull T key, long startMillis, long durationMillis)
      Associates the specified value with the specified key in this map.
      Parameters:
      key - The key with which the specified value is to be associated
      startMillis - The start time of the value, in milliseconds
      durationMillis - The duration of the value, in milliseconds (use -1 for infinite)
      Returns:
      The identifier of the key
    • store

      @NotNull public @NotNull Identifier store(@NotNull @NotNull Identifier id, @NotNull T key, long millis)
      Stores the specified key in this map for the specified duration.
      Parameters:
      id - The identifier for the key
      key - The key to store
      millis - The duration to store the key for, in milliseconds (use -1 for infinite)
      Returns:
      The identifier of the key
    • store

      @NotNull public @NotNull Identifier store(@NotNull T key, long millis)
      Stores the specified key in this map for the specified duration.
      Parameters:
      key - The key to store
      millis - The duration to store the key for, in milliseconds (use -1 for infinite)
      Returns:
      The identifier of the key
    • store

      Stores the specified key in this map for the specified duration.
      Parameters:
      id - The identifier for the key
      key - The key whose associated value is to be returned
      time - The current time, in milliseconds
      unit - The time unit of the duration
      Returns:
      The identifier for the key
    • store

      @NotNull public @NotNull Identifier store(@NotNull T key, long time, @NotNull @NotNull TimeUnit unit)
      Stores the specified key in this map for the specified duration.
      Parameters:
      key - The key whose associated value is to be returned
      time - The current time, in milliseconds
      unit - The time unit of the duration
      Returns:
      The identifier for the key
    • store

      Stores the specified key in this map for the specified duration.
      Parameters:
      id - The identifier for the key
      key - The key whose associated value is to be returned
      duration - The duration to store the key for
      Returns:
      The identifier for the key
    • store

      Stores the specified key in this map for the specified duration.
      Parameters:
      key - The key whose associated value is to be returned
      duration - The duration to store the key for
      Returns:
      The identifier for the key
    • storeInfinite

      @NotNull public @NotNull Identifier storeInfinite(@NotNull @NotNull Identifier id, @NotNull T key)
      Stores the specified key in this map for an infinite duration.
      Parameters:
      id - The identifier for the key
      key - The key whose associated value is to be returned
      Returns:
      The identifier for the key
    • storeInfinite

      @NotNull public @NotNull Identifier storeInfinite(@NotNull T key)
      Stores the specified key in this map for an infinite duration.
      Parameters:
      key - The key whose associated value is to be returned
      Returns:
      The identifier for the key
    • remove

      public void remove(@NotNull T key)
      Removes the mapping for the specified key from this map if present.
      Parameters:
      key - The key whose associated value is to be returned
    • remove

      @Nullable public T remove(@NotNull @NotNull Identifier id)
      Removes the mapping for the specified identifier from this map if present.
      Parameters:
      id - The identifier whose associated value is to be removed
      Returns:
      The key that was removed, or null if the identifier was not present
    • getStart

      public long getStart(@NotNull T key)
      Returns the start time of the specified key.
      Parameters:
      key - The key whose start time is to be returned
      Returns:
      The start time of the key, in milliseconds
    • getDuration

      public long getDuration(@NotNull T key)
      Returns the duration of the specified key.
      Parameters:
      key - The key whose duration is to be returned
      Returns:
      The duration of the key, in milliseconds
    • getRemainingTime

      public long getRemainingTime(@NotNull T key)
      Returns the remaining time of the specified key.
      Parameters:
      key - The key whose remaining time is to be returned
      Returns:
      The remaining time of the key (in milliseconds), -1 if the key is not present, or Long.MAX_VALUE if the key is infinite
    • isInfinite

      public boolean isInfinite(@NotNull T key)
      Returns true if the specified key has an infinite duration.
      Parameters:
      key - The key whose duration is to be checked
      Returns:
      True if the key has an infinite duration
    • getIdentifier

      @NotNull public @NotNull Identifier getIdentifier(@NotNull T key)
      Returns the identifier of the specified key.
      Parameters:
      key - The key whose identifier is to be returned
      Returns:
      The identifier of the key
    • getContents

      @NotNull public @NotNull Map<Identifier,T> getContents()
      Gets an immutable copy of the contents of the LifecycleMap as a map of identifiers to keys.
      Returns:
      The contents of the LifecycleMap
    • getIdentifiers

      @NotNull public @NotNull Set<Identifier> getIdentifiers()
      Fetches an immutable set of the identifiers in the map.
      Returns:
      Set of identifiers for their keys
    • size

      public int size()
      Gets the size of the map.
      Returns:
      The size of the map
    • containsKey

      public boolean containsKey(@NotNull T key)
      Returns true if this map contains a mapping for the specified key.
      Parameters:
      key - The key whose presence in this map is to be tested
      Returns:
      True if this map contains a mapping for the specified key
    • containsIdentifier

      public boolean containsIdentifier(@NotNull @NotNull Identifier id)
      Returns true if this map contains a mapping for the specified identifier.
      Parameters:
      id - The identifier whose presence in this map is to be tested
      Returns:
      True if this map contains a mapping for the specified identifier
    • iterator

      @NotNull public @NotNull Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>
    • removeIn

      public static void removeIn(@NotNull @NotNull Identifier id, @NotNull @NotNull LifecycleMap<?>... maps)
      Removes a specific key from multiple LifecycleMaps.
      Parameters:
      id - The identifier of the key to remove
      maps - The LifecycleMaps to remove the key from
      Throws:
      IllegalArgumentException - if the maps are null