1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.core.appender.rolling.action;
19
20 import org.apache.logging.log4j.core.config.plugins.Plugin;
21 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
22 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
23
24
25
26
27 @Plugin(name = "SortByModificationTime", category = "Core", printObject = true)
28 public class PathSortByModificationTime implements PathSorter {
29
30 private final boolean recentFirst;
31 private final int multiplier;
32
33
34
35
36
37
38 public PathSortByModificationTime(final boolean recentFirst) {
39 this.recentFirst = recentFirst;
40 this.multiplier = recentFirst ? 1 : -1;
41 }
42
43
44
45
46
47
48
49 @PluginFactory
50 public static PathSorter createSorter(
51 @PluginAttribute(value = "recentFirst", defaultBoolean = true) final boolean recentFirst) {
52 return new PathSortByModificationTime(recentFirst);
53 }
54
55
56
57
58
59
60 public boolean isRecentFirst() {
61 return recentFirst;
62 }
63
64
65
66
67
68
69 @Override
70 public int compare(final PathWithAttributes path1, final PathWithAttributes path2) {
71 final long lastModified1 = path1.getAttributes().lastModifiedTime().toMillis();
72 final long lastModified2 = path2.getAttributes().lastModifiedTime().toMillis();
73 int result = Long.signum(lastModified2 - lastModified1);
74 if (result == 0) {
75 try {
76
77
78 result = path2.getPath().compareTo(path1.getPath());
79 } catch (final ClassCastException ex) {
80 result = path2.getPath().toString().compareTo(path1.getPath().toString());
81 }
82 }
83 return multiplier * result;
84 }
85 }