Hi John,
I think I found a bug in the DiskCache comparators:
The problem is a truncation issue when casting from long to int.
In DiskCache.java, line 346, 'FileAgeComparator'
Here is the problem line:
return (int) (f2.getTime() - f1.getTime());
Here is a fix:
return (f2.getTime() - f1.getTime())> 0 ? 1 : -1;
The 'FileSizeComparator' has the same issue but I think is less likely
to show unless really big files are in use.
I hope this helps.
Steve
Here is a short test class using Date instead of File, that shows the
problem:
package steve.test;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
public class ComparatorTest {
public static void main(String[] args) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date[] dateList = new Date[] {
sdf.parse("2002-01-01"),
sdf.parse("2002-01-02"),
sdf.parse("2002-01-03"),
sdf.parse("2002-01-04"),
sdf.parse("2002-02-05"),
sdf.parse("2002-03-06")
};
Arrays.sort(dateList, new DateComparator1());
System.out.println("sort error: " + Arrays.toString(dateList));
Arrays.sort(dateList, new DateComparator2());
System.out.println("sort fix: "+Arrays.toString(dateList));
} catch (Exception e) {
e.printStackTrace();
}
}
// reverse sort - latest come first
static private class DateComparator1 implements Comparator<Date> {
public int compare(Date f1, Date f2) {
System.out.print(f2+"-"+f1+" =
"+f2.getTime()+"-"+f1.getTime()+" = int: "+(int) (f2.getTime() -
f1.getTime()));
System.out.println(" long: "+(f2.getTime() - f1.getTime()));
return (int) (f2.getTime() - f1.getTime());
}
}
// reverse sort - latest come first
static private class DateComparator2 implements Comparator<Date> {
public int compare(Date f1, Date f2) {
return (f2.getTime() - f1.getTime())> 0 ? 1 : -1;
}
}
}