下面列出了java.io.File#compareTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Collection<File> sortFiles(Collection<File> files) {
final class FileComparator implements Comparator<File> {
public int compare(File f1, File f2) {
return f1.compareTo(f2);
}
}
if (files.isEmpty()) {
return Collections.<File>emptyList();
}
if (files.size() == 1) {
return Collections.singletonList(files.iterator().next());
}
SortedSet<File> sortedFiles = new TreeSet<File>(new FileComparator());
sortedFiles.addAll(files);
return sortedFiles;
}
/** {inheritDoc} */
public int compare(File f1, File f2) {
// Need to make sure we read the child namespace before the base
// namespace. To do that, we sort the list in reverse order based
// on the length of the file name. If two filenames have the same
// length, then we need to do a lexigraphical comparison to make
// sure that the filenames themselves are different.
int lenDif = f2.getAbsolutePath().length() - f1.getAbsolutePath().length();
if (lenDif != 0) {
return lenDif;
}
return f2.compareTo(f1);
}
/** {inheritDoc} */
public int compare(File f1, File f2) {
// Need to make sure we read the child namespace before the base
// namespace. To do that, we sort the list in reverse order based
// on the length of the file name. If two filenames have the same
// length, then we need to do a lexigraphical comparison to make
// sure that the filenames themselves are different.
int lenDif = f2.getAbsolutePath().length() - f1.getAbsolutePath().length();
if (lenDif != 0) {
return lenDif;
}
return f2.compareTo(f1);
}
public boolean navigateBack() {
File parent = mFileNavigator.getmCurrentNode().getParentFile();
if(parent==null || parent.compareTo(mFileNavigator.getmCurrentNode())==0 || Constants.externalStorageRoot==null || Constants.externalStorageRoot.compareTo(mFileNavigator.getmCurrentNode())==0 || Constants.internalStorageRoot.compareTo(mFileNavigator.getmCurrentNode())==0)
return false;
mFileNavigator.setmCurrentNode(parent);
triggerFileChanged();
return true;
}
/**
* Verify whether the actual directory location of block file has the
* expected directory path computed using its block ID.
*/
private void verifyFileLocation(File actualBlockDir,
File bpFinalizedDir, long blockId) {
File blockDir = DatanodeUtil.idToBlockDir(bpFinalizedDir, blockId);
if (actualBlockDir.compareTo(blockDir) != 0) {
LOG.warn("Block: " + blockId
+ " has to be upgraded to block ID-based layout");
}
}
/**
* Verify whether the actual directory location of block file has the
* expected directory path computed using its block ID.
*/
private void verifyFileLocation(File actualBlockDir,
File bpFinalizedDir, long blockId) {
File blockDir = DatanodeUtil.idToBlockDir(bpFinalizedDir, blockId);
if (actualBlockDir.compareTo(blockDir) != 0) {
LOG.warn("Block: " + blockId
+ " has to be upgraded to block ID-based layout");
}
}
public int compare(File left, File right)
{
if (left.isDirectory())
{
if (right.isDirectory())
{
return left.compareTo(right);
}
return -1;
}
return right.isDirectory() ? 1 : left.compareTo(right);
}
/**
* Limit list of possible file to import.
*
* @param toFilter set of files to import
* @param fileName optional file name
* @return the filtered file array
*/
private static File[] filterFiles(final File[] toFilter, final String fileName) {
if (fileName != null && toFilter != null) {
final File fileAsFilter = new File(fileName);
for (File file : toFilter) {
if (file.compareTo(fileAsFilter) == 0) {
return new File[]{fileAsFilter};
}
}
return new File[0];
}
return toFilter;
}
@Override
public int compare(File lhs, File rhs) {
if (lhs.lastModified() == rhs.lastModified()) {
return lhs.compareTo(rhs);
} else if (lhs.lastModified() < rhs.lastModified()) {
return 1;
} else {
return -1;
}
}
/** {inheritDoc} */
public int compare(File f1, File f2) {
// Need to make sure we read the child namespace before the base
// namespace. To do that, we sort the list in reverse order based
// on the length of the file name. If two filenames have the same
// length, then we need to do a lexigraphical comparison to make
// sure that the filenames themselves are different.
int lenDif = f2.getAbsolutePath().length() - f1.getAbsolutePath().length();
if (lenDif != 0) {
return lenDif;
}
return f2.compareTo(f1);
}
/** {inheritDoc} */
public int compare(File f1, File f2) {
// Need to make sure we read the child namespace before the base
// namespace. To do that, we sort the list in reverse order based
// on the length of the file name. If two filenames have the same
// length, then we need to do a lexigraphical comparison to make
// sure that the filenames themselves are different.
int lenDif = f2.getAbsolutePath().length() - f1.getAbsolutePath().length();
if (lenDif != 0) {
return lenDif;
}
return f2.compareTo(f1);
}
public int compare(File o1, File o2) {
if (o1.isDirectory() && !o2.isDirectory()) {
return -1;
} else if (!o1.isDirectory() && o2.isDirectory()) {
return 1;
} else {
return o1.compareTo(o2);
}
}
/**
* Add a file to the history and/or switch to the history for the file.
*
* @param newPath Path to add/switch to.
* @return True if the active data set has changed. False if no change (path
* was already in the selected history).
*/
public Pair<Boolean, DirNode> addPath(File file) {
int depthDelta = file.getPath().split(File.separator).length - mFileDepth;
if (depthDelta == 1) {
for (int i = 0; i < mChildren.size(); i++) {
if (file.compareTo(mChildren.get(i).mFile) == 0) {
if (i == 0) {
// Already in the currently selected history and we
// don't need to changes the data set.
return new Pair<Boolean, DirNode>(DATA_SET_UNCHANGED, mChildren.get(i));
}
DirNode selected = mChildren.remove(i);
addChild(selected);
selected.addOrUpdateFragments();
return new Pair<Boolean, DirNode>(DATA_SET_CHANGED, selected);
}
}
DirNode newChild = new DirNode(this, file, mPagerAdapter, mFragmentManager);
addChild(newChild);
newChild.addOrUpdateFragments();
return new Pair<Boolean, DirNode>(DATA_SET_CHANGED, newChild);
} else if (depthDelta > 1) {
// Assume that only the active history will have paths added so
// pass on down to the zeroth child.
return mChildren.get(0).addPath(file);
} else if (TextUtils.equals(mFile.getPath(), file.getPath())) {
return new Pair<Boolean, DirNode>(DATA_SET_UNCHANGED, this);
} else if (mParent == null) {
throw new RuntimeException("Illegal attempt to add " + file.getPath()
+ " to parent node where no parent exists.");
} else {
return mParent.addPath(file);
}
}
public int compare(File left, File right)
{
return left.compareTo(right);
}
private boolean isBaseDirectory(File directory) {
return directory.compareTo(baseDirectory) == 0;
}
/**
* Compare the two files using the {@link File#compareTo(File)} method.
*
* @param file1 The first file to compare
* @param file2 The second file to compare
* @return the result of calling file1's
* {@link File#compareTo(File)} with file2 as the parameter.
*/
public int compare(File file1, File file2) {
return file1.compareTo(file2);
}
/**
* Compare the two files using the {@link File#compareTo(File)} method.
*
* @param file1 The first file to compare
* @param file2 The second file to compare
* @return the result of calling file1's
* {@link File#compareTo(File)} with file2 as the parameter.
*/
public int compare(final File file1, final File file2) {
return file1.compareTo(file2);
}
@Override
public int compare(final File o1, final File o2) {
return o2.compareTo(o1);
}