java.lang.Object
net.luis.utils.io.FileAccess
- All Implemented Interfaces:
AutoCloseable
Class to manage file access in a thread-safe way.
This class manages the access to a file from multiple threads.
This class manages the access to a file from multiple threads.
To use this class, create an instance of it with the file you want to access.
Share the instance with the threads that need to access the file.
To ensure that only one thread accesses the file at a time,
use the access(FailableConsumer)
or access(FailableFunction)
methods.
These methods will lock the file for the calling thread and unlock it when the action is done.
Each file can only be accessed by one instance of this class at a time.
If you try to access a file that is already accessed by another instance of this class, an exception will be thrown.
-
Field Summary
FieldsModifier and TypeFieldDescriptionMap of all files that are currently accessed by an instance of this class.
Weak references are used to ensure that the map does not prevent the garbage collection of the files.private boolean
Flag to check if the file access is closed.private final Path
File that is managed by this instance of the class.private final ReentrantLock
Lock to ensure that one thread only accesses the file at a time. -
Constructor Summary
ConstructorsConstructorDescriptionFileAccess
(@NotNull Path file) Constructs a new file access instance for the given file. -
Method Summary
Modifier and TypeMethodDescriptionvoid
access
(@NotNull org.apache.commons.lang3.function.FailableConsumer<Path, IOException> action) Access the file with the given action.<T> T
access
(@NotNull org.apache.commons.lang3.function.FailableFunction<Path, T, IOException> action) Access the file with the given action and return the result.boolean
Checks if the file can be accessed.void
close()
Closes the file access.
The call to this method is optional, because the file access will be closed automatically when the instance is garbage collected.private void
Ensures that the file access is open.boolean
@NotNull Path
getFile()
Returns the file accessed by this instance of the class.int
hashCode()
toString()
-
Field Details
-
ACCESSED_FILES
Map of all files that are currently accessed by an instance of this class.
Weak references are used to ensure that the map does not prevent the garbage collection of the files. -
lock
Lock to ensure that one thread only accesses the file at a time. -
file
File that is managed by this instance of the class. -
closed
private boolean closedFlag to check if the file access is closed.
-
-
Constructor Details
-
FileAccess
Constructs a new file access instance for the given file.- Parameters:
file
- The file to access- Throws:
NullPointerException
- If the file is nullIllegalStateException
- If the file is already accessed by another instance of this class
-
-
Method Details
-
ensureOpen
private void ensureOpen()Ensures that the file access is open.- Throws:
IllegalStateException
- If the file access is closed
-
getFile
Returns the file accessed by this instance of the class.- Returns:
- The file
-
canAccess
public boolean canAccess()Checks if the file can be accessed.- Returns:
- True if the file can be accessed, false if not
-
access
public void access(@NotNull @NotNull org.apache.commons.lang3.function.FailableConsumer<Path, IOException> action) Access the file with the given action.- Parameters:
action
- The action to perform on the file- Throws:
IllegalStateException
- If the file access is closedUncheckedIOException
- If the action throws an IOException
-
access
@NotNull public <T> T access(@NotNull @NotNull org.apache.commons.lang3.function.FailableFunction<Path, T, IOException> action) Access the file with the given action and return the result.- Type Parameters:
T
- The type of the result- Parameters:
action
- The action to perform on the file- Returns:
- The result of the action
- Throws:
IllegalStateException
- If the file access is closedUncheckedIOException
- If the action throws an IOException
-
close
public void close()Closes the file access.
The call to this method is optional, because the file access will be closed automatically when the instance is garbage collected.- Specified by:
close
in interfaceAutoCloseable
-
equals
-
hashCode
public int hashCode() -
toString
-