Skip to content

Commit

Permalink
HADOOP-12209 Comparable type should be in FileStatus. (Yong Zhang via…
Browse files Browse the repository at this point in the history
… stevel)
  • Loading branch information
steveloughran committed Jul 20, 2015
1 parent 05130e9 commit 9141e1a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
3 changes: 3 additions & 0 deletions hadoop-common-project/hadoop-common/CHANGES.txt
Expand Up @@ -972,6 +972,9 @@ Release 2.8.0 - UNRELEASED
HADOOP-12235 hadoop-openstack junit & mockito dependencies should be
"provided". (Ted Yu via stevel)

HADOOP-12209 Comparable type should be in FileStatus.
(Yong Zhang via stevel)

Release 2.7.2 - UNRELEASED

INCOMPATIBLE CHANGES
Expand Down
Expand Up @@ -31,7 +31,7 @@
*/
@InterfaceAudience.Public
@InterfaceStability.Stable
public class FileStatus implements Writable, Comparable {
public class FileStatus implements Writable, Comparable<FileStatus> {

private Path path;
private long length;
Expand Down Expand Up @@ -323,19 +323,14 @@ public void readFields(DataInput in) throws IOException {
}

/**
* Compare this object to another object
*
* @param o the object to be compared.
* Compare this FileStatus to another FileStatus
* @param o the FileStatus to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
*
* @throws ClassCastException if the specified object's is not of
* type FileStatus
*/
@Override
public int compareTo(Object o) {
FileStatus other = (FileStatus)o;
return this.getPath().compareTo(other.getPath());
public int compareTo(FileStatus o) {
return this.getPath().compareTo(o.getPath());
}

/** Compare if this object is equal to another object
Expand Down
Expand Up @@ -90,17 +90,13 @@ public BlockLocation[] getBlockLocations() {
}

/**
* Compare this object to another object
*
* @param o the object to be compared.
* Compare this FileStatus to another FileStatus
* @param o the FileStatus to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
*
* @throws ClassCastException if the specified object's is not of
* type FileStatus
*/
@Override
public int compareTo(Object o) {
public int compareTo(FileStatus o) {
return super.compareTo(o);
}

Expand Down
Expand Up @@ -18,6 +18,7 @@
package org.apache.hadoop.fs.viewfs;

import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission;
Expand Down Expand Up @@ -120,7 +121,7 @@ public BlockLocation[] getBlockLocations() {
}

@Override
public int compareTo(Object o) {
public int compareTo(FileStatus o) {
return super.compareTo(o);
}

Expand Down
Expand Up @@ -26,6 +26,9 @@
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.Test;
import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -183,6 +186,25 @@ public void toStringDir() throws IOException {
validateToString(fileStatus);
}

@Test
public void testCompareTo() throws IOException {
Path path1 = new Path("path1");
Path path2 = new Path("path2");
FileStatus fileStatus1 =
new FileStatus(1, true, 1, 1, 1, 1, FsPermission.valueOf("-rw-rw-rw-"),
"one", "one", null, path1);
FileStatus fileStatus2 =
new FileStatus(1, true, 1, 1, 1, 1, FsPermission.valueOf("-rw-rw-rw-"),
"one", "one", null, path2);
assertTrue(fileStatus1.compareTo(fileStatus2) < 0);
assertTrue(fileStatus2.compareTo(fileStatus1) > 0);

List<FileStatus> statList = new ArrayList<>();
statList.add(fileStatus1);
statList.add(fileStatus2);
assertTrue(Collections.binarySearch(statList, fileStatus1) > -1);
}

/**
* Check that toString produces the expected output for a symlink.
*/
Expand Down

0 comments on commit 9141e1a

Please sign in to comment.