From 16ec6bc5e4a11bd968d60886a99e2876eafed0eb Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 6 Jul 2023 02:03:47 +0200 Subject: [PATCH] Add Network.Builder class --- .../org/pgpainless/wot/dijkstra/sq/Network.kt | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Network.kt b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Network.kt index 72ed83da..7952f102 100644 --- a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Network.kt +++ b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Network.kt @@ -26,6 +26,11 @@ class Network( fun empty(referenceTime: ReferenceTime): Network { return Network(HashMap(), HashMap(), HashMap(), referenceTime) } + + @JvmStatic + fun builder(): Builder { + return Builder() + } } /** @@ -60,4 +65,45 @@ class Network( } return sb.toString() } + + class Builder internal constructor() { + private val nodes: MutableMap = mutableMapOf() + private val protoEdges: MutableMap, CertificationSet> = mutableMapOf() + private var referenceTime: ReferenceTime = ReferenceTime.now() + + fun addNode(node: CertSynopsis) { + nodes[node.fingerprint] = node + } + + fun getNode(fingerprint: Fingerprint): CertSynopsis? { + return nodes[fingerprint] + } + + fun addEdge(edge: Certification) { + protoEdges.getOrPut(Pair(edge.issuer.fingerprint, edge.target.fingerprint)) { + CertificationSet.empty(edge.issuer, edge.target) + }.add(edge) + } + + fun setReferenceTime(time: ReferenceTime) { + this.referenceTime = time + } + + fun build(): Network { + val edges = mutableMapOf>() + val revEdges = mutableMapOf>() + + protoEdges.forEach { (pair, certificationSet) -> + edges.getOrPut(pair.first) { + mutableListOf() + }.add(certificationSet) + + revEdges.getOrPut(pair.second) { + mutableListOf() + }.add(certificationSet) + } + + return Network(nodes, edges, revEdges, referenceTime) + } + } } \ No newline at end of file