diff --git a/src/main/org/apache/tools/ant/types/TimeComparison.java b/src/main/org/apache/tools/ant/types/TimeComparison.java new file mode 100755 index 000000000..bd7cfac57 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/TimeComparison.java @@ -0,0 +1,121 @@ +/* + * 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; + } + +} + diff --git a/src/main/org/apache/tools/ant/types/selectors/DateSelector.java b/src/main/org/apache/tools/ant/types/selectors/DateSelector.java index 8f8e8daad..4f1f608b5 100644 --- a/src/main/org/apache/tools/ant/types/selectors/DateSelector.java +++ b/src/main/org/apache/tools/ant/types/selectors/DateSelector.java @@ -24,8 +24,8 @@ import java.text.ParseException; 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 @@ public class DateSelector extends BaseExtendSelector { 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 class DateSelector extends BaseExtendSelector { 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 class DateSelector extends BaseExtendSelector { */ public void setDatetime(String dateTime) { this.dateTime = dateTime; + millis = -1; } /** @@ -144,7 +139,15 @@ public class DateSelector extends BaseExtendSelector { * @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 @@ public class DateSelector extends BaseExtendSelector { + 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 @@ public class DateSelector extends BaseExtendSelector { 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. *
*/ - 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 { } }