-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemHolder.kt
More file actions
38 lines (33 loc) · 1.61 KB
/
ProblemHolder.kt
File metadata and controls
38 lines (33 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package problems
import components.Problem
import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.util.jar.JarFile
import java.util.stream.Collectors
class ProblemHolder {
companion object {
val problems = readProblems()
private fun readProblems() : Map<Int,List<Problem>> =
try {
// Try if we are in JAR file
JarFile(File(ProblemHolder::class.java.protectionDomain.codeSource.location.toURI().path)).stream()
.map { it.toString() }
.filter { it.startsWith("problems/") && it.endsWith(".class") }
.map { it.substring(0, it.length - 6).replace("/".toRegex(), ".") }
} catch (e: IOException) {
// But maybe we are in .class file
val path: Path = File(ProblemHolder::class.java.protectionDomain.codeSource.location.toURI().path).toPath()
Files.walk(File("$path\\problems\\").toPath())
.map { it.toString() }
.filter { it.endsWith(".class") }
.map { it.substring(path.toString().length + 1, it.length - 6).replace("\\\\".toRegex(), ".") }
}
.map { Class.forName(it) }
.filter { it.interfaces.contains(Problem::class.java) }
.map { it.getConstructor().newInstance() as Problem }
.collect(Collectors.toMap({ it.id }, { mutableListOf(it) }, { list1, list2 -> list1.addAll(list2); list1 }))
.toSortedMap(Comparator.naturalOrder<Int>().reversed())
}
}