FileDocCategorySizeDatePackage
DifferentSelector.javaAPI DocApache Ant 1.704263Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.types.selectors

DifferentSelector

public class DifferentSelector extends MappingSelector
This selector selects files against a mapped set of target files, selecting all those files which are different. Files with different lengths are deemed different automatically Files with identical timestamps are viewed as matching by default, unless you specify otherwise. Contents are compared if the lengths are the same and the timestamps are ignored or the same, except if you decide to ignore contents to gain speed.

This is a useful selector to work with programs and tasks that don't handle dependency checking properly; Even if a predecessor task always creates its output files, followup tasks can be driven off copies made with a different selector, so their dependencies are driven on the absolute state of the files, not a timestamp.

Clearly, however, bulk file comparisons is inefficient; anything that can use timestamps is to be preferred. If this selector must be used, use it over as few files as possible, perhaps following it with an <uptodate;> to keep the descendent routines conditional.

Fields Summary
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
private boolean
ignoreFileTimes
private boolean
ignoreContents
Constructors Summary
Methods Summary
protected booleanselectionTest(java.io.File srcfile, java.io.File destfile)
this test is our selection test that compared the file with the destfile

param
srcfile the source file
param
destfile the destination file
return
true if the files are different


        //if either of them is missing, they are different
        if (srcfile.exists() != destfile.exists()) {
            return true;
        }

        if (srcfile.length() != destfile.length()) {
            // different size =>different files
            return true;
        }

        if (!ignoreFileTimes) {
            //same date if dest timestamp is within granularity of the srcfile
            boolean sameDate;
            sameDate = destfile.lastModified() >= srcfile.lastModified() - granularity
                    && destfile.lastModified() <= srcfile.lastModified() + granularity;

            // different dates => different files
            if (!sameDate) {
                return true;
            }
        }
        if (!ignoreContents) {
            //here do a bulk comparison
            try {
                return !FILE_UTILS.contentEquals(srcfile, destfile);
            } catch (IOException e) {
                throw new BuildException("while comparing " + srcfile + " and "
                        + destfile, e);
            }
        } else {
            return false;
        }
    
public voidsetIgnoreContents(boolean ignoreContents)
This flag tells the selector to ignore contents

param
ignoreContents if true ignore contents
since
ant 1.6.3

        this.ignoreContents = ignoreContents;
    
public voidsetIgnoreFileTimes(boolean ignoreFileTimes)
This flag tells the selector to ignore file times in the comparison

param
ignoreFileTimes if true ignore file times



                            
        
        this.ignoreFileTimes = ignoreFileTimes;