Class PathFinder
- java.lang.Object
-
- eu.simuline.util.PathFinder
-
- Direct Known Subclasses:
PathFinder.Primary,PathFinder.Secondary
public abstract class PathFinder extends Object
Emulates the unix shell commandfindwhich is invoked asfind [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] (expression)*. Ifpathis left out (possibly more than one path?), then the default is., i.e. the current directory. Thepathneed not be a directory. If it is a proper file then typically wildcards are used. Then the name is matched. If no expression is given, the default is-print. So we may assume, that both, a path and a non-empty sequence of expressions, are given.The idea behind the
findcommand is, that, starting with the files matching thepath, eachexpressionserves as a filter, feeding the filter corresponding to the following expression. Each filter can have a side effect. Expressions may be either tests or actions. For tests, the focus is on the filter-functionality, whereas actions are trivial filters just passing the files they receive. Actions are applied because of their side effects likeprint.The most basic kind of finder corresponds with the command
find pathiterating just over the files in the path. This kind of finder is returned by the static methodpath(Path). Starting with this basic finder, further finders can be added to the pipe using the member methodsname(String)andprint(PrintStream). Created: Wed Nov 21 17:29:41 2012- Version:
- 1.0
- Author:
- Ernst Reissner
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classPathFinder.AndFilterOne of the logical operations of filters: Returns a filter which passes a path iff all original filters inPathFinder.AndFilter.filtersdo so.static interfacePathFinder.CallableTo be implemented by java classes to apply a filter to a path and give feedback whether this filter succeeded.(package private) static classPathFinder.ExecFilterFilter executing a shell command and passes the path received if the shell command succeeds according to its return code.(package private) static classPathFinder.ExecJavaFilterFilter executing a java class implementingPathFinder.Callablewhich passes the path received if the methodPathFinder.Callable.call(Path)succeeds according to its return code.(package private) static classPathFinder.FilterRepresents a path filter.(package private) static classPathFinder.NameFilterFilters paths by name.(package private) static classPathFinder.NegFilterOne of the logical operations of filters: Returns a filter which passes a path iff the original filterPathFinder.NegFilter.tbNegFilterdoes not.(package private) static classPathFinder.OrFilterOne of the logical operations of filters: Returns a filter which passes a path iff at least one of the original filters inPathFinder.OrFilter.filtersdo so.(package private) static classPathFinder.PrimaryThe most basic kind of finder: Methodpath(Path)returns an instance of this.(package private) classPathFinder.PrintFilterA filter passing all information and printing it onPathFinder.PrintFilter.str.(package private) static classPathFinder.SecondaryA finder wrapping aPathFinder.Filter.
-
Field Summary
Fields Modifier and Type Field Description static PathFinder.FilterCAN_EXECFilter passing the path received iff it is executable.static PathFinder.FilterCAN_READFilter passing the path received iff it is readable.static PathFinder.FilterCAN_WRITEFilter passing the path received iff it is writable.static StringEXEC_ARGForPathFinder.ExecFilters representing a filter invoking a shell command, this string represents the argument of the command which is in the original find a{}and which is replaced by the path name received by the preceding portion of the pipe.static PathFinder.FilterFALSEA filter passing no path.static PathFinder.FilterIS_DIRFilter passing the path received iff it is a regular path.static PathFinder.FilterIS_PATHFilter passing the path received iff it is a regular path.static PathFinder.FilterTRUEA filter passing all paths.
-
Constructor Summary
Constructors Modifier Constructor Description privatePathFinder()This is declaredprivateto prevent instantiation.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description PathFinderadd(PathFinder.Filter filter)Returns a finder by attachig the given filter.PathFinderand(PathFinder.Filter[] filters)Convenience method: Returns a finder by attaching an and-filter.PathFinderexec(String[] cmd)Convenience method: adds an execution filter to this finder.static PathFinder.FilterexecFilter(String[] cmd)Returns a filter invoking a shell command: just passes the paths received by this finder further if the command succeeds according to its return value.PathFinderexecJava(PathFinder.Callable callable)Convenience method: adds a java execution filter to this finder.static PathFinder.FilterexecJavaFilter(PathFinder.Callable callable)Returns a filter invoking methodPathFinder.Callable.call(Path)of the givencallable.abstract booleanhasNext()Returns whether this PathFinder can emit another path.static voidmain(String[] args)PathFindername(String pattern)Convenience method: adds a name filter to this finder.static PathFinder.FilternameFilter(String pattern)Returns a filter passing a path iff its (short) names of which match the regular expressionpattern.abstract Pathnext()Returns the next path this finder can emit.PathFindernot(PathFinder.Filter filter)Convenience method: Returns a finder by attaching the inverse of the given filter.PathFinderor(PathFinder.Filter[] filters)Convenience method: Returns a finder by attaching an or-filter.static PathFinderpath(Path path)Returns a basic finder, emitting the given path if it exists and is accessible and, recursively, if it is a directory all paths therein.PathFinderprint(PrintStream str)Adds a trivial filter passing all paths received by this finder printing their full names tostr.
-
-
-
Field Detail
-
EXEC_ARG
public static final String EXEC_ARG
ForPathFinder.ExecFilters representing a filter invoking a shell command, this string represents the argument of the command which is in the original find a{}and which is replaced by the path name received by the preceding portion of the pipe. CAUTION: It must be used this instance and no other equivalent string{}.- See Also:
- Constant Field Values
-
TRUE
public static final PathFinder.Filter TRUE
A filter passing all paths. This corresponds the test-truein the original find command.
-
FALSE
public static final PathFinder.Filter FALSE
A filter passing no path. This corresponds the test-falsein the original find command.
-
CAN_EXEC
public static final PathFinder.Filter CAN_EXEC
Filter passing the path received iff it is executable. This corresponds the test-executablein the original find command. Do not mix up withPathFinder.ExecFilter.
-
CAN_READ
public static final PathFinder.Filter CAN_READ
Filter passing the path received iff it is readable. This corresponds the test-readablein the original find command.
-
CAN_WRITE
public static final PathFinder.Filter CAN_WRITE
Filter passing the path received iff it is writable. This corresponds the test-writablein the original find command.
-
IS_PATH
public static final PathFinder.Filter IS_PATH
Filter passing the path received iff it is a regular path. This corresponds the test-type fin the original find command.
-
IS_DIR
public static final PathFinder.Filter IS_DIR
Filter passing the path received iff it is a regular path. This corresponds the test-type din the original find command.
-
-
Method Detail
-
path
public static PathFinder path(Path path)
Returns a basic finder, emitting the given path if it exists and is accessible and, recursively, if it is a directory all paths therein. The ordering is the same as in original find-command. The expressionPathFinder.path(path)corresponds withfind path, except that the find command implicitly adds a print filter as defined inprint(PrintStream). Note also that, unlike th original find, no wildcards are supported.- Returns:
- an instance of
PathFinder.Primary
-
print
public PathFinder print(PrintStream str)
Adds a trivial filter passing all paths received by this finder printing their full names tostr.
-
add
public PathFinder add(PathFinder.Filter filter)
Returns a finder by attachig the given filter. For adding the most common filters, there are convenience methods.
-
name
public PathFinder name(String pattern)
Convenience method: adds a name filter to this finder.- See Also:
nameFilter(String)
-
exec
public PathFinder exec(String[] cmd)
Convenience method: adds an execution filter to this finder.- Returns:
- an instance of
PathFinder.Secondaryinstantiated with a filter of typePathFinder.ExecFilter - See Also:
execFilter(String[])
-
execJava
public PathFinder execJava(PathFinder.Callable callable)
Convenience method: adds a java execution filter to this finder.- Returns:
- an instance of
PathFinder.Secondaryinstantiated with a filter of typePathFinder.ExecJavaFilter - See Also:
execJavaFilter(Callable)
-
not
public PathFinder not(PathFinder.Filter filter)
Convenience method: Returns a finder by attaching the inverse of the given filter. This corresponds the tests\! expr1in the original find command.- See Also:
PathFinder.Filter.not()
-
and
public PathFinder and(PathFinder.Filter[] filters)
Convenience method: Returns a finder by attaching an and-filter. This corresponds the tests\! expr1in the original find command.- See Also:
PathFinder.Filter.and(Filter[])
-
or
public PathFinder or(PathFinder.Filter[] filters)
Convenience method: Returns a finder by attaching an or-filter. This corresponds the tests\! expr1in the original find command.- See Also:
PathFinder.Filter.or(Filter[])
-
nameFilter
public static PathFinder.Filter nameFilter(String pattern)
Returns a filter passing a path iff its (short) names of which match the regular expressionpattern.- Returns:
- an instance of
PathFinder.Secondaryinstantiated with a filter of typePathFinder.NameFilter
-
execFilter
public static PathFinder.Filter execFilter(String[] cmd)
Returns a filter invoking a shell command: just passes the paths received by this finder further if the command succeeds according to its return value. Example in original find-terminology:find . -exec grep "pattern without quotes" {} \; -print. Here, the portion specifying execution is-exec grep "pattern without quotes" {} \;: It starts with-execand ends with\;. Note that the commandgrephas arguments"pattern without quotes"and{}. The first argument must be quoted because it contains whitespace and would otherwise be interpreted as three arguments. The second argument{}is silently replaced by the path name received by the preceeding portion of the find-command.- Parameters:
cmd- a shell command with its arguments. The 0th entry is the command itself and the others are arguments. Be aware when escape sequences are needed. Quotes ensuring that an argument is interpreted as a unit must be omitted.In the original find command,
{}represents arguments to be replaced by the path name received by the preceding portion of the pipe. These must be represented by the object instanceEXEC_ARGwhich is also a string{}but it is wrong to put an equivalent string as e.g.new String("{}").For example to obtain a grep use
new String{} {"grep", "pattern without quotes", EXEC_ARG}- Returns:
- a filter of type
PathFinder.ExecFilter - See Also:
execJavaFilter(Callable)
-
execJavaFilter
public static PathFinder.Filter execJavaFilter(PathFinder.Callable callable)
Returns a filter invoking methodPathFinder.Callable.call(Path)of the givencallable.- Parameters:
callable- A callable defining a filter. If parameters are required, these must be given when instantiating the callable or later using setter methods.- Returns:
- a filter of type
PathFinder.ExecFilter - See Also:
execFilter(String[])
-
hasNext
public abstract boolean hasNext()
Returns whether this PathFinder can emit another path.- See Also:
next()
-
next
public abstract Path next()
Returns the next path this finder can emit. This does not throw an exception iffhasNext()returns true.
-
main
public static void main(String[] args)
-
-