Class Finder
- java.lang.Object
-
- eu.simuline.util.Finder
-
- Direct Known Subclasses:
Finder.Primary,Finder.Secondary
public abstract class Finder extends java.lang.ObjectEmulates 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(File). 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 classFinder.AndFilterOne of the logical operations of filters: Returns a filter which passes a file iff all original filters inFinder.AndFilter.filtersdo so.static interfaceFinder.CallableTo be implemented by java classes to apply a filter to a file and give feedback whether this filter succeeded.(package private) static classFinder.ExecFilterFilter executing a shell command and passes the file received if the shell command succeeds according to its return code.(package private) static classFinder.ExecJavaFilterFilter executing a java class implementingFinder.Callablewhich passes the file received if the methodFinder.Callable.call(File)succeeds according to its return code.(package private) static classFinder.FilterRepresents a file filter.(package private) static classFinder.NameFilterFilters files by name.(package private) static classFinder.NegFilterOne of the logical operations of filters: Returns a filter which passes a file iff the original filterFinder.NegFilter.tbNegFilterdoes not.(package private) static classFinder.OrFilterOne of the logical operations of filters: Returns a filter which passes a file iff at least one of the original filters inFinder.OrFilter.filtersdo so.(package private) static classFinder.PrimaryThe most basic kind of finder: Methodpath(File)returns an instance of this.(package private) classFinder.PrintFilterA filter passing all information and printing it onFinder.PrintFilter.str.(package private) static classFinder.SecondaryA finder wrapping aFinder.Filter.
-
Field Summary
Fields Modifier and Type Field Description static Finder.FilterCAN_EXECFilter passing the file received iff it is executable.static Finder.FilterCAN_READFilter passing the file received iff it is readable.static Finder.FilterCAN_WRITEFilter passing the file received iff it is writable.static java.lang.StringEXEC_ARGForFinder.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 file name received by the preceding portion of the pipe.static Finder.FilterFALSEA filter passing no file.static Finder.FilterIS_DIRFilter passing the file received iff it is a regular file.static Finder.FilterIS_FILEFilter passing the file received iff it is a regular file.static Finder.FilterTRUEA filter passing all files.
-
Constructor Summary
Constructors Modifier Constructor Description privateFinder()This is declaredprivateto prevent instantiation.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Finderadd(Finder.Filter filter)Returns a finder by attachig the given filter.Finderand(Finder.Filter[] filters)Convenience method: Returns a finder by attaching an and-filter.Finderexec(java.lang.String[] cmd)Convenience method: adds an execution filter to this finder.static Finder.FilterexecFilter(java.lang.String[] cmd)Returns a filter invoking a shell command: just passes the files received by this finder further if the command succeeds according to its return value.FinderexecJava(Finder.Callable callable)Convenience method: adds a java execution filter to this finder.static Finder.FilterexecJavaFilter(Finder.Callable callable)Returns a filter invoking methodFinder.Callable.call(File)of the givencallable.abstract booleanhasNext()Returns whether this Finder can emit another file.static voidmain(java.lang.String[] args)Findername(java.lang.String pattern)Convenience method: adds a name filter to this finder.static Finder.FilternameFilter(java.lang.String pattern)Returns a filter passing a file iff its (short) names of which match the regular expressionpattern.abstract java.io.Filenext()Returns the next file this finder can emit.Findernot(Finder.Filter filter)Convenience method: Returns a finder by attaching the inverse of the given filter.Finderor(Finder.Filter[] filters)Convenience method: Returns a finder by attaching an or-filter.static Finderpath(java.io.File file)Returns a basic finder, emitting the given file if it exists and is accessible and, recursively, if it is a directory all files therein.Finderprint(java.io.PrintStream str)Adds a trivial filter passing all files received by this finder printing their full names tostr.
-
-
-
Field Detail
-
EXEC_ARG
public static final java.lang.String EXEC_ARG
ForFinder.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 file 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 Finder.Filter TRUE
A filter passing all files. This corresponds the test-truein the original find command.
-
FALSE
public static final Finder.Filter FALSE
A filter passing no file. This corresponds the test-falsein the original find command.
-
CAN_EXEC
public static final Finder.Filter CAN_EXEC
Filter passing the file received iff it is executable. This corresponds the test-executablein the original find command. Do not mix up withFinder.ExecFilter.
-
CAN_READ
public static final Finder.Filter CAN_READ
Filter passing the file received iff it is readable. This corresponds the test-readablein the original find command.
-
CAN_WRITE
public static final Finder.Filter CAN_WRITE
Filter passing the file received iff it is writable. This corresponds the test-writablein the original find command.
-
IS_FILE
public static final Finder.Filter IS_FILE
Filter passing the file received iff it is a regular file. This corresponds the test-type fin the original find command.
-
IS_DIR
public static final Finder.Filter IS_DIR
Filter passing the file received iff it is a regular file. This corresponds the test-type din the original find command.
-
-
Method Detail
-
path
public static Finder path(java.io.File file)
Returns a basic finder, emitting the given file if it exists and is accessible and, recursively, if it is a directory all files therein. The ordering is the same as in original find-command. The expressionFinder.path(file)corresponds withfind file, 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
Finder.Primary
-
print
public Finder print(java.io.PrintStream str)
Adds a trivial filter passing all files received by this finder printing their full names tostr.
-
add
public Finder add(Finder.Filter filter)
Returns a finder by attachig the given filter. For adding the most common filters, there are convenience methods.
-
name
public Finder name(java.lang.String pattern)
Convenience method: adds a name filter to this finder.- See Also:
nameFilter(String)
-
exec
public Finder exec(java.lang.String[] cmd)
Convenience method: adds an execution filter to this finder.- Returns:
- an instance of
Finder.Secondaryinstantiated with a filter of typeFinder.ExecFilter - See Also:
execFilter(String[])
-
execJava
public Finder execJava(Finder.Callable callable)
Convenience method: adds a java execution filter to this finder.- Returns:
- an instance of
Finder.Secondaryinstantiated with a filter of typeFinder.ExecJavaFilter - See Also:
execJavaFilter(Callable)
-
not
public Finder not(Finder.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:
Finder.Filter.not()
-
and
public Finder and(Finder.Filter[] filters)
Convenience method: Returns a finder by attaching an and-filter. This corresponds the tests\! expr1in the original find command.- See Also:
Finder.Filter.and(Filter[])
-
or
public Finder or(Finder.Filter[] filters)
Convenience method: Returns a finder by attaching an or-filter. This corresponds the tests\! expr1in the original find command.- See Also:
Finder.Filter.or(Filter[])
-
nameFilter
public static Finder.Filter nameFilter(java.lang.String pattern)
Returns a filter passing a file iff its (short) names of which match the regular expressionpattern.- Returns:
- an instance of
Finder.Secondaryinstantiated with a filter of typeFinder.NameFilter
-
execFilter
public static Finder.Filter execFilter(java.lang.String[] cmd)
Returns a filter invoking a shell command: just passes the files 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 file 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 file 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
Finder.ExecFilter - See Also:
execJavaFilter(Callable)
-
execJavaFilter
public static Finder.Filter execJavaFilter(Finder.Callable callable)
Returns a filter invoking methodFinder.Callable.call(File)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
Finder.ExecFilter - See Also:
execFilter(String[])
-
hasNext
public abstract boolean hasNext()
Returns whether this Finder can emit another file.- See Also:
next()
-
next
public abstract java.io.File next()
Returns the next file this finder can emit. This does not throw an exception iffhasNext()returns true.
-
main
public static void main(java.lang.String[] args)
-
-