2010年4月13日星期二

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

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

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

沒有留言 :

發佈留言