001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types;
019:
020: import org.apache.tools.ant.BuildException;
021: import org.apache.tools.ant.util.FileUtils;
022:
023: /**
024: * EnumeratedAttribute for time comparisons. Accepts values
025: * "before", "after", "equal".
026: * @since Ant 1.7
027: */
028: public class TimeComparison extends EnumeratedAttribute {
029: private static final String[] VALUES = new String[] { "before",
030: "after", "equal" };
031:
032: private static final FileUtils FILE_UTILS = FileUtils
033: .getFileUtils();
034:
035: /** Before Comparison. */
036: public static final TimeComparison BEFORE = new TimeComparison(
037: "before");
038:
039: /** After Comparison. */
040: public static final TimeComparison AFTER = new TimeComparison(
041: "after");
042:
043: /** Equal Comparison. */
044: public static final TimeComparison EQUAL = new TimeComparison(
045: "equal");
046:
047: /**
048: * Default constructor.
049: */
050: public TimeComparison() {
051: }
052:
053: /**
054: * Construct a new TimeComparison with the specified value.
055: * @param value the EnumeratedAttribute value.
056: */
057: public TimeComparison(String value) {
058: setValue(value);
059: }
060:
061: /**
062: * Return the possible values.
063: * @return String[] of EnumeratedAttribute values.
064: */
065: public String[] getValues() {
066: return VALUES;
067: }
068:
069: /**
070: * Evaluate two times against this TimeComparison.
071: * @param t1 the first time to compare.
072: * @param t2 the second time to compare.
073: * @return true if the comparison result fell within the parameters of this TimeComparison.
074: */
075: public boolean evaluate(long t1, long t2) {
076: return evaluate(t1, t2, FILE_UTILS
077: .getFileTimestampGranularity());
078: }
079:
080: /**
081: * Evaluate two times against this TimeComparison.
082: * @param t1 the first time to compare.
083: * @param t2 the second time to compare.
084: * @param g the timestamp granularity.
085: * @return true if the comparison result fell within the parameters of this TimeComparison.
086: */
087: public boolean evaluate(long t1, long t2, long g) {
088: int cmp = getIndex();
089: if (cmp == -1) {
090: throw new BuildException("TimeComparison value not set.");
091: }
092: if (cmp == 0) {
093: return t1 - g < t2;
094: }
095: if (cmp == 1) {
096: return t1 + g > t2;
097: }
098: return Math.abs(t1 - t2) <= g;
099: }
100:
101: /**
102: * Compare two times.
103: * @param t1 the first time to compare.
104: * @param t2 the second time to compare.
105: * @return a negative integer, a positive integer, or zero as t1 is
106: * before, after, or equal to t2 accounting for the default granularity.
107: */
108: public static int compare(long t1, long t2) {
109: return compare(t1, t2, FILE_UTILS.getFileTimestampGranularity());
110: }
111:
112: /**
113: * Compare two times.
114: * @param t1 the first time to compare.
115: * @param t2 the second time to compare.
116: * @param g the timestamp granularity.
117: * @return a negative integer, a positive integer, or zero as t1 is
118: * before, after, or equal to t2 accounting for the specified granularity.
119: */
120: public static int compare(long t1, long t2, long g) {
121: long diff = t1 - t2;
122: long abs = Math.abs(diff);
123: return abs > Math.abs(g) ? (int) (diff / abs) : 0;
124: }
125:
126: }
|