xfilecomparator.java

来自「JavaExplorer是一个独立于平台的浏览器」· Java 代码 · 共 156 行

JAVA
156
字号
/**  * File and FTP Explorer  * Copyright 2002  * BOESCH Vincent  *  * This program is free software; you can redistribute it and/or  * modify it under the terms of the GNU General Public License  * as published by the Free Software Foundation; either version 2  * of the License, or (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  */package javaexplorer.util.comparator;import java.util.*;import javaexplorer.model.XFile;public class XFileComparator implements Comparator {    private static XFileComparator _fileComparator = new XFileComparator();    public static final int SORT_NAME = 0;    public static final int SORT_SIZE = 1;    public static final int SORT_DATE = 2;    private int _intSortType = 0; //Par defaut, sort sur le nom    boolean reverse = false;    /**     *  Constructor for the FileComparator     *  object     */    public XFileComparator() {    }    //Comparaison de deux fichiers    /**     *  Description of the Method     *     *@param  obj1  Description of Parameter     *@param  obj2  Description of Parameter     *@return       Description of the Returned     *      Value     */    public int compare(Object obj1, Object obj2) {        //Objet null plus petit qu'un objet non null        if (obj1 == null) {            if (obj2 == null) {                return 0;            } else {                return 1;            }        } else {            if (obj2 == null) {                return -1;            } else {                XFile f1 = (XFile) obj1;                XFile f2 = (XFile) obj2;                return compare(f1, f2, _intSortType);            }        }    }    /**     * Comparaison de deux xfile non-nuls !!!     */    private int compare(XFile x1, XFile x2, int method) {        switch (method) {        case SORT_SIZE:            if (x1.length() == x2.length()) {                return compare(x1, x2, SORT_NAME);            } else {                return (x1.length() > x2.length()) ? (reverse ? (-1) : 1)                                                   : (reverse ? 1 : (-1));            }        case SORT_DATE:            if (x1.lastModified() == x2.lastModified()) {                return compare(x1, x2, SORT_NAME);            } else {                return (x1.lastModified() > x2.lastModified())                ? (reverse ? (-1) : 1) : (reverse ? 1 : (-1));            }        case SORT_NAME:default:            if (x1.isDirectory()) {                if (!x2.isDirectory()) {                    return (reverse ? 1 : (-1));                }            } else {                if (x2.isDirectory()) {                    return (reverse ? (-1) : 1);                }            }            int res = x1.toString().compareToIgnoreCase(x2.toString());            //Si pareil en ignoreCase, on regarde si les noms sont diff            if (res == 0) {                res = x1.toString().compareTo(x2.toString());            }            return reverse ? (-res) : res;        }    }    //2 fichiers egaux ?    /**     *  Description of the Method     *     *@param  obj1  Description of Parameter     *@param  obj2  Description of Parameter     *@return       Description of the Returned     *      Value     */    public boolean equals(Object obj1, Object obj2) {        return compare(obj1, obj2) == 0;    }    public void setSortMethod(int sortType) {        if (sortType == _intSortType) {            reverse = !reverse;        } else {            reverse = false;        }        _intSortType = sortType;    }    public int getSortMethod() {        return _intSortType;    }    /**     *  Gets the Comparator attribute of the     *  FileComparator class     *     *@return    The Comparator value     */    public static XFileComparator getComparator() {        return _fileComparator;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?