mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-25 04:17:59 +01:00
Add Network.Builder class
This commit is contained in:
parent
80a300b43e
commit
16ec6bc5e4
1 changed files with 46 additions and 0 deletions
|
@ -26,6 +26,11 @@ class Network(
|
||||||
fun empty(referenceTime: ReferenceTime): Network {
|
fun empty(referenceTime: ReferenceTime): Network {
|
||||||
return Network(HashMap(), HashMap(), HashMap(), referenceTime)
|
return Network(HashMap(), HashMap(), HashMap(), referenceTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun builder(): Builder {
|
||||||
|
return Builder()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -60,4 +65,45 @@ class Network(
|
||||||
}
|
}
|
||||||
return sb.toString()
|
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