小米開原始檔管理器MiCodeFileExplorer-原始碼研究(8)-檔案排序工具類FileSortHelper

小雷FansUnion發表於2015-10-29
FileSortHelper的核心功能就是,對檔案集合FileInfo排序。
FileInfo有若干欄位,根據欄位定義了4種比較器Comparator。
呼叫示例:Collections.sort(List<FileInfo>, Comparator);
實現排序,FileInfo可以實現Comparable介面,但是比較方式是固定的,也就是說只能採用一種方式排序。
而Comparator則比較靈活,更像是一種“策略模式”,傳入不同的“策略”,實現不同方式的排序。


package net.micode.fileexplorer.util;


import java.util.Comparator;
import java.util.HashMap;


import net.micode.fileexplorer.model.FileInfo;
//檔案排序工具類
//可以按照檔案的名字、大小、日期、型別,排序
//呼叫示例:Collections.sort(List<FileInfo>, Comparator);
public class FileSortHelper {


	public enum SortMethod {
		name, size, date, type
	}


	// 排序型別
	private SortMethod mSort;


	// 是否檔案優先
	private boolean mFileFirst;


	// 比較器,這個才是關鍵,實現compare方法
	private HashMap<SortMethod, Comparator> mComparatorList = new HashMap<SortMethod, Comparator>();


	public FileSortHelper() {
		mSort = SortMethod.name;
		mComparatorList.put(SortMethod.name, cmpName);
		mComparatorList.put(SortMethod.size, cmpSize);
		mComparatorList.put(SortMethod.date, cmpDate);
		mComparatorList.put(SortMethod.type, cmpType);
	}


	public void setSortMethog(SortMethod s) {
		mSort = s;
	}


	public SortMethod getSortMethod() {
		return mSort;
	}


	public void setFileFirst(boolean f) {
		mFileFirst = f;
	}


	public Comparator getComparator() {
		return mComparatorList.get(mSort);
	}


	private abstract class FileComparator implements Comparator<FileInfo> {


		@Override
		public int compare(FileInfo object1, FileInfo object2) {
			if (object1.IsDir == object2.IsDir) {
				return doCompare(object1, object2);
			}


			if (mFileFirst) {
				// the files are listed before the dirs
				return (object1.IsDir ? 1 : -1);
			} else {
				// the dir-s are listed before the files
				return object1.IsDir ? -1 : 1;
			}
		}


		protected abstract int doCompare(FileInfo object1, FileInfo object2);
	}


	private Comparator cmpName = new FileComparator() {
		@Override
		public int doCompare(FileInfo object1, FileInfo object2) {
			return object1.fileName.compareToIgnoreCase(object2.fileName);
		}
	};


	private Comparator cmpSize = new FileComparator() {
		@Override
		public int doCompare(FileInfo object1, FileInfo object2) {
			return longToCompareInt(object1.fileSize - object2.fileSize);
		}
	};


	private Comparator cmpDate = new FileComparator() {
		@Override
		public int doCompare(FileInfo object1, FileInfo object2) {
			return longToCompareInt(object2.ModifiedDate - object1.ModifiedDate);
		}
	};


	private int longToCompareInt(long result) {
		return result > 0 ? 1 : (result < 0 ? -1 : 0);
	}


	private Comparator cmpType = new FileComparator() {
		@Override
		public int doCompare(FileInfo object1, FileInfo object2) {
			int result = Util.getExtFromFilename(object1.fileName)
					.compareToIgnoreCase(
							Util.getExtFromFilename(object2.fileName));
			if (result != 0)
				return result;


			return Util.getNameFromFilename(object1.fileName)
					.compareToIgnoreCase(
							Util.getNameFromFilename(object2.fileName));
		}
	};
}




還有一些工具類,由於和impl包和UI包關聯比較大,後面在一起講解。

相關文章