Initial commit

This commit is contained in:
2023-12-03 01:24:30 +00:00
commit 9489e6048c
18 changed files with 521 additions and 0 deletions

View 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 })
}
}
}
}

View 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)