Package cn.nukkit.positiontracking
Class PositionTrackingStorage
java.lang.Object
cn.nukkit.positiontracking.PositionTrackingStorage
- All Implemented Interfaces:
Closeable
,AutoCloseable
@PowerNukkitOnly
@Since("1.4.0.0-PN")
@ParametersAreNonnullByDefault
public class PositionTrackingStorage
extends Object
implements Closeable
Stores a sequential range of
PositionTracking
objects in a file. The read operation is cached.
This object holds a file handler and must be closed when it is no longer needed.
Once closed the instance cannot be reused.
- Author:
- joserobjr
-
Field Summary
-
Constructor Summary
ConstructorDescriptionPositionTrackingStorage
(int startIndex, File persistenceFile) Opens or create the file and all directories in the path automatically.PositionTrackingStorage
(int startIndex, File persistenceFile, int maxStorage) Opens or create the file and all directories in the path automatically. -
Method Summary
Modifier and TypeMethodDescriptionaddNewPosition
(NamedPosition position) Adds the given position as a new entry in this storage, even if the position is already registered and enabled.addNewPosition
(NamedPosition position, boolean enabled) Adds the given position as a new entry in this storage, even if the position is already registered and enabled.addOrReusePosition
(NamedPosition position) Attempts to reuse an existing and enabled trackingHandler for the given position, if none is found than a new handler is created if the limit was not exceeded.void
close()
protected void
finalize()
findTrackingHandler
(NamedPosition position) it.unimi.dsi.fastutil.ints.IntList
it.unimi.dsi.fastutil.ints.IntList
findTrackingHandlers
(NamedPosition pos, boolean onlyEnabled) it.unimi.dsi.fastutil.ints.IntList
findTrackingHandlers
(NamedPosition pos, boolean onlyEnabled, int limit) int
getPosition
(int trackingHandler) Retrieves thePositionTracking
object that is assigned to the given trackingHandler.getPosition
(int trackingHandler, boolean onlyEnabled) Retrieves thePositionTracking
object that is assigned to the given trackingHandler.int
boolean
hasPosition
(int trackingHandler) boolean
hasPosition
(int trackingHandler, boolean onlyEnabled) void
invalidateHandler
(int trackingHandler) boolean
isEnabled
(int trackingHandler) boolean
setEnabled
(int trackingHandler, boolean enabled)
-
Field Details
-
DEFAULT_MAX_STORAGE
- See Also:
-
-
Constructor Details
-
PositionTrackingStorage
@PowerNukkitOnly @Since("1.4.0.0-PN") public PositionTrackingStorage(int startIndex, File persistenceFile) throws IOException Opens or create the file and all directories in the path automatically. The given start index will be used in new files and will be checked when opening files. If the file being opened don't matches this value internally than anIllegalArgumentException
will be thrown.- Parameters:
startIndex
- The number of the first handler. Must be higher than 0 and must match the number of the existing file.persistenceFile
- The file being opened or created. Parent directories will also be created if necessary.- Throws:
IOException
- If an error has occurred while reading, parsing or creating the fileIllegalArgumentException
- If opening an existing file and the internal startIndex don't match the given startIndex
-
PositionTrackingStorage
@PowerNukkitOnly @Since("1.4.0.0-PN") public PositionTrackingStorage(int startIndex, File persistenceFile, int maxStorage) throws IOException Opens or create the file and all directories in the path automatically. The given start index will be used in new files and will be checked when opening files. If the file being opened don't matches this value internally than anIllegalArgumentException
will be thrown.- Parameters:
startIndex
- The number of the first handler. Must be higher than 0 and must match the number of the existing file.persistenceFile
- The file being opened or created. Parent directories will also be created if necessary.maxStorage
- The maximum amount of positions that this storage may hold. It cannot be changed after creation. Ignored when loading an existing file. When zero or negative, a default value will be used.- Throws:
IOException
- If an error has occurred while reading, parsing or creating the fileIllegalArgumentException
- If opening an existing file and the internal startIndex don't match the given startIndex
-
-
Method Details
-
getPosition
@PowerNukkitOnly @Since("1.4.0.0-PN") @Nullable public PositionTracking getPosition(int trackingHandler) throws IOException Retrieves thePositionTracking
object that is assigned to the given trackingHandler. The handler must be valid for this storage.This call may return a cached result but the returned object can be modified freely.
- Parameters:
trackingHandler
- A valid handler for this storage- Returns:
- A clone of the cached result.
- Throws:
IOException
- If an error has occurred while accessing the fileIllegalArgumentException
- If the trackingHandler is not valid for this storage
-
getPosition
@PowerNukkitOnly @Since("1.4.0.0-PN") @Nullable public PositionTracking getPosition(int trackingHandler, boolean onlyEnabled) throws IOException Retrieves thePositionTracking
object that is assigned to the given trackingHandler. The handler must be valid for this storage.This call may return a cached result but the returned object can be modified freely.
- Parameters:
trackingHandler
- A valid handler for this storageonlyEnabled
- When false, disabled positions that wasn't invalidated may be returned. Caching only works when this is set to true- Returns:
- A clone of the cached result.
- Throws:
IOException
- If an error has occurred while accessing the fileIllegalArgumentException
- If the trackingHandler is not valid for this storage
-
addOrReusePosition
@PowerNukkitOnly @Since("1.4.0.0-PN") public OptionalInt addOrReusePosition(NamedPosition position) throws IOException Attempts to reuse an existing and enabled trackingHandler for the given position, if none is found than a new handler is created if the limit was not exceeded.- Parameters:
position
- The position that needs a handler- Returns:
- The trackingHandler assigned to the position or an empty OptionalInt if none was found and this storage is full
- Throws:
IOException
- If an error occurred while reading or writing the file
-
addNewPosition
@PowerNukkitOnly @Since("1.4.0.0-PN") public OptionalInt addNewPosition(NamedPosition position) throws IOException Adds the given position as a new entry in this storage, even if the position is already registered and enabled.- Parameters:
position
- The position that needs a handler- Returns:
- The trackingHandler assigned to the position or an empty OptionalInt if none was found and this storage is full
- Throws:
IOException
- If an error occurred while reading or writing the file
-
addNewPosition
@PowerNukkitOnly @Since("1.4.0.0-PN") public OptionalInt addNewPosition(NamedPosition position, boolean enabled) throws IOException Adds the given position as a new entry in this storage, even if the position is already registered and enabled.- Parameters:
position
- The position that needs a handlerenabled
- If the position will be added as enabled or disabled- Returns:
- The trackingHandler assigned to the position or an empty OptionalInt if none was found and this storage is full
- Throws:
IOException
- If an error occurred while reading or writing the file
-
findTrackingHandler
@PowerNukkitOnly @Since("1.4.0.0-PN") @Nonnull public OptionalInt findTrackingHandler(NamedPosition position) throws IOException - Throws:
IOException
-
invalidateHandler
@PowerNukkitOnly @Since("1.4.0.0-PN") public void invalidateHandler(int trackingHandler) throws IOException - Throws:
IOException
-
isEnabled
@PowerNukkitOnly @Since("1.4.0.0-PN") public boolean isEnabled(int trackingHandler) throws IOException - Throws:
IOException
-
setEnabled
@PowerNukkitOnly @Since("1.4.0.0-PN") public boolean setEnabled(int trackingHandler, boolean enabled) throws IOException - Throws:
IOException
-
hasPosition
@PowerNukkitOnly @Since("1.4.0.0-PN") public boolean hasPosition(int trackingHandler) throws IOException - Throws:
IOException
-
hasPosition
@PowerNukkitOnly @Since("1.4.0.0-PN") public boolean hasPosition(int trackingHandler, boolean onlyEnabled) throws IOException - Throws:
IOException
-
findTrackingHandlers
@PowerNukkitOnly @Since("1.4.0.0-PN") @Nonnull public it.unimi.dsi.fastutil.ints.IntList findTrackingHandlers(NamedPosition pos) throws IOException - Throws:
IOException
-
findTrackingHandlers
@PowerNukkitOnly @Since("1.4.0.0-PN") @Nonnull public it.unimi.dsi.fastutil.ints.IntList findTrackingHandlers(NamedPosition pos, boolean onlyEnabled) throws IOException - Throws:
IOException
-
findTrackingHandlers
@PowerNukkitOnly @Since("1.4.0.0-PN") @Nonnull public it.unimi.dsi.fastutil.ints.IntList findTrackingHandlers(NamedPosition pos, boolean onlyEnabled, int limit) throws IOException - Throws:
IOException
-
getStartingHandler
-
getMaxHandler
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
finalize
-