diff options
Diffstat (limited to 'lib/src/main/kotlin/PathInfo.kt')
-rw-r--r-- | lib/src/main/kotlin/PathInfo.kt | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/lib/src/main/kotlin/PathInfo.kt b/lib/src/main/kotlin/PathInfo.kt new file mode 100644 index 0000000..2aee406 --- /dev/null +++ b/lib/src/main/kotlin/PathInfo.kt @@ -0,0 +1,93 @@ +/** + * This class holds all info about paths, their FIDs, and their QIDs. + */ +class PathInfo() { + private val paths: MutableSet<Path> = mutableSetOf() + + /** + * Information about a path. Each FID maps to one path but not all paths have FIDs (injective). On the other hand, + * one QID can be shared with different paths and FIDs. + * + * FIDs are optional because some R-messages only return QIDs for certain files (e.g. walk). + * + * @param path The path. + * @param fid The optional FID associated with the path. + * @param qid The QID associated with the path. + */ + data class Path(val path: List<String>, val fid: UInt?, val qid: QID) + + /** + * Add a path. + */ + fun addPath(path: Path) { + this.paths.add(path) + } + + /** + * Add a QID to an existing FID. Do nothing if the given FID does not exist. + */ + fun addQIDToFID(fid: UInt, qid: QID) { + findByFID(fid)?.qid = qid + } + + /** + * Remove a FID. + */ + fun removeFID(fid: UInt) { + this.paths.removeIf { x -> x.fid == fid } + } + + fun find(predicate: (Path) -> Boolean) = this.paths.find(predicate) + + /** + * Find [Path] object by path. + * + * @param path The path to search for. + * @return A path object if [path] exists, or null otherwise. + */ + fun findByPath(path: List<String>): Path? { + return this.paths.find { x -> x.path == path } + } + + /** + * Find [Path] object by FID. + * + * @param fid The FID to search for. + * @return A path object if [fid] exists, or null otherwise. + */ + fun findByFID(fid: UInt): Path? { + return this.paths.find { x -> x.fid == fid } + } + + /** + * Find [Path] object by QID. + * + * @param qid The path to search for. + * @return A path object if [qid] exists, or null otherwise. + */ + fun findByQID(qid: QID): Path? { + return this.paths.find { x -> x.qid == qid } + } + + /** + * Retrieve all paths + */ + fun getAllPaths(): Set<Path> { + return this.paths.toSet() + } + + /** + * Generate a new FID which is not already in use. + * + * @return The new FID. + * @throws IllegalStateException if there is no available FID. + */ + fun genFID(): UInt { + for (newFid in 0u..UInt.MAX_VALUE) { + if (findByFID(newFid) == null) { + return newFid + } + } + throw IllegalStateException() + } +}
\ No newline at end of file |