Java Code Examples for java.io.File#compareTo()

The following examples show how to use java.io.File#compareTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCommandTestCase.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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;
    }
 
Example 2
Source File: Configuration.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/** {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);
    }
 
Example 3
Source File: DirNode.java    From FilePicker with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 4
Source File: FileWrap.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
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);
	}
}
 
Example 5
Source File: Configuration.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/** {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);
    }
 
Example 6
Source File: Config.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/** {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);
}
 
Example 7
Source File: CrashFileManager.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@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;
    }
}
 
Example 8
Source File: ImportFileUtils.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: FileExplorerActivity.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: DirectoryScanner.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * 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");
  }
}
 
Example 11
Source File: DirectoryScanner.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * 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");
  }
}
 
Example 12
Source File: NavigationHelper.java    From Android-FileBrowser-FilePicker with MIT License 5 votes vote down vote up
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;
    }
 
Example 13
Source File: Config.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/** {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);
}
 
Example 14
Source File: FileExplorerActivity.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
public int compare(File left, File right)
{
	return left.compareTo(right);
}
 
Example 15
Source File: Dockerfile.java    From docker-java with Apache License 2.0 4 votes vote down vote up
private boolean isBaseDirectory(File directory) {
    return directory.compareTo(baseDirectory) == 0;
}
 
Example 16
Source File: CommitCounterUtility.java    From database with GNU General Public License v2.0 2 votes vote down vote up
@Override
public int compare(final File o1, final File o2) {

    return o2.compareTo(o1);

}
 
Example 17
Source File: DefaultFileComparator.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: DefaultFileComparator.java    From aion-germany with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 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);
}