1 package de.jwi.jfm; 2 3 24 25 26 import java.io.File ; 27 import java.util.Comparator ; 28 29 30 34 public class FileComparator implements Comparator 35 { 36 public static final int SORT_NAME_UP = 1; 37 public static final int SORT_NAME_DOWN = 2; 38 public static final int SORT_DATE_UP = 3; 39 public static final int SORT_DATE_DOWN = 4; 40 public static final int SORT_SIZE_UP = 5; 41 public static final int SORT_SIZE_DOWN = 6; 42 43 44 public static final Comparator nameUpInstance = new FileComparator(SORT_NAME_UP); 45 public static final Comparator nameDownInstance = new FileComparator(SORT_NAME_DOWN); 46 public static final Comparator dateUpInstance = new FileComparator(SORT_DATE_UP); 47 public static final Comparator dateDownInstance = new FileComparator(SORT_DATE_DOWN); 48 public static final Comparator sizeUpInstance = new FileComparator(SORT_SIZE_UP); 49 public static final Comparator sizeDownInstance = new FileComparator(SORT_SIZE_DOWN); 50 51 52 private int mode; 53 54 FileComparator(int mode) 55 { 56 this.mode = mode; 57 } 58 59 public int compare(Object o1, 60 Object o2) 61 { 62 FileWrapper fw1 = (FileWrapper)o1; 63 FileWrapper fw2 = (FileWrapper)o2; 64 65 File f1 = fw1.getFile(); 66 File f2 = fw2.getFile(); 67 68 int rc = 0; 69 70 if (SORT_NAME_UP == mode) 71 { 72 rc= f1.getName().compareToIgnoreCase(f2.getName()); 73 } 74 else if (SORT_NAME_DOWN == mode) 75 { 76 rc= f2.getName().compareToIgnoreCase(f1.getName()); 77 } 78 else if (SORT_SIZE_UP == mode) 79 { 80 long l =(f1.length() - f2.length() ) ; 81 rc = l < 0l ? -1 : l == 0l ? 0 : 1; 82 } 83 else if (SORT_SIZE_DOWN == mode) 84 { 85 long l =(f2.length() - f1.length() ) ; 86 rc = l < 0l ? -1 : l == 0l ? 0 : 1; 87 } 88 89 else if (SORT_DATE_UP == mode) 90 { 91 long l =(f1.lastModified() - f2.lastModified()) ; 92 rc = l < 0l ? -1 : l == 0l ? 0 : 1; 93 } 94 else if (SORT_DATE_DOWN == mode) 95 { 96 long l =(f2.lastModified() - f1.lastModified()) ; 97 rc = l < 0l ? -1 : l == 0l ? 0 : 1; 98 } 99 return rc; 100 } 101 } 102 | Popular Tags |