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:
parent
6f4b7f9344
commit
34f7f7123f
1 changed files with 46 additions and 0 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue