From 9141e1aa16561e44f73e00b349735f530c94acc3 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Mon, 20 Jul 2015 12:32:32 +0100 Subject: [PATCH] HADOOP-12209 Comparable type should be in FileStatus. (Yong Zhang via stevel) --- .../hadoop-common/CHANGES.txt | 3 +++ .../java/org/apache/hadoop/fs/FileStatus.java | 15 +++++-------- .../apache/hadoop/fs/LocatedFileStatus.java | 10 +++------ .../fs/viewfs/ViewFsLocatedFileStatus.java | 3 ++- .../org/apache/hadoop/fs/TestFileStatus.java | 22 +++++++++++++++++++ 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 481d7de820a..18475b96d17 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -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 diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileStatus.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileStatus.java index 98757a737ab..6a79768a7be 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileStatus.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileStatus.java @@ -31,7 +31,7 @@ import org.apache.hadoop.io.Writable; */ @InterfaceAudience.Public @InterfaceStability.Stable -public class FileStatus implements Writable, Comparable { +public class FileStatus implements Writable, Comparable { private Path path; private long length; @@ -323,19 +323,14 @@ public class FileStatus implements Writable, Comparable { } /** - * 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 diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/LocatedFileStatus.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/LocatedFileStatus.java index 9e920c513bb..588fd6a4b6d 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/LocatedFileStatus.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/LocatedFileStatus.java @@ -90,17 +90,13 @@ public class LocatedFileStatus extends FileStatus { } /** - * 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); } diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFsLocatedFileStatus.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFsLocatedFileStatus.java index 347a809575a..4e681a7217e 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFsLocatedFileStatus.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFsLocatedFileStatus.java @@ -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; @@ -120,7 +121,7 @@ class ViewFsLocatedFileStatus extends LocatedFileStatus { } @Override - public int compareTo(Object o) { + public int compareTo(FileStatus o) { return super.compareTo(o); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileStatus.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileStatus.java index 5614dd6e56b..dd5279d963c 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileStatus.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileStatus.java @@ -26,6 +26,9 @@ import java.io.DataInputStream; 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; @@ -183,6 +186,25 @@ public class TestFileStatus { 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 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. */