Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 312   Methods: 10
NCLOC: 144   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
SizeSelector.java 69.6% 74.6% 90% 74%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.types.selectors;
 56   
 
 57   
 import java.io.File;
 58   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 59   
 import org.apache.tools.ant.types.Parameter;
 60   
 
 61   
 /**
 62   
  * Selector that filters files based on their size.
 63   
  *
 64   
  * @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
 65   
  * @since 1.5
 66   
  */
 67   
 public class SizeSelector extends BaseExtendSelector {
 68   
 
 69   
     private long size = -1;
 70   
     private long multiplier = 1;
 71   
     private long sizelimit = -1;
 72   
     private int cmp = 2;
 73   
     public final static String SIZE_KEY = "value";
 74   
     public final static String UNITS_KEY = "units";
 75   
     public final static String WHEN_KEY = "when";
 76   
 
 77  13
     public SizeSelector() {
 78   
     }
 79   
 
 80  0
     public String toString() {
 81  0
         StringBuffer buf = new StringBuffer("{sizeselector value: ");
 82  0
         buf.append(sizelimit);
 83  0
         buf.append("compare: ");
 84  0
         if (cmp == 0) {
 85  0
             buf.append("less");
 86   
         }
 87  0
         else if (cmp == 1) {
 88  0
             buf.append("more");
 89   
         } else {
 90  0
             buf.append("equal");
 91   
         }
 92  0
         buf.append("}");
 93  0
         return buf.toString();
 94   
     }
 95   
 
 96   
     /**
 97   
      * A size selector needs to know what size to base its selecting on.
 98   
      * This will be further modified by the multiplier to get an
 99   
      * actual size limit.
 100   
      *
 101   
      * @param size the size to select against expressed in units
 102   
      */
 103  9
     public void setValue(long size) {
 104  9
         this.size = size;
 105  9
         if ((multiplier != 0) && (size > -1)) {
 106  8
             sizelimit = size * multiplier;
 107   
         }
 108   
     }
 109   
 
 110   
     /**
 111   
      * Sets the units to use for the comparison. This is a little
 112   
      * complicated because common usage has created standards that
 113   
      * play havoc with capitalization rules. Thus, some people will
 114   
      * use "K" for indicating 1000's, when the SI standard calls for
 115   
      * "k". Others have tried to introduce "K" as a multiple of 1024,
 116   
      * but that falls down when you reach "M", since "m" is already
 117   
      * defined as 0.001.
 118   
      * <p>
 119   
      * To get around this complexity, a number of standards bodies
 120   
      * have proposed the 2^10 standard, and at least one has adopted
 121   
      * it. But we are still left with a populace that isn't clear on
 122   
      * how capitalization should work.
 123   
      * <p>
 124   
      * We therefore ignore capitalization as much as possible.
 125   
      * Completely mixed case is not possible, but all upper and lower
 126   
      * forms are accepted for all long and short forms. Since we have
 127   
      * no need to work with the 0.001 case, this practice works here.
 128   
      * <p>
 129   
      * This function translates all the long and short forms that a
 130   
      * unit prefix can occur in and translates them into a single
 131   
      * multiplier.
 132   
      *
 133   
      * @param units The units to compare the size to, using an
 134   
      *        EnumeratedAttribute
 135   
      */
 136  4
     public void setUnits(ByteUnits units) {
 137  4
         int i = units.getIndex();
 138  4
         multiplier = 0;
 139  4
         if ((i > -1) && (i < 4)) {
 140  1
             multiplier = 1000;
 141   
         }
 142  3
         else if ((i > 3) && (i < 9)) {
 143  2
             multiplier = 1024;
 144   
         }
 145  1
         else if ((i > 8) && (i < 13)) {
 146  0
             multiplier = 1000000;
 147   
         }
 148  1
         else if ((i > 12) && (i < 18)) {
 149  0
             multiplier = 1048576;
 150   
         }
 151  1
         else if ((i > 17) && (i < 22)) {
 152  0
             multiplier = 1000000000L;
 153   
         }
 154  1
         else if ((i > 21) && (i < 27)) {
 155  0
             multiplier = 1073741824L;
 156   
         }
 157  1
         else if ((i > 26) && (i < 31)) {
 158  0
             multiplier = 1000000000000L;
 159   
         }
 160  1
         else if ((i > 30) && (i < 36)) {
 161  1
             multiplier = 1099511627776L;
 162   
         }
 163  4
         if ((multiplier > 0) && (size > -1)) {
 164  4
             sizelimit = size * multiplier;
 165   
         }
 166   
     }
 167   
 
 168   
     /**
 169   
      * This specifies when the file should be selected, whether it be
 170   
      * when the file matches a particular size, when it is smaller,
 171   
      * or whether it is larger.
 172   
      *
 173   
      * @param cmp The comparison to perform, an EnumeratedAttribute
 174   
      */
 175  7
     public void setWhen(SizeComparisons cmp) {
 176  7
         this.cmp = cmp.getIndex();
 177   
     }
 178   
 
 179   
     /**
 180   
      * When using this as a custom selector, this method will be called.
 181   
      * It translates each parameter into the appropriate setXXX() call.
 182   
      *
 183   
      * @param parameters the complete set of parameters for this selector
 184   
      */
 185  4
     public void setParameters(Parameter[] parameters) {
 186  4
         super.setParameters(parameters);
 187  4
         if (parameters != null) {
 188  4
             for (int i = 0; i < parameters.length; i++) {
 189  7
                 String paramname = parameters[i].getName();
 190  7
                 if (SIZE_KEY.equalsIgnoreCase(paramname)) {
 191  3
                     try {
 192  3
                         setValue(new Long(parameters[i].getValue()
 193   
                                 ).longValue());
 194   
                     } catch (NumberFormatException nfe) {
 195  1
                         setError("Invalid size setting "
 196   
                             + parameters[i].getValue());
 197   
                     }
 198   
                 }
 199  4
                 else if (UNITS_KEY.equalsIgnoreCase(paramname)) {
 200  2
                     ByteUnits units = new ByteUnits();
 201  2
                     units.setValue(parameters[i].getValue());
 202  1
                     setUnits(units);
 203   
                 }
 204  2
                 else if (WHEN_KEY.equalsIgnoreCase(paramname)) {
 205  1
                     SizeComparisons cmp = new SizeComparisons();
 206  1
                     cmp.setValue(parameters[i].getValue());
 207  1
                     setWhen(cmp);
 208   
                 }
 209   
                 else {
 210  1
                     setError("Invalid parameter " + paramname);
 211   
                 }
 212   
             }
 213   
         }
 214   
     }
 215   
 
 216   
     /**
 217   
      * <p>Checks to make sure all settings are kosher. In this case, it
 218   
      * means that the size attribute has been set (to a positive value),
 219   
      * that the multiplier has a valid setting, and that the size limit
 220   
      * is valid. Since the latter is a calculated value, this can only
 221   
      * fail due to a programming error.
 222   
      * </p>
 223   
      * <p>If a problem is detected, the setError() method is called.
 224   
      * </p>
 225   
      */
 226  86
     public void verifySettings() {
 227  86
         if (size < 0) {
 228  2
             setError("The value attribute is required, and must be positive");
 229   
         }
 230  84
         else if (multiplier < 1) {
 231  0
             setError("Invalid Units supplied, must be K,Ki,M,Mi,G,Gi,T,or Ti");
 232   
         }
 233  84
         else if (sizelimit < 0) {
 234  0
             setError("Internal error: Code is not setting sizelimit correctly");
 235   
         }
 236   
     }
 237   
 
 238   
     /**
 239   
      * The heart of the matter. This is where the selector gets to decide
 240   
      * on the inclusion of a file in a particular fileset.
 241   
      *
 242   
      * @param basedir A java.io.File object for the base directory
 243   
      * @param filename The name of the file to check
 244   
      * @param file A File object for this filename
 245   
      * @return whether the file should be selected or not
 246   
      */
 247  89
     public boolean isSelected(File basedir, String filename, File file) {
 248   
 
 249   
         // throw BuildException on error
 250  89
         validate();
 251   
 
 252   
         // Directory size never selected for
 253  84
         if (file.isDirectory()) {
 254  14
             return true;
 255   
         }
 256  70
         if (cmp == 0) {
 257  10
             return (file.length() < sizelimit);
 258   
         }
 259  60
         else if (cmp == 1) {
 260  50
             return (file.length() > sizelimit);
 261   
         }
 262   
         else {
 263  10
             return (file.length() == sizelimit);
 264   
         }
 265   
     }
 266   
 
 267   
 
 268   
 
 269   
     /**
 270   
      * Enumerated attribute with the values for units.
 271   
      * <p>
 272   
      * This treats the standard SI units as representing powers of ten,
 273   
      * as they should. If you want the powers of 2 that approximate
 274   
      * the SI units, use the first two characters followed by a
 275   
      * <code>bi</code>. So 1024 (2^10) becomes <code>kibi</code>,
 276   
      * 1048576 (2^20) becomes <code>mebi</code>, 1073741824 (2^30)
 277   
      * becomes <code>gibi</code>, and so on. The symbols are also
 278   
      * accepted, and these are the first letter capitalized followed
 279   
      * by an <code>i</code>. <code>Ki</code>, <code>Mi</code>,
 280   
      * <code>Gi</code>, and so on. Capitalization variations on these
 281   
      * are also accepted.
 282   
      * <p>
 283   
      * This binary prefix system is approved by the IEC and appears on
 284   
      * its way for approval by other agencies, but it is not an SI
 285   
      * standard. It disambiguates things for us, though.
 286   
      */
 287   
     public static class ByteUnits extends EnumeratedAttribute {
 288  5
         public String[] getValues() {
 289  5
             return new String[] {"K", "k", "kilo", "KILO",
 290   
                                  "Ki", "KI", "ki", "kibi", "KIBI",
 291   
                                  "M", "m", "mega", "MEGA",
 292   
                                  "Mi", "MI", "mi", "mebi", "MEBI",
 293   
                                  "G", "g", "giga", "GIGA",
 294   
                                  "Gi", "GI", "gi", "gibi", "GIBI",
 295   
                                  "T", "t", "tera", "TERA",
 296   
             /* You wish! */      "Ti", "TI", "ti", "tebi", "TEBI"
 297   
                                  };
 298   
         }
 299   
     }
 300   
 
 301   
     /**
 302   
      * Enumerated attribute with the values for size comparison.
 303   
      */
 304   
     public static class SizeComparisons extends EnumeratedAttribute {
 305  4
         public String[] getValues() {
 306  4
             return new String[] {"less", "more", "equal"};
 307   
         }
 308   
     }
 309   
 
 310   
 }
 311   
 
 312