Class Finder

  • Direct Known Subclasses:
    Finder.Primary, Finder.Secondary

    public abstract class Finder
    extends Object
    Emulates the unix shell command 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

    Version:
    1.0
    Author:
    Ernst Reissner
    • Field Detail

      • EXEC_ARG

        public static final 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. 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 -true in the original find command.
      • FALSE

        public static final Finder.Filter FALSE
        A filter passing no file. This corresponds the test -false in 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 -executable in the original find command. Do not mix up with Finder.ExecFilter.
      • CAN_READ

        public static final Finder.Filter CAN_READ
        Filter passing the file received iff it is readable. This corresponds the test -readable in 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 -writable in 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 f in 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 d in the original find command.
    • Constructor Detail

      • Finder

        private Finder()
        This is declared private to prevent instantiation.
    • Method Detail

      • path

        public 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. The ordering is the same as in original find-command. The expression 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.
        Returns:
        an instance of Finder.Primary
      • print

        public Finder print​(PrintStream str)
        Adds a trivial filter passing all files received by this finder printing their full names to str.
      • 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.
      • not

        public Finder not​(Finder.Filter filter)
        Convenience method: Returns a finder by attaching the inverse of the given filter. This corresponds the tests \! expr1 in the original find command.
        See Also:
        Finder.Filter.not()
      • nameFilter

        public static Finder.Filter nameFilter​(String pattern)
        Returns a filter passing a file iff its (short) names of which match the regular expression pattern.
        Returns:
        an instance of Finder.Secondary instantiated with a filter of type Finder.NameFilter
      • execFilter

        public 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. 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 -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.
        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 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}

        Returns:
        a filter of type Finder.ExecFilter
        See Also:
        execJavaFilter(Callable)
      • hasNext

        public abstract boolean hasNext()
        Returns whether this Finder can emit another file.
        See Also:
        next()
      • next

        public abstract File next()
        Returns the next file this finder can emit. This does not throw an exception iff hasNext() returns true.
      • main

        public static void main​(String[] args)