com.eu.miscedautils.CommonUtils
Class Paths

java.lang.Object
  extended by com.eu.miscedautils.CommonUtils.Paths
All Implemented Interfaces:
java.lang.Iterable<java.lang.String>

public class Paths
extends java.lang.Object
implements java.lang.Iterable<java.lang.String>

Collects filesystem paths using wildcards, preserving the directory structure. Copies, deletes, and zips paths.


Constructor Summary
Paths()
          Creates an empty Paths object.
Paths(java.lang.String dir, java.util.List<java.lang.String> patterns)
          Creates a Paths object and calls glob(String, List) with the specified arguments.
Paths(java.lang.String dir, java.lang.String... patterns)
          Creates a Paths object and calls glob(String, String[]) with the specified arguments.
 
Method Summary
 void add(Paths paths)
          Adds all paths from the specified Paths object to this Paths object.
 Paths add(java.lang.String dir, java.lang.String name)
          Adds a single path to this Paths object.
 Paths addFile(java.lang.String fullPath)
          Adds a single path to this Paths object.
 Paths copyTo(java.lang.String destDir)
          Copies the files and directories to the specified directory.
 int count()
           
 boolean delete()
          Deletes all the files, directories, and any files in the directories.
 Paths dirsOnly()
          Returns a Paths object containing the paths that are directories.
 java.util.Iterator<java.io.File> fileIterator()
          Iterates over the paths as File objects.
 Paths filesOnly()
          Returns a Paths object containing the paths that are files.
 Paths flatten()
          Returns a Paths object containing the paths that are files, as if each file were selected from its parent directory.
 java.util.List<java.io.File> getFiles()
          Returns the paths as File objects.
 java.util.List<java.lang.String> getNames()
          Returns the paths' filenames.
 java.util.List<java.lang.String> getPaths()
          Returns the full paths.
 java.util.List<java.lang.String> getRelativePaths()
          Returns the portion of the path after the root directory where the path was collected.
 Paths glob(java.lang.String dir, java.util.List<java.lang.String> patterns)
          Calls glob(String, String...).
 Paths glob(java.lang.String dir, java.lang.String... patterns)
          Collects all files and directories in the specified directory matching the wildcard patterns.
 boolean isEmpty()
           
 java.util.Iterator<java.lang.String> iterator()
          Iterates over the absolute paths.
static void main(java.lang.String[] args)
           
 Paths regex(java.lang.String dir, java.lang.String... patterns)
          Collects all files and directories in the specified directory matching the regular expression patterns.
static void setDefaultGlobExcludes(java.lang.String... defaultGlobExcludes)
          Sets the exclude patterns that will be used in addition to the excludes specified for all glob searches.
 java.lang.String toString()
          Returns the absolute paths delimited by commas.
 java.lang.String toString(java.lang.String delimiter)
          Returns the absolute paths delimited by the specified character.
 void zip(java.lang.String destFile)
          Compresses the files and directories specified by the paths into a new zip file at the specified location.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Paths

public Paths()
Creates an empty Paths object.


Paths

public Paths(java.lang.String dir,
             java.lang.String... patterns)
Creates a Paths object and calls glob(String, String[]) with the specified arguments.


Paths

public Paths(java.lang.String dir,
             java.util.List<java.lang.String> patterns)
Creates a Paths object and calls glob(String, List) with the specified arguments.

Method Detail

glob

public Paths glob(java.lang.String dir,
                  java.lang.String... patterns)
Collects all files and directories in the specified directory matching the wildcard patterns.

Parameters:
dir - The directory containing the paths to collect. If it does not exist, no paths are collected. If null, "." is assumed.
patterns - The wildcard patterns of the paths to collect or exclude. Patterns may optionally contain wildcards represented by asterisks and question marks. If empty or omitted then the dir parameter is split on the "|" character, the first element is used as the directory and remaining are used as the patterns. If null, ** is assumed (collects all paths).

A single question mark (?) matches any single character. Eg, something? collects any path that is named "something" plus any character.

A single asterisk (*) matches any characters up to the next slash (/). Eg, *\*\something* collects any path that has two directories of any name, then a file or directory that starts with the name "something".

A double asterisk (**) matches any characters. Eg, **\something\** collects any path that contains a directory named "something".

A pattern starting with an exclamation point (!) causes paths matched by the pattern to be excluded, even if other patterns would select the paths.

glob

public Paths glob(java.lang.String dir,
                  java.util.List<java.lang.String> patterns)
Calls glob(String, String...).


regex

public Paths regex(java.lang.String dir,
                   java.lang.String... patterns)
Collects all files and directories in the specified directory matching the regular expression patterns. This method is much slower than glob(String, String...) because every file and directory under the specified directory must be inspected.

Parameters:
dir - The directory containing the paths to collect. If it does not exist, no paths are collected.
patterns - The regular expression patterns of the paths to collect or exclude. If empty or omitted then the dir parameter is split on the "|" character, the first element is used as the directory and remaining are used as the patterns. If null, ** is assumed (collects all paths).

A pattern starting with an exclamation point (!) causes paths matched by the pattern to be excluded, even if other patterns would select the paths.

copyTo

public Paths copyTo(java.lang.String destDir)
             throws java.io.IOException
Copies the files and directories to the specified directory.

Returns:
A paths object containing the paths of the new files.
Throws:
java.io.IOException

delete

public boolean delete()
Deletes all the files, directories, and any files in the directories.

Returns:
False if any file could not be deleted.

zip

public void zip(java.lang.String destFile)
         throws java.io.IOException
Compresses the files and directories specified by the paths into a new zip file at the specified location. If there are no paths or all the paths are directories, no zip file will be created.

Throws:
java.io.IOException

count

public int count()

isEmpty

public boolean isEmpty()

toString

public java.lang.String toString(java.lang.String delimiter)
Returns the absolute paths delimited by the specified character.


toString

public java.lang.String toString()
Returns the absolute paths delimited by commas.

Overrides:
toString in class java.lang.Object

flatten

public Paths flatten()
Returns a Paths object containing the paths that are files, as if each file were selected from its parent directory.


filesOnly

public Paths filesOnly()
Returns a Paths object containing the paths that are files.


dirsOnly

public Paths dirsOnly()
Returns a Paths object containing the paths that are directories.


getFiles

public java.util.List<java.io.File> getFiles()
Returns the paths as File objects.


getRelativePaths

public java.util.List<java.lang.String> getRelativePaths()
Returns the portion of the path after the root directory where the path was collected.


getPaths

public java.util.List<java.lang.String> getPaths()
Returns the full paths.


getNames

public java.util.List<java.lang.String> getNames()
Returns the paths' filenames.


addFile

public Paths addFile(java.lang.String fullPath)
Adds a single path to this Paths object.


add

public Paths add(java.lang.String dir,
                 java.lang.String name)
Adds a single path to this Paths object.


add

public void add(Paths paths)
Adds all paths from the specified Paths object to this Paths object.


iterator

public java.util.Iterator<java.lang.String> iterator()
Iterates over the absolute paths. The iterator supports the remove method.

Specified by:
iterator in interface java.lang.Iterable<java.lang.String>

fileIterator

public java.util.Iterator<java.io.File> fileIterator()
Iterates over the paths as File objects. The iterator supports the remove method.


setDefaultGlobExcludes

public static void setDefaultGlobExcludes(java.lang.String... defaultGlobExcludes)
Sets the exclude patterns that will be used in addition to the excludes specified for all glob searches.


main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Throws:
java.io.IOException