/* * [Traverse.java] * * Summary: Find files in given directories, and add them to the list of files to processZipsPass1. * * Copyright: (c) 2003-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 10.2 2009-04-03 tidy up code to check presence of necessary files to make it more WORA. * Just ability to add files to list, not remove. Similar code to com.mindprod.commandline.CommandLine. */ package com.mindprod.replicatorsender; import com.mindprod.filter.AllButSVNDirectoriesFilter; import com.mindprod.filter.OnlyFilesFilter; import com.mindprod.sorted.SortedArrayList; import java.io.File; import java.util.ArrayList; import static java.lang.System.*; /** * Find files in given directories, and add them to the list of files to processZipsPass1. * * @author Roedy Green, Canadian Mind Products * @version 10.2 2009-04-03 tidy up code to check presence of necessary files to make it more WORA. * Just ability to add files to list, not remove. Similar code to com.mindprod.commandline.CommandLine. * @since 2003-03-11 */ final class Traverse { /** * FilenameFilter than accepts only directories. */ private static final AllButSVNDirectoriesFilter justDirs = new AllButSVNDirectoriesFilter(); /** * FilenameFilter that accepts only files. */ private static final OnlyFilesFilter justFiles = new OnlyFilesFilter(); /** * List of files that will be processed. Objects in ArrayList are MaxiFDs. */ private final ArrayList collectedFiles; /** * base directory. All other directories specified are relative to this one. "" if none. */ private final String base; /** * Constructor * * @param expectedFiles Estimate of how many files you will eventually collect. Aim on the slightly high side. * @param base base directory. All other directories specified are relative to this one. "" if none. */ public Traverse( int expectedFiles, String base ) { collectedFiles = new ArrayList<>( expectedFiles ); this.base = base; } /** * Add a single file to the list. * * @param file File including base. */ private void addFile( File file ) { // chop off base String parent = file.getParent(); if ( parent.equals( base ) ) { parent = ""; } else { assert parent.length() > base.length() + 1 : "bad parent " + file.getParent(); // strip off base parent = parent.substring( base.length() + 1 ); } String child = file.getName(); MaxiFD fd = new MaxiFD( parent, child ); fd.setFileLength( file.length() ); long lastModified = file.lastModified(); fd.setTimestamp( lastModified ); fd.setClumping( lastModified ); collectedFiles.add( fd ); } // end addFile /** * Find all files in the given dir and add them to the list. * * @param dir Directory whose files you want added. Must be a directory. * @param recurse True if you want files in subdirs of that directory added too. */ private void addFilesInTree( File dir, boolean recurse ) { // processZipsPass1 all files in this dir first String[] allFiles = dir.list( justFiles ); for ( String allFile : allFiles ) { addFile( new File( dir, allFile ) ); } // recursively processZipsPass1 all subdirs in this directory if ( recurse ) { String[] allDirs = dir.list( justDirs ); for ( String allDir : allDirs ) { addFilesInTree( new File( dir, allDir ), recurse ); } } } // end addFilesInTree /** * Add a single file to the list. * * @param filename name of the file to add. Name relative to the base. */ public void addFile( String filename ) { final File file = new File( base, filename ); if ( !file.isFile() ) { err.println( file + " not accessible. Ignored." ); return; } addFile( file ); } // end addFile /** * Add all files in a directory, but not its subdirectories. * * @param dir Name of directory to search for files. Name relative to base. */ public void addFilesInDir( String dir ) { final File container = dir.equals( "*" ) ? new File( base ) : new File( base, dir ); if ( !container.isDirectory() ) { err.println( dir + " is not an existing directory. Ignored." ); return; } addFilesInTree( container, false ); } // end addFilesInDir /** * Add all the files in this directory tree to the list. * * @param dirTree Directory tree to search for files to add. Name relative to base. */ public void addFilesInTree( String dirTree ) { final File container = dirTree.equals( "*" ) ? new File( base ) : new File( base, dirTree ); if ( !container.isDirectory() ) { err.println( container + " is not an existing directory. Ignored." ); return; } addFilesInTree( container, true ); } // end addFilesInTree /** * Get ArrayList of all files collected. This is the master, not a copy. * * @return All files collected, in no particular order, but ready for sorting. */ public SortedArrayList getCollectedFiles() { return new SortedArrayList<>( collectedFiles ); } /** * HowToProcess many files have been found so far. * * @return count of files in the collection so far. */ public int size() { return collectedFiles.size(); } }