ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject cvs commit: ant/src/main/org/apache/tools/ant/types/selectors DateSelector.java
Date Mon, 23 May 2005 19:31:49 GMT
mbenson     2005/05/23 12:31:49

  Modified:    src/main/org/apache/tools/ant/types/selectors
                        DateSelector.java
  Added:       src/main/org/apache/tools/ant/types TimeComparison.java
  Log:
  add TimeComparison ExtendedAttribute; retrofit DateSelector
  
  Revision  Changes    Path
  1.1                  ant/src/main/org/apache/tools/ant/types/TimeComparison.java
  
  Index: TimeComparison.java
  ===================================================================
  /*
   * Copyright 2005 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   *
   */
  package org.apache.tools.ant.types;
  
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.util.FileUtils;
  
  /**
   * EnumeratedAttribute for time comparisons.  Accepts values
   * "before", "after", "equal".
   * @since Ant 1.7
   */
  public class TimeComparison extends EnumeratedAttribute {
      private static final String[] VALUES
      = new String[] {"before", "after", "equal"};
  
      private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  
      /** Before Comparison. */
      public static final TimeComparison BEFORE = new TimeComparison("before");
  
      /** After Comparison. */
      public static final TimeComparison AFTER = new TimeComparison("after");
  
      /** Equal Comparison. */
      public static final TimeComparison EQUAL = new TimeComparison("equal");
  
      /**
       * Default constructor.
       */
      public TimeComparison() {
      }
  
      /**
       * Construct a new TimeComparison with the specified value.
       * @param value the EnumeratedAttribute value.
       */
      public TimeComparison(String value) {
          setValue(value);
      }
  
      /**
       * Return the possible values.
       * @return String[] of EnumeratedAttribute values.
       */
      public String[] getValues() {
          return VALUES;
      }
  
      /**
       * Evaluate two times against this TimeComparison.
       * @param t1 the first time to compare.
       * @param t2 the second time to compare.
       * @return true if the comparison result fell within the parameters of this TimeComparison.
       */
      public boolean evaluate(long t1, long t2) {
          return evaluate(t1, t2, FILE_UTILS.getFileTimestampGranularity());
      }
  
      /**
       * Evaluate two times against this TimeComparison.
       * @param t1 the first time to compare.
       * @param t2 the second time to compare.
       * @param g the timestamp granularity.
       * @return true if the comparison result fell within the parameters of this TimeComparison.
       */
      public boolean evaluate(long t1, long t2, long g) {
          int cmp = getIndex();
          if (cmp == -1) {
              throw new BuildException("TimeComparison value not set.");
          }
          if (cmp == 0) {
              return t1 - g < t2;
          }
          if (cmp == 1) {
              return t1 + g > t2;
          }
          return Math.abs(t1 - t2) <= g;
      }
  
      /**
       * Compare two times.
       * @param t1 the first time to compare.
       * @param t2 the second time to compare.
       * @return a negative integer, a positive integer, or zero as t1 is
       *         before, after, or equal to t2 accounting for the default granularity.
       */
      public static int compare(long t1, long t2) {
          return compare(t1, t2, FILE_UTILS.getFileTimestampGranularity());
      }
  
      /**
       * Compare two times.
       * @param t1 the first time to compare.
       * @param t2 the second time to compare.
       * @param g the timestamp granularity.
       * @return a negative integer, a positive integer, or zero as t1 is
       *         before, after, or equal to t2 accounting for the specified granularity.
       */
      public static int compare(long t1, long t2, long g) {
          long diff = t1 - t2;
          long abs = Math.abs(diff);
          return abs > Math.abs(g) ? (int) (diff / abs) : 0;
      }
  
  }
  
  
  
  
  1.19      +18 -31    ant/src/main/org/apache/tools/ant/types/selectors/DateSelector.java
  
  Index: DateSelector.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/selectors/DateSelector.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- DateSelector.java	6 Jan 2005 12:05:09 -0000	1.18
  +++ DateSelector.java	23 May 2005 19:31:49 -0000	1.19
  @@ -24,8 +24,8 @@
   import java.util.Locale;
   
   import org.apache.tools.ant.Project;
  -import org.apache.tools.ant.types.EnumeratedAttribute;
   import org.apache.tools.ant.types.Parameter;
  +import org.apache.tools.ant.types.TimeComparison;
   import org.apache.tools.ant.util.FileUtils;
   
   /**
  @@ -42,8 +42,9 @@
       private String dateTime = null;
       private boolean includeDirs = false;
       private long granularity = 0;
  -    private int cmp = 2;
       private String pattern;
  +    private TimeComparison when = TimeComparison.EQUAL;
  +
       /** Key to used for parameterized custom selector */
       public static final String MILLIS_KEY = "millis";
       /** Key to used for parameterized custom selector */
  @@ -71,14 +72,7 @@
       public String toString() {
           StringBuffer buf = new StringBuffer("{dateselector date: ");
           buf.append(dateTime);
  -        buf.append(" compare: ");
  -        if (cmp == 0) {
  -            buf.append("before");
  -        } else if (cmp == 1) {
  -            buf.append("after");
  -        } else {
  -            buf.append("equal");
  -        }
  +        buf.append(" compare: ").append(when.getValue());
           buf.append(" granularity: ");
           buf.append(granularity);
           if (pattern != null) {
  @@ -117,6 +111,7 @@
        */
       public void setDatetime(String dateTime) {
           this.dateTime = dateTime;
  +        millis = -1;
       }
   
       /**
  @@ -144,7 +139,15 @@
        * @param tcmp The comparison to perform, an EnumeratedAttribute.
        */
       public void setWhen(TimeComparisons tcmp) {
  -        this.cmp = tcmp.getIndex();
  +        setWhen((TimeComparison) tcmp);
  +    }
  +
  +    /**
  +     * Set the comparison type.
  +     * @param t TimeComparison object.
  +     */
  +    public void setWhen(TimeComparison t) {
  +        when = t;
       }
   
       /**
  @@ -188,9 +191,7 @@
                               + parameters[i].getValue());
                       }
                   } else if (WHEN_KEY.equalsIgnoreCase(paramname)) {
  -                    TimeComparisons tcmp = new TimeComparisons();
  -                    tcmp.setValue(parameters[i].getValue());
  -                    setWhen(tcmp);
  +                    setWhen(new TimeComparison(parameters[i].getValue()));
                   } else if (PATTERN_KEY.equalsIgnoreCase(paramname)) {
                       setPattern(parameters[i].getValue());
                   } else {
  @@ -244,29 +245,15 @@
   
           validate();
   
  -        if (file.isDirectory() && (!includeDirs)) {
  -            return true;
  -        }
  -        if (cmp == 0) {
  -            return ((file.lastModified() - granularity) < millis);
  -        } else if (cmp == 1) {
  -            return ((file.lastModified() + granularity) > millis);
  -        } else {
  -            return (Math.abs(file.lastModified() - millis) <= granularity);
  -        }
  +        return (file.isDirectory() && !includeDirs)
  +            || when.evaluate(file.lastModified(), millis, granularity);
       }
   
       /**
        * Enumerated attribute with the values for time comparison.
        * <p>
        */
  -    public static class TimeComparisons extends EnumeratedAttribute {
  -        /**
  -         * @return the values as an array of strings
  -         */
  -        public String[] getValues() {
  -            return new String[]{"before", "after", "equal"};
  -        }
  +    public static class TimeComparisons extends TimeComparison {
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message