summaryrefslogtreecommitdiff
path: root/lib/src/main/kotlin/PathInfo.kt
blob: 32022bacf12a4c3ae07226dd9f4f4ae6d264908f (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
/**
 * 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()
    }
}
*/