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

SizeSelector

public class SizeSelector extends BaseExtendSelector
Selector that filters files based on their size.
since
1.5

Fields Summary
public static final String
SIZE_KEY
Used for parameterized custom selector
public static final String
UNITS_KEY
Used for parameterized custom selector
public static final String
WHEN_KEY
Used for parameterized custom selector
private long
size
private long
multiplier
private long
sizelimit
private org.apache.tools.ant.types.Comparison
when
Constructors Summary
public SizeSelector()
Creates a new SizeSelector instance.


              
      
    
Methods Summary
public booleanisSelected(java.io.File basedir, java.lang.String filename, java.io.File file)
The heart of the matter. This is where the selector gets to decide on the inclusion of a file in a particular fileset.

param
basedir A java.io.File object for the base directory.
param
filename The name of the file to check.
param
file A File object for this filename.
return
whether the file should be selected or not.


        // throw BuildException on error
        validate();

        // Directory size never selected for
        if (file.isDirectory()) {
            return true;
        }
        long diff = file.length() - sizelimit;
        return when.evaluate(diff == 0 ? 0 : (int) (diff / Math.abs(diff)));
    
public voidsetParameters(org.apache.tools.ant.types.Parameter[] parameters)
When using this as a custom selector, this method will be called. It translates each parameter into the appropriate setXXX() call.

param
parameters the complete set of parameters for this selector.

        super.setParameters(parameters);
        if (parameters != null) {
            for (int i = 0; i < parameters.length; i++) {
                String paramname = parameters[i].getName();
                if (SIZE_KEY.equalsIgnoreCase(paramname)) {
                    try {
                        setValue(new Long(parameters[i].getValue()
                        ).longValue());
                    } catch (NumberFormatException nfe) {
                        setError("Invalid size setting "
                                + parameters[i].getValue());
                    }
                } else if (UNITS_KEY.equalsIgnoreCase(paramname)) {
                    ByteUnits units = new ByteUnits();
                    units.setValue(parameters[i].getValue());
                    setUnits(units);
                } else if (WHEN_KEY.equalsIgnoreCase(paramname)) {
                    SizeComparisons scmp = new SizeComparisons();
                    scmp.setValue(parameters[i].getValue());
                    setWhen(scmp);
                } else {
                    setError("Invalid parameter " + paramname);
                }
            }
        }
    
public voidsetUnits(org.apache.tools.ant.types.selectors.SizeSelector$ByteUnits units)
Sets the units to use for the comparison. This is a little complicated because common usage has created standards that play havoc with capitalization rules. Thus, some people will use "K" for indicating 1000's, when the SI standard calls for "k". Others have tried to introduce "K" as a multiple of 1024, but that falls down when you reach "M", since "m" is already defined as 0.001.

To get around this complexity, a number of standards bodies have proposed the 2^10 standard, and at least one has adopted it. But we are still left with a populace that isn't clear on how capitalization should work.

We therefore ignore capitalization as much as possible. Completely mixed case is not possible, but all upper and lower forms are accepted for all long and short forms. Since we have no need to work with the 0.001 case, this practice works here.

This function translates all the long and short forms that a unit prefix can occur in and translates them into a single multiplier.

param
units The units to compare the size to, using an EnumeratedAttribute.

        int i = units.getIndex();
        multiplier = 0;
        if (i > -1 && i < 4) {
            multiplier = 1000;
        } else if (i > 3 && i < 9) {
            multiplier = 1024;
        } else if (i > 8 && i < 13) {
            multiplier = 1000000;
        } else if (i > 12 && i < 18) {
            multiplier = 1048576;
        } else if (i > 17 && i < 22) {
            multiplier = 1000000000L;
        } else if (i > 21 && i < 27) {
            multiplier = 1073741824L;
        } else if (i > 26 && i < 31) {
            multiplier = 1000000000000L;
        } else if (i > 30 && i < 36) {
            multiplier = 1099511627776L;
        }
        if (multiplier > 0 && size > -1) {
            sizelimit = size * multiplier;
        }
    
public voidsetValue(long size)
A size selector needs to know what size to base its selecting on. This will be further modified by the multiplier to get an actual size limit.

param
size the size to select against expressed in units.

        this.size = size;
        if (multiplier != 0 && size > -1) {
            sizelimit = size * multiplier;
        }
    
public voidsetWhen(org.apache.tools.ant.types.selectors.SizeSelector$SizeComparisons when)
This specifies when the file should be selected, whether it be when the file matches a particular size, when it is smaller, or whether it is larger.

param
when The comparison to perform, an EnumeratedAttribute.

        this.when = when;
    
public java.lang.StringtoString()
Returns a String object representing the specified SizeSelector. This is "{sizeselector value: " + <"compare", "less", "more", "equal"> + "}".

return
a string describing this object

        StringBuffer buf = new StringBuffer("{sizeselector value: ");
        buf.append(sizelimit);
        buf.append("compare: ").append(when.getValue());
        buf.append("}");
        return buf.toString();
    
public voidverifySettings()

Checks to make sure all settings are kosher. In this case, it means that the size attribute has been set (to a positive value), that the multiplier has a valid setting, and that the size limit is valid. Since the latter is a calculated value, this can only fail due to a programming error.

If a problem is detected, the setError() method is called.

        if (size < 0) {
            setError("The value attribute is required, and must be positive");
        } else if (multiplier < 1) {
            setError("Invalid Units supplied, must be K,Ki,M,Mi,G,Gi,T,or Ti");
        } else if (sizelimit < 0) {
            setError("Internal error: Code is not setting sizelimit correctly");
        }