Class FileAccess

java.lang.Object
net.luis.utils.io.FileAccess
All Implemented Interfaces:
AutoCloseable

public class FileAccess extends Object implements AutoCloseable
Class to manage file access in a thread-safe way.
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

    Fields
    Modifier and Type
    Field
    Description
    private static final Map<String,Boolean>
    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.
    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

    Constructors
    Constructor
    Description
    FileAccess(@NotNull Path file)
    Constructs a new file access instance for the given file.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    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
    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
    equals(Object object)
     
    @NotNull Path
    Returns the file accessed by this instance of the class.
    int
     
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • ACCESSED_FILES

      private static final Map<String,Boolean> 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

      private final ReentrantLock lock
      Lock to ensure that one thread only accesses the file at a time.
    • file

      private final Path file
      File that is managed by this instance of the class.
    • closed

      private boolean closed
      Flag to check if the file access is closed.
  • Constructor Details

    • FileAccess

      public FileAccess(@NotNull @NotNull Path file)
      Constructs a new file access instance for the given file.
      Parameters:
      file - The file to access
      Throws:
      NullPointerException - If the file is null
      IllegalStateException - 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

      @NotNull public @NotNull Path 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 closed
      UncheckedIOException - 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 closed
      UncheckedIOException - 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 interface AutoCloseable
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object