2010-04-13

自訂的 Java File 排序 (Sort the File class in Java)

1
2
import java.util.Comparator;
import java.io.File;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
public class FileComparator implements Comparator<File>{
 
    public static final int NAME = 1;
    public static final int SIZE = 2;
    public static final int TYPE = 3;
    public static final int MODIFY = 4;
 
    public static final char ASCENDING = 'A';
    public static final char DESCENDING = 'D';
 
    private int method;
    private char order;
 
    public FileComparator(){
        this.setMethod(FileComparator.NAME);
        this.setOrder(FileComparator.ASCENDING);
    }
 
    public FileComparator(int method){
        this.setMethod(method);
        this.setOrder(FileComparator.ASCENDING);
    }
 
    public FileComparator(char order){
        this.setMethod(FileComparator.NAME);
        this.setOrder(order);
    }
 
    public FileComparator(int method, char order){
        this.setMethod(method);
        this.setOrder(order);
    }
 
    private void setMethod(int method){
        method: switch (method){
            case FileComparator.SIZE:
            case FileComparator.TYPE:
            case FileComparator.MODIFY:{
                this.method = method;
            } break method;
            default:{
                this.method = FileComparator.NAME;
            }
        }
    }
 
    private void setOrder(char order){
        order: switch (order){
            case FileComparator.DESCENDING:{
                this.order = order;
            } break order;
            default:{
                this.order = FileComparator.ASCENDING;
            }
        }
    }
 
    public int compare(File file1, File file2){
        int c;
        method: switch (this.method){
            case FileComparator.SIZE:{
                order: switch (this.order){
                    case FileComparator.DESCENDING:{
                        c = new Long(file2.length() - file1.length()).intValue();
                    } break order;
                    default:{
                        c = new Long(file1.length() - file2.length()).intValue();
                    }
                }
            } break method;
            case FileComparator.TYPE:{
                String extension1 = file1.getName();
                String extension2 = file2.getName();
                String filename1 = null;
                String filename2 = null;
                {
                    int last = extension1.lastIndexOf('.');
                    if (last > -1){
                        filename1 = extension1.substring(0, last);
                        extension1 = extension1.substring(last + 1);
                    }
                }
                {
                    int last = extension2.lastIndexOf('.');
                    if (last > -1){
                        filename2 = extension2.substring(0, last);
                        extension2 = extension2.substring(last + 1);
                    }
                }
                order: switch (this.order){
                    case FileComparator.DESCENDING:{
                        c = extension2.compareTo(extension1) * 1024 + ((filename2 == null) ? 0 : filename2.compareTo(filename1));
                    } break order;
                    default:{
                        c = extension1.compareTo(extension2) * 1024 + ((filename1 == null) ? 0 : filename1.compareTo(filename2));
                    }
                }
            } break method;
            case FileComparator.MODIFY:{
                order: switch (this.order){
                    case FileComparator.DESCENDING:{
                        c = new Long(file2.lastModified() - file1.lastModified()).intValue();
                    } break order;
                    default:{
                        c = new Long(file1.lastModified() - file2.lastModified()).intValue();
                    }
                }
            } break method;
            default:{
                order: switch (this.order){
                    case FileComparator.DESCENDING:{
                        c = file2.getName().compareTo(file1.getName());
                    } break order;
                    default:{
                        c = file1.getName().compareTo(file2.getName());
                    }
                }
            }
        }
        return c;
    }
 
    public String toString(){
        return String.format("Method: %s, Order: %s", this.method, this.order);
    }
}

使用 java.util.Arrays.sort() 時, 可讓 File 的類別依照指定的形式排序
如 java.util.Arrays.sort(files, new FileComparator(FileComparator.NAME, FileComparator.ASCENDING)
Use java.util.Arrays.sort() to make File class sort by specific order
For example java.util.Arrays.sort(files, new FileComparator(FileComparator.NAME, FileComparator.ASCENDING)

但需要注意的是 FileComparator.TYPE 的排序方式並非最好的編程方法
However, FileComparator.TYPE is not the best order

沒有留言 :

張貼留言