Package xyz.gmitch215.socketmc.util
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>
A map that contains objects for a certain amount of time.
-
Constructor Summary
ConstructorDescriptionConstructs a new, empty LifecycleMap.LifecycleMap
(@Nullable LifecycleMap<T> map) Constructs a new LifecycleMap with the same mappings as the specified map. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns true if this map contains a mapping for the specified identifier.boolean
containsKey
(T key) Returns true if this map contains a mapping for the specified key.Gets an immutable copy of the contents of the LifecycleMap as a map of identifiers to keys.long
getDuration
(T key) Returns the duration of the specified key.getIdentifier
(T key) Returns the identifier of the specified key.Fetches an immutable set of the identifiers in the map.long
getRemainingTime
(T key) Returns the remaining time of the specified key.long
Returns the start time of the specified key.boolean
isInfinite
(T key) Returns true if the specified key has an infinite duration.iterator()
Associates the specified value with the specified key in this map.put
(@NotNull Identifier id, T key, long startMillis, long durationMillis) Associates the specified value with the specified key in this map.void
Removes the mapping for the specified key from this map if present.remove
(@NotNull Identifier id) Removes the mapping for the specified identifier from this map if present.static void
removeIn
(@NotNull Identifier id, @NotNull LifecycleMap<?>... maps) Removes a specific key from multiple LifecycleMaps.void
run()
Runs the map, removing any expired values.int
size()
Gets the size of the map.Stores the specified key in this map for the specified duration.Stores the specified key in this map for the specified duration.Stores the specified key in this map for the specified duration.store
(@NotNull Identifier id, T key, long millis) Stores the specified key in this map for the specified duration.Stores the specified key in this map for the specified duration.Stores the specified key in this map for the specified duration.storeInfinite
(T key) Stores the specified key in this map for an infinite duration.storeInfinite
(@NotNull Identifier id, T key) Stores the specified key in this map for an infinite duration.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
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 keykey
- The key with which the specified value is to be associatedstartMillis
- The start time of the value, in millisecondsdurationMillis
- The duration of the value, in milliseconds (use-1
for infinite)- Returns:
- The identifier of the key
-
put
Associates the specified value with the specified key in this map.- Parameters:
key
- The key with which the specified value is to be associatedstartMillis
- The start time of the value, in millisecondsdurationMillis
- 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 keykey
- The key to storemillis
- 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:
key
- The key to storemillis
- 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 @NotNull Identifier id, @NotNull T key, long time, @NotNull @NotNull TimeUnit unit) Stores the specified key in this map for the specified duration.- Parameters:
id
- The identifier for the keykey
- The key whose associated value is to be returnedtime
- The current time, in millisecondsunit
- 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 returnedtime
- The current time, in millisecondsunit
- The time unit of the duration- Returns:
- The identifier for the key
-
store
@NotNull public @NotNull Identifier store(@NotNull @NotNull Identifier id, @NotNull T key, @NotNull @NotNull Duration duration) Stores the specified key in this map for the specified duration.- Parameters:
id
- The identifier for the keykey
- The key whose associated value is to be returnedduration
- 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 returnedduration
- The duration to store the key for- Returns:
- The identifier for the key
-
storeInfinite
Stores the specified key in this map for an infinite duration.- Parameters:
id
- The identifier for the keykey
- The key whose associated value is to be returned- Returns:
- The identifier for the key
-
storeInfinite
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
Removes the mapping for the specified key from this map if present.- Parameters:
key
- The key whose associated value is to be returned
-
remove
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
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
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
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, orLong.MAX_VALUE
if the key is infinite
-
isInfinite
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
Returns the identifier of the specified key.- Parameters:
key
- The key whose identifier is to be returned- Returns:
- The identifier of the key
-
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
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
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
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
-
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 removemaps
- The LifecycleMaps to remove the key from- Throws:
IllegalArgumentException
- if the maps are null
-