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 Summary

    Constructors
    Constructor
    Description
    Constructs a new, empty LifecycleMap.
    Constructs a new LifecycleMap with the same mappings as the specified map.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if this map contains a mapping for the specified key.
    long
    Returns the duration of the specified key.
    long
    Returns the remaining time of the specified key.
    long
    getStart(T key)
    Returns the start time of the specified key.
     
    void
    put(T key, long startMillis, long durationMillis)
    Associates the specified value with the specified key in this map.
    void
    remove(T key)
    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
    void
    run()
    Runs the map, removing any expired values.
    int
    Gets the size of the map.
    void
    store(T key, long millis)
    Stores the specified key in this map for the specified duration.
    void
    store(T key, long time, @NotNull TimeUnit unit)
    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
    void
    store(T key, @NotNull Duration duration)
    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • 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

      public void 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
    • store

      public void 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
    • store

      public void store(@NotNull T key, long time, @NotNull @NotNull TimeUnit unit)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
      Parameters:
      key - The key whose associated value is to be returned
      time - The current time, in milliseconds
      unit - The time unit of the duration
    • store

      public void store(@NotNull T key, @NotNull @NotNull Duration duration)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
      Parameters:
      key - The key whose associated value is to be returned
      duration - The duration to store the key for
    • remove

      public void remove(@NotNull T key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
      Parameters:
      key - The key whose associated value is to be returned
    • 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, or -1 if the key is not present
    • 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
    • iterator

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