de.unkrig.commons.file.fileprocessing
Class FileProcessings

java.lang.Object
  extended by de.unkrig.commons.file.fileprocessing.FileProcessings

public final class FileProcessings
extends java.lang.Object

Various file processing utility methods.


Nested Class Summary
static interface FileProcessings.DirectoryCombiner<T>
          Transforms the return values of the invocations of directoryMemberProcessor for each member.
 
Method Summary
static
<T> FileProcessor<T>
archiveCompressedAndNormalFileProcessor(Predicate<java.lang.String> lookIntoFormat, ContentsProcessor<T> archiveContentsProcessor, ContentsProcessings.ArchiveCombiner<T> archiveEntryCombiner, ContentsProcessor<T> compressedContentsProcessor, ContentsProcessor<T> normalContentsProcessor, ExceptionHandler<java.io.IOException> exceptionHandler)
          Returns a FileProcessor which processes files by feeding their contents either to the archiveContentsProcessor, the compressedContentsProcessor or the normalContentsProcessor}.
static
<T> FileProcessor<T>
directoryProcessor(Predicate<java.lang.String> pathPredicate, FileProcessor<T> regularFileProcessor, java.util.Comparator<java.lang.Object> directoryMemberNameComparator, FileProcessor<T> directoryMemberProcessor, FileProcessings.DirectoryCombiner<T> directoryCombiner, SquadExecutor<java.lang.Void> squadExecutor, ExceptionHandler<java.io.IOException> exceptionHandler)
          Returns a FileProcessor which processes directories and regular files.
static
<T> FileProcessor<T>
directoryTreeProcessor(Predicate<java.lang.String> pathPredicate, FileProcessor<T> regularFileProcessor, java.util.Comparator<java.lang.Object> directoryMemberNameComparator, FileProcessings.DirectoryCombiner<T> directoryCombiner, SquadExecutor<java.lang.Void> squadExecutor, ExceptionHandler<java.io.IOException> exceptionHandler)
          Returns a FileProcessor which processes directories and regular files exactly like the FileProcessor returned by directoryProcessor(Predicate, FileProcessor, Comparator, FileProcessor, DirectoryCombiner, SquadExecutor, ExceptionHandler), except that it processes directory members recursively.
static
<T> FileProcessor<T>
nop()
           
static
<T> FileProcessings.DirectoryCombiner<T>
nopDirectoryCombiner()
          A FileProcessings.DirectoryCombiner which ignores the combinables and returns null.
static
<T> void
process(java.util.List<java.io.File> files, FileProcessor<T> fileProcessor)
           
static
<T> void
process(java.util.List<java.io.File> files, FileProcessor<T> fileProcessor, ExceptionHandler<java.io.IOException> exceptionHandler)
          Invokes the fileProcessor for each of the files.
static
<T> FileProcessor<T>
recursiveCompressedAndArchiveFileProcessor(Predicate<java.lang.String> lookIntoFormat, ContentsProcessings.ArchiveCombiner<T> archiveEntryCombiner, ContentsProcessor<T> delegate, ExceptionHandler<java.io.IOException> exceptionHandler)
          Returns a FileProcessor which processes files by feeding their contents through the delegate (just like the FileContentsProcessor), but automagically detects various archive and compression formats (also nested) and processes the entries of the archive and the uncompressed contents instead of the "raw" contents.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

nop

public static <T> FileProcessor<T> nop()
Returns:
A FileProcessor that does nothing.

process

public static <T> void process(java.util.List<java.io.File> files,
                               FileProcessor<T> fileProcessor)
                    throws java.io.IOException
Throws:
java.io.IOException
See Also:
process(List, FileProcessor, ExceptionHandler)

process

public static <T> void process(java.util.List<java.io.File> files,
                               FileProcessor<T> fileProcessor,
                               ExceptionHandler<java.io.IOException> exceptionHandler)
                    throws java.io.IOException
Invokes the fileProcessor for each of the files.

Parameters:
exceptionHandler - Called if the processing of one of the inputFiles throws an IOException or a RuntimeException
Throws:
java.io.IOException

directoryTreeProcessor

public static <T> FileProcessor<T> directoryTreeProcessor(Predicate<java.lang.String> pathPredicate,
                                                          FileProcessor<T> regularFileProcessor,
                                                          @Nullable
                                                          java.util.Comparator<java.lang.Object> directoryMemberNameComparator,
                                                          FileProcessings.DirectoryCombiner<T> directoryCombiner,
                                                          SquadExecutor<java.lang.Void> squadExecutor,
                                                          ExceptionHandler<java.io.IOException> exceptionHandler)
Returns a FileProcessor which processes directories and regular files exactly like the FileProcessor returned by directoryProcessor(Predicate, FileProcessor, Comparator, FileProcessor, DirectoryCombiner, SquadExecutor, ExceptionHandler), except that it processes directory members recursively.

Parameters:
directoryMemberNameComparator - The comparator used to sort a directory's members; a null value means to NOT sort the members, i.e. leave them in their 'natural' order as File.list() returns them
See Also:
directoryProcessor(Predicate, FileProcessor, Comparator, FileProcessor, DirectoryCombiner, SquadExecutor, ExceptionHandler)

directoryProcessor

public static <T> FileProcessor<T> directoryProcessor(Predicate<java.lang.String> pathPredicate,
                                                      FileProcessor<T> regularFileProcessor,
                                                      @Nullable
                                                      java.util.Comparator<java.lang.Object> directoryMemberNameComparator,
                                                      FileProcessor<T> directoryMemberProcessor,
                                                      FileProcessings.DirectoryCombiner<T> directoryCombiner,
                                                      SquadExecutor<java.lang.Void> squadExecutor,
                                                      ExceptionHandler<java.io.IOException> exceptionHandler)
Returns a FileProcessor which processes directories and regular files.

Its behavior is as follows:

Notice that the pathPredicate is only used to avoid unnecessary directory scans; apart from that the regularFileProcessor and the directoryMemberProcessor are called for any file without further ado.

If you use Void for <T>, then nopDirectoryCombiner() is the right directoryCombiner.

Type Parameters:
T - The return type of all FileProcessor.process(String, File) methods
Parameters:
directoryMemberNameComparator - The comparator used to sort a directory's members; a null value means to NOT sort the members, i.e. leave them in their 'natural' order as File.list() returns them

nopDirectoryCombiner

public static <T> FileProcessings.DirectoryCombiner<T> nopDirectoryCombiner()
A FileProcessings.DirectoryCombiner which ignores the combinables and returns null.


archiveCompressedAndNormalFileProcessor

public static <T> FileProcessor<T> archiveCompressedAndNormalFileProcessor(Predicate<java.lang.String> lookIntoFormat,
                                                                           ContentsProcessor<T> archiveContentsProcessor,
                                                                           ContentsProcessings.ArchiveCombiner<T> archiveEntryCombiner,
                                                                           ContentsProcessor<T> compressedContentsProcessor,
                                                                           ContentsProcessor<T> normalContentsProcessor,
                                                                           ExceptionHandler<java.io.IOException> exceptionHandler)
Returns a FileProcessor which processes files by feeding their contents either to the archiveContentsProcessor, the compressedContentsProcessor or the normalContentsProcessor}.

Archive files and compressed files are introspected iff lookIntoFormat evaluates true for "format:path".

Parameters:
lookIntoFormat - See CompressUtil.processFile(String, File, Predicate, ArchiveHandler, CompressorHandler, NormalContentsHandler)

recursiveCompressedAndArchiveFileProcessor

public static <T> FileProcessor<T> recursiveCompressedAndArchiveFileProcessor(Predicate<java.lang.String> lookIntoFormat,
                                                                              ContentsProcessings.ArchiveCombiner<T> archiveEntryCombiner,
                                                                              ContentsProcessor<T> delegate,
                                                                              ExceptionHandler<java.io.IOException> exceptionHandler)
Returns a FileProcessor which processes files by feeding their contents through the delegate (just like the FileContentsProcessor), but automagically detects various archive and compression formats (also nested) and processes the entries of the archive and the uncompressed contents instead of the "raw" contents.

Archive files/entries and compressed files/entries are introspected iff lookIntoFormat evaluates true for "format:path".