Initial commit
This commit is contained in:
25
src/main/kotlin/filesystem/FSCreator.kt
Normal file
25
src/main/kotlin/filesystem/FSCreator.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package filesystem
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
|
||||
class FSCreator {
|
||||
fun create(entryToCreate: FSEntry, destination: String) {
|
||||
val queue = ArrayDeque<Pair<FSEntry, Path>>()
|
||||
queue.add(entryToCreate to Path.of(destination))
|
||||
|
||||
while (queue.isNotEmpty()) {
|
||||
val (entry, dest) = queue.removeFirst()
|
||||
val path = dest.resolve(entry.name)
|
||||
try {
|
||||
when (entry) {
|
||||
is FSFile -> Files.createFile(path)
|
||||
is FSFolder -> Files.createDirectory(path)
|
||||
}
|
||||
} catch (_: FileAlreadyExistsException) {} // Allow files/folders to already exist.
|
||||
if (entry is FSFolder) {
|
||||
queue.addAll(entry.entries.map { it to path })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
src/main/kotlin/filesystem/FSEntry.kt
Normal file
9
src/main/kotlin/filesystem/FSEntry.kt
Normal file
@@ -0,0 +1,9 @@
|
||||
package filesystem
|
||||
|
||||
// Note sealed allows for simpler logic in FSCreator by guaranteeing FSFile and FSFolder are the only possible FSEntries
|
||||
// (as we expect), and it also makes the class abstract as required.
|
||||
sealed class FSEntry(val name: String)
|
||||
|
||||
class FSFile(name: String, val content: String): FSEntry(name)
|
||||
|
||||
class FSFolder(name: String, val entries: List<FSEntry>): FSEntry(name)
|
Reference in New Issue
Block a user