Package com.tccc.kos.commons.path
Class PathMgr
java.lang.Object
com.tccc.kos.commons.path.PathMgr
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:
- 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. - 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 Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionSame as above except that it accepts an Enum value.Same as above, excepts accepts an Enum value and Enum baseName.Same as above, excepts accepts an Enum value.Adds a named path to thePathMgr
.Adds a named path.Same as above, excepts accepts an Enum value.Same as above, excepts accepts an Enum value.Returns aFile
for the named path.Returns aFile
using the named path as a base directory and adding the specified path.Return all named pathsSame as above, excepts accepts an Enum value.Returns the named path with the specified name.getUserFile
(String path) Returns aFile
relative to the user's "home" directory.void
Same as above, excepts accepts an Enum value.void
Same as above, excepts accepts an Enum value.void
Creates a directory for the named path, if it doesn't already exist.void
Creates a directory for the named path as a base directory and adding the specified path.
-
Field Details
-
ROOT
- See Also:
-
-
Method Details
-
addPath
Adds a named path to thePathMgr
. 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 pathbase
- optional basepath
- the path
-
addPath
Same as above except that it accepts an Enum value.- Parameters:
name
- the unique name (identifier) of the path as an enumerated itembase
- the optional basepath
- the path
-
addPath
Adds a named path. Ifbase
is specified, then thepath
is relative to the specified base, otherwise it is considered absolute.- Parameters:
name
- the unique name of the pathbaseName
- optional base identified by namepath
- the path
-
addPath
Same as above, excepts accepts an Enum value. -
addPath
Same as above, excepts accepts an Enum value and Enum baseName. -
getPath
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
Same as above, excepts accepts an Enum value. -
getFile
Returns aFile
for the named path.- Parameters:
name
- the name of the path- Returns:
- a
File
for the given named path - Throws:
IllegalArgumentException
- if the givenname
is undefined
-
getFile
Same as above, excepts accepts an Enum value. -
getFile
Returns aFile
using the named path as a base directory and adding the specified path.- Parameters:
name
- the name of the path to use as a basepath
- the path to append to the base
-
getFile
Same as above, excepts accepts an Enum value. -
mkdirs
Creates a directory for the named path, if it doesn't already exist.- Parameters:
name
- the name of the path
-
mkdirs
Same as above, excepts accepts an Enum value. -
mkdirs
Creates a directory for the named path as a base directory and adding the specified path.- Parameters:
name
- the name of the path
-
mkdirs
Same as above, excepts accepts an Enum value. -
getUserFile
Returns aFile
relative to the user's "home" directory.- Parameters:
path
- the path to add to the home directory
-
getNamedPaths
Return all named paths
-