public abstract class Finder extends Object
find which is invoked as
find [-H] [-L] [-P] [-Olevel]
[-D help|tree|search|stat|rates|opt|exec] [path...] (expression)*.
If path is left out (possibly more than one path?),
then the default is ., i.e. the current directory.
The path need 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 find command is,
that, starting with the files matching the path,
each expression serves 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 like print.
The most basic kind of finder corresponds with the command
find path iterating just over the files in the path.
This kind of finder is returned by the static method path(File).
Starting with this basic finder, further finders can be added to the pipe
using the member methods
name(String) and print(PrintStream).
Created: Wed Nov 21 17:29:41 2012
| Modifier and Type | Class and Description |
|---|---|
(package private) static class |
Finder.AndFilter
One of the logical operations of filters:
Returns a filter which passes a file
iff all original filters in
Finder.AndFilter.filters do so. |
static interface |
Finder.Callable
To be implemented by java classes to apply a filter to a file
and give feedback whether this filter succeeded.
|
(package private) static class |
Finder.ExecFilter
Filter executing a shell command and passes the file received
if the shell command succeeds according to its return code.
|
(package private) static class |
Finder.ExecJavaFilter
Filter executing a java class implementing
Finder.Callable
which passes the file received
if the method Finder.Callable.call(File) succeeds
according to its return code. |
(package private) static class |
Finder.Filter
Represents a file filter.
|
(package private) static class |
Finder.NameFilter
Filters files by name.
|
(package private) static class |
Finder.NegFilter
One of the logical operations of filters:
Returns a filter which passes a file
iff the original filter
Finder.NegFilter.negFilter does not. |
(package private) static class |
Finder.OrFilter
One of the logical operations of filters:
Returns a filter which passes a file
iff at least one of the original filters in
Finder.OrFilter.filters do so. |
(package private) static class |
Finder.Primary
The most basic kind of finder:
Method
path(File) returns an instance of this. |
(package private) class |
Finder.PrintFilter
A filter passing all information and printing it on
Finder.PrintFilter.str. |
(package private) static class |
Finder.Secondary
A finder wrapping a
Finder.Filter. |
| Modifier and Type | Field and Description |
|---|---|
static Finder.Filter |
CAN_EXEC
Filter passing the file received iff it is executable.
|
static Finder.Filter |
CAN_READ
Filter passing the file received iff it is readable.
|
static Finder.Filter |
CAN_WRITE
Filter passing the file received iff it is writable.
|
static String |
EXEC_ARG
For
Finder.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.Filter |
FALSE
A filter passing no file.
|
static Finder.Filter |
IS_DIR
Filter passing the file received iff it is a regular file.
|
static Finder.Filter |
IS_FILE
Filter passing the file received iff it is a regular file.
|
static Finder.Filter |
TRUE
A filter passing all files.
|
| Modifier | Constructor and Description |
|---|---|
private |
Finder()
This is declared
private to prevent instantiation. |
| Modifier and Type | Method and Description |
|---|---|
Finder |
add(Finder.Filter filter)
Returns a finder by attachig the given filter.
|
Finder |
and(Finder.Filter[] filters)
Convenience method:
Returns a finder by attaching an and-filter.
|
Finder |
exec(String[] cmd)
Convenience method: adds an execution filter to this finder.
|
static Finder.Filter |
execFilter(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.
|
Finder |
execJava(Finder.Callable callable)
Convenience method: adds a java execution filter to this finder.
|
static Finder.Filter |
execJavaFilter(Finder.Callable callable)
Returns a filter invoking method
Finder.Callable.call(File)
of the given callable. |
abstract boolean |
hasNext()
Returns whether this Finder can emit another file.
|
static void |
main(String[] args) |
Finder |
name(String pattern)
Convenience method: adds a name filter to this finder.
|
static Finder.Filter |
nameFilter(String pattern)
Returns a filter passing a file
iff its (short) names of which
match the regular expression
pattern. |
abstract File |
next()
Returns the next file this finder can emit.
|
Finder |
not(Finder.Filter filter)
Convenience method:
Returns a finder by attaching the inverse of the given filter.
|
Finder |
or(Finder.Filter[] filters)
Convenience method:
Returns a finder by attaching an or-filter.
|
static Finder |
path(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.
|
Finder |
print(PrintStream str)
Adds a trivial filter passing all files received by this finder
printing their full names to
str. |
public static final String EXEC_ARG
Finder.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 {}.public static final Finder.Filter TRUE
-true
in the original find command.public static final Finder.Filter FALSE
-false
in the original find command.public static final Finder.Filter CAN_EXEC
-executable
in the original find command.
Do not mix up with Finder.ExecFilter.public static final Finder.Filter CAN_READ
-readable
in the original find command.public static final Finder.Filter CAN_WRITE
-writable
in the original find command.public static final Finder.Filter IS_FILE
-type f
in the original find command.public static final Finder.Filter IS_DIR
-type d
in the original find command.public static Finder path(File file)
Finder.path(file)
corresponds with find file,
except that the find command implicitly adds a print filter
as defined in print(PrintStream).
Note also that, unlike th original find,
no wildcards are supported.Finder.Primarypublic Finder print(PrintStream str)
str.public Finder add(Finder.Filter filter)
public Finder name(String pattern)
nameFilter(String)public Finder exec(String[] cmd)
Finder.Secondary
instantiated with a filter of type Finder.ExecFilterexecFilter(String[])public Finder execJava(Finder.Callable callable)
Finder.Secondary
instantiated with a filter of type Finder.ExecJavaFilterexecJavaFilter(Callable)public Finder not(Finder.Filter filter)
\! expr1
in the original find command.Finder.Filter.not()public Finder and(Finder.Filter[] filters)
\! expr1
in the original find command.and(Filter[])public Finder or(Finder.Filter[] filters)
\! expr1
in the original find command.or(Filter[])public static Finder.Filter nameFilter(String pattern)
pattern.Finder.Secondary
instantiated with a filter of type Finder.NameFilterpublic static Finder.Filter execFilter(String[] cmd)
find . -exec grep "pattern without quotes" {} \; -print.
Here, the portion specifying execution is
-exec grep "pattern without quotes" {} \;:
It starts with -exec and ends with \;.
Note that the command grep
has 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.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 instance EXEC_ARG
which 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}
Finder.ExecFilterexecJavaFilter(Callable)public static Finder.Filter execJavaFilter(Finder.Callable callable)
Finder.Callable.call(File)
of the given callable.callable - A callable defining a filter.
If parameters are required,
these must be given when instantiating the callable
or later using setter methods.Finder.ExecFilterexecFilter(String[])public abstract boolean hasNext()
next()public abstract File next()
hasNext() returns true.public static void main(String[] args)
Copyright © 2012–2018 Simuline Organization (l2r). All rights reserved.