1 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 28 public class TimeComparison extends EnumeratedAttribute { 29 private static final String [] VALUES 30 = new String [] {"before", "after", "equal"}; 31 32 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 33 34 35 public static final TimeComparison BEFORE = new TimeComparison("before"); 36 37 38 public static final TimeComparison AFTER = new TimeComparison("after"); 39 40 41 public static final TimeComparison EQUAL = new TimeComparison("equal"); 42 43 46 public TimeComparison() { 47 } 48 49 53 public TimeComparison(String value) { 54 setValue(value); 55 } 56 57 61 public String [] getValues() { 62 return VALUES; 63 } 64 65 71 public boolean evaluate(long t1, long t2) { 72 return evaluate(t1, t2, FILE_UTILS.getFileTimestampGranularity()); 73 } 74 75 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 103 public static int compare(long t1, long t2) { 104 return compare(t1, t2, FILE_UTILS.getFileTimestampGranularity()); 105 } 106 107 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 |