LOG4J2-435 sort by lastModified time first, by path lexicographically
next

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/9df265a5
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/9df265a5
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/9df265a5

Branch: refs/heads/LOG4J-1181
Commit: 9df265a58426a29b3fef38cf4674a69bcb6a8438
Parents: d84624b
Author: rpopma <[email protected]>
Authored: Sun Nov 29 15:18:03 2015 +0900
Committer: rpopma <[email protected]>
Committed: Sun Nov 29 15:18:03 2015 +0900

----------------------------------------------------------------------
 .../rolling/action/PathSortByModificationTime.java     | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9df265a5/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathSortByModificationTime.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathSortByModificationTime.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathSortByModificationTime.java
index 522d1c3..22aaf20 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathSortByModificationTime.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/PathSortByModificationTime.java
@@ -54,12 +54,13 @@ public class PathSortByModificationTime implements 
PathSorter {
 
     /**
      * Returns whether this sorter sorts recent files first.
+     * 
      * @return whether this sorter sorts recent files first
      */
     public boolean isRecentFirst() {
         return recentFirst;
     }
-    
+
     /*
      * (non-Javadoc)
      * 
@@ -69,6 +70,14 @@ public class PathSortByModificationTime implements 
PathSorter {
     public int compare(final PathWithAttributes path1, final 
PathWithAttributes path2) {
         final long lastModified1 = 
path1.getAttributes().lastModifiedTime().toMillis();
         final long lastModified2 = 
path2.getAttributes().lastModifiedTime().toMillis();
-        return multiplier * Long.signum(lastModified2 - lastModified1);
+        int result = multiplier * Long.signum(lastModified2 - lastModified1);
+        if (result == 0) { // if same time compare paths lexicographically
+            try {
+                result = path1.getPath().compareTo(path2.getPath());
+            } catch (final ClassCastException ex) {
+                result = 
path1.getPath().toString().compareTo(path2.getPath().toString());
+            }
+        }
+        return result;
     }
 }

Reply via email to