1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-05 03:55:58 +01:00

Add Network.Builder class

This commit is contained in:
Paul Schaub 2023-07-06 02:03:47 +02:00
parent 6f4b7f9344
commit 34f7f7123f
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -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<Fingerprint, CertSynopsis> = mutableMapOf()
private val protoEdges: MutableMap<Pair<Fingerprint, Fingerprint>, 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<Fingerprint, MutableList<CertificationSet>>()
val revEdges = mutableMapOf<Fingerprint, MutableList<CertificationSet>>()
protoEdges.forEach { (pair, certificationSet) ->
edges.getOrPut(pair.first) {
mutableListOf()
}.add(certificationSet)
revEdges.getOrPut(pair.second) {
mutableListOf()
}.add(certificationSet)
}
return Network(nodes, edges, revEdges, referenceTime)
}
}
}