KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > TimeComparison


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.types;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.util.FileUtils;
22
23 /**
24  * EnumeratedAttribute for time comparisons. Accepts values
25  * "before", "after", "equal".
26  * @since Ant 1.7
27  */

28 public class TimeComparison extends EnumeratedAttribute {
29     private static final String JavaDoc[] VALUES
30         = new String JavaDoc[] {"before", "after", "equal"};
31
32     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
33
34     /** Before Comparison. */
35     public static final TimeComparison BEFORE = new TimeComparison("before");
36
37     /** After Comparison. */
38     public static final TimeComparison AFTER = new TimeComparison("after");
39
40     /** Equal Comparison. */
41     public static final TimeComparison EQUAL = new TimeComparison("equal");
42
43     /**
44      * Default constructor.
45      */

46     public TimeComparison() {
47     }
48
49     /**
50      * Construct a new TimeComparison with the specified value.
51      * @param value the EnumeratedAttribute value.
52      */

53     public TimeComparison(String JavaDoc value) {
54         setValue(value);
55     }
56
57     /**
58      * Return the possible values.
59      * @return String[] of EnumeratedAttribute values.
60      */

61     public String JavaDoc[] getValues() {
62         return VALUES;
63     }
64
65     /**
66      * Evaluate two times against this TimeComparison.
67      * @param t1 the first time to compare.
68      * @param t2 the second time to compare.
69      * @return true if the comparison result fell within the parameters of this TimeComparison.
70      */

71     public boolean evaluate(long t1, long t2) {
72         return evaluate(t1, t2, FILE_UTILS.getFileTimestampGranularity());
73     }
74
75     /**
76      * Evaluate two times against this TimeComparison.
77      * @param t1 the first time to compare.
78      * @param t2 the second time to compare.
79      * @param g the timestamp granularity.
80      * @return true if the comparison result fell within the parameters of this TimeComparison.
81      */

82     public boolean evaluate(long t1, long t2, long g) {
83         int cmp = getIndex();
84         if (cmp == -1) {
85             throw new BuildException("TimeComparison value not set.");
86         }
87         if (cmp == 0) {
88             return t1 - g < t2;
89         }
90         if (cmp == 1) {
91             return t1 + g > t2;
92         }
93         return Math.abs(t1 - t2) <= g;
94     }
95
96     /**
97      * Compare two times.
98      * @param t1 the first time to compare.
99      * @param t2 the second time to compare.
100      * @return a negative integer, a positive integer, or zero as t1 is
101      * before, after, or equal to t2 accounting for the default granularity.
102      */

103     public static int compare(long t1, long t2) {
104         return compare(t1, t2, FILE_UTILS.getFileTimestampGranularity());
105     }
106
107     /**
108      * Compare two times.
109      * @param t1 the first time to compare.
110      * @param t2 the second time to compare.
111      * @param g the timestamp granularity.
112      * @return a negative integer, a positive integer, or zero as t1 is
113      * before, after, or equal to t2 accounting for the specified granularity.
114      */

115     public static int compare(long t1, long t2, long g) {
116         long diff = t1 - t2;
117         long abs = Math.abs(diff);
118         return abs > Math.abs(g) ? (int) (diff / abs) : 0;
119     }
120
121 }
122
123
Popular Tags