Class PathMgr

java.lang.Object
com.tccc.kos.commons.path.PathMgr

public class PathMgr extends Object
System component that is used to define well-known filesystem paths by name, which are used with relative paths elsewhere in the code. These well-defined paths are constructed in a graph. Any point in this graph can be overridden by external inputs to relocate parts or all of the filesystem graph to an alternate location. This makes it easy to solve a couple recurring use cases:
  1. You run on a Mac, where unexpectedly the root directory is no longer accessible. With the PathMgr, you can relocate an entire filesystem graph to some other part of the filesystem.
  2. You have an existing working directory full of data and want to switch to another set of data. Rather than moving files and directories around on the filesystem, you use the PathMgr to simply redefine the locations via overrides.

Example

 
  public class MyClass {
      @Autowired
      private PathMgr pathMgr;

      public enum MyPath { TOP, DATA, LOGS, MISC }

      public void someMethod() {
          // Configure the paths (examples assume that pathMgr.getPath(PathMgr.ROOT) = "/home/fred"):
          pathMgr.addPath(MyPath.TOP,  PathMgr.ROOT, "top");        // MyPath.TOP  = "/home/fred/top"
          pathMgr.addPath(MyPath.DATA, MyPath.TOP,   "datafiles");  // MyPath.DATA = "/home/fred/top/datafiles"
          pathMgr.addPath(MyPath.LOGS, MyPath.TOP,   "logfiles");   // MyPath.LOGS = "/home/fred/top/logfiles"
          pathMgr.addPath(MyPath.MISC, MyPath.DATA,  "miscfiles");  // MyPath.MISC = "/home/fred/top/datafiles/miscfiles"
      }

      public void anotherMethod() {
          String directory = pathMgr.getPath(MyPath.LOGS);   // returns "/home/fred/top/logfiles"
      }
  }
 
 
Since:
1.0
Version:
2023-03-06
See Also:
  • Field Details

  • Method Details

    • addPath

      public NamedPath addPath(String name, NamedPath base, String path)
      Adds a named path to the PathMgr . If base is specified, then the path is relative to the specified base, otherwise it is considered absolute.
      Parameters:
      name - the unique name (identifier) of the path
      base - optional base
      path - the path
    • addPath

      public NamedPath addPath(Enum<?> name, NamedPath base, String path)
      Same as above except that it accepts an Enum value.
      Parameters:
      name - the unique name (identifier) of the path as an enumerated item
      base - the optional base
      path - the path
    • addPath

      public NamedPath addPath(String name, String baseName, String path)
      Adds a named path. If base is specified, then the path is relative to the specified base, otherwise it is considered absolute.
      Parameters:
      name - the unique name of the path
      baseName - optional base identified by name
      path - the path
    • addPath

      public NamedPath addPath(Enum<?> name, String baseName, String path)
      Same as above, excepts accepts an Enum value.
    • addPath

      public NamedPath addPath(Enum<?> name, Enum<?> baseName, String path)
      Same as above, excepts accepts an Enum value and Enum baseName.
    • getPath

      public NamedPath getPath(String name)
      Returns the named path with the specified name.
      Parameters:
      name - the name of the path to return
      Returns:
      the NamedPath for the name or null
    • getPath

      public NamedPath getPath(Enum<?> name)
      Same as above, excepts accepts an Enum value.
    • getFile

      public File getFile(String name)
      Returns a File for the named path.
      Parameters:
      name - the name of the path
      Returns:
      a File for the given named path
      Throws:
      IllegalArgumentException - if the given name is undefined
    • getFile

      public File getFile(Enum<?> name)
      Same as above, excepts accepts an Enum value.
    • getFile

      public File getFile(String name, String path)
      Returns a File using the named path as a base directory and adding the specified path.
      Parameters:
      name - the name of the path to use as a base
      path - the path to append to the base
    • getFile

      public File getFile(Enum<?> name, String path)
      Same as above, excepts accepts an Enum value.
    • mkdirs

      public void mkdirs(String name)
      Creates a directory for the named path, if it doesn't already exist.
      Parameters:
      name - the name of the path
    • mkdirs

      public void mkdirs(Enum<?> name)
      Same as above, excepts accepts an Enum value.
    • mkdirs

      public void mkdirs(String name, String path)
      Creates a directory for the named path as a base directory and adding the specified path.
      Parameters:
      name - the name of the path
    • mkdirs

      public void mkdirs(Enum<?> name, String path)
      Same as above, excepts accepts an Enum value.
    • getUserFile

      public File getUserFile(String path)
      Returns a File relative to the user's "home" directory.
      Parameters:
      path - the path to add to the home directory
    • getNamedPaths

      public Collection<NamedPath> getNamedPaths()
      Return all named paths