|
19 | 19 | import java.awt.*; |
20 | 20 | import java.io.File; |
21 | 21 | import java.io.IOException; |
| 22 | +import java.time.Duration; |
| 23 | +import java.time.Instant; |
22 | 24 | import java.util.*; |
23 | 25 | import java.util.List; |
24 | 26 | import java.util.concurrent.ExecutionException; |
@@ -52,7 +54,9 @@ public void onChooseFolder(ActionEvent event) { |
52 | 54 | if (langFolder != null) { |
53 | 55 | if(view.isSmartSearchEnabled()) { |
54 | 56 | view.setStatus("Starting smart search from selected folder"); |
55 | | - langFolder = findLangFolder(langFolder); |
| 57 | + Instant i1 = Instant.now(); |
| 58 | + System.out.println(langFolder = findLangFolder2(langFolder)); |
| 59 | + System.out.println(Duration.between(i1, Instant.now())); |
56 | 60 | } |
57 | 61 | view.setStatus("loading lang files"); |
58 | 62 | try { |
@@ -83,13 +87,48 @@ private File findLangFolder(File rootFolder) { |
83 | 87 | File[] subFiles = rootFolder.listFiles(); |
84 | 88 | if(subFiles == null || subFiles.length == 0) |
85 | 89 | return rootFolder; |
86 | | - return Arrays.stream(subFiles).filter(File::isDirectory).map(this::findLangFolder).max((f1, f2) -> { |
| 90 | + return Arrays.stream(subFiles).parallel().filter(File::isDirectory).map(this::findLangFolder).max((f1, f2) -> { |
| 91 | + File[] subFiles1 = f1.listFiles(); |
| 92 | + File[] subFiles2 = f2.listFiles(); |
| 93 | + if(subFiles1 == null) return subFiles2 == null ? 0 : -1; |
| 94 | + if(subFiles2 == null) return 1; |
| 95 | + int score1 = 0; |
| 96 | + int score2 = 0; |
| 97 | + long langFiles1 = Arrays.stream(subFiles1).filter(f -> f.isFile() && LANG_PATTERN.matcher(f.getName()).matches()).count(); |
| 98 | + long langFiles2 = Arrays.stream(subFiles2).filter(f -> f.isFile() && LANG_PATTERN.matcher(f.getName()).matches()).count(); |
| 99 | + if(langFiles1 > langFiles2) // a lang folder has lang files in it |
| 100 | + score1++; |
| 101 | + else if(langFiles2 > langFiles1) |
| 102 | + score2++; |
| 103 | + if("lang".equals(f1.getName())) // a lang folder is called "lang" |
| 104 | + score1++; |
| 105 | + if("lang".equals(f2.getName())) |
| 106 | + score2++; |
| 107 | + if(Arrays.stream(subFiles1).noneMatch(File::isDirectory)) // a lang folder doesn't have any subfolder |
| 108 | + score1++; |
| 109 | + if(Arrays.stream(subFiles2).noneMatch(File::isDirectory)) |
| 110 | + score2++; |
| 111 | +// System.out.println(f1 + ": " + score1 + "\n" + f2 + ": " + score2); |
| 112 | + return Integer.compare(score1, score2); |
| 113 | + }).orElse(rootFolder); |
| 114 | + } |
| 115 | + |
| 116 | + private File findLangFolder2(File rootFolder) { |
| 117 | + File[] subFiles = rootFolder.listFiles(); |
| 118 | + if(subFiles == null || subFiles.length == 0) |
| 119 | + return rootFolder; |
| 120 | + return Arrays.stream(subFiles).parallel().filter(File::isDirectory).map(this::findLangFolder).max((f1, f2) -> { |
87 | 121 | File[] langFiles1 = f1.listFiles(f -> f.isFile() && LANG_PATTERN.matcher(f.getName()).matches()); |
88 | 122 | File[] langFiles2 = f2.listFiles(f -> f.isFile() && LANG_PATTERN.matcher(f.getName()).matches()); |
89 | 123 | if(langFiles1 == null) return langFiles2 == null ? 0 : -1; |
90 | 124 | if(langFiles2 == null) return 1; |
91 | 125 | if(langFiles1.length > 0) { |
92 | | - if(langFiles2.length > 0) return Integer.compare(langFiles1.length, langFiles2.length); |
| 126 | + if(langFiles2.length > 0) { |
| 127 | + if (langFiles1.length != langFiles2.length) |
| 128 | + return Integer.compare(langFiles1.length, langFiles2.length); |
| 129 | + else |
| 130 | + return Long.compare(f1.lastModified(), f2.lastModified()); |
| 131 | + } |
93 | 132 | return 1; |
94 | 133 | } else if(langFiles2.length > 0) return -1; |
95 | 134 | if(f1.getName().equals("lang")) return f2.getName().equals("lang") ? 0 : 1; |
|
0 commit comments