From 9515eb09341fa43ad580130763e2bed3609ea847 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Thu, 6 Jul 2023 19:37:12 +0200 Subject: [PATCH] Implement Root, Roots --- .../org/pgpainless/wot/dijkstra/sq/Root.kt | 10 ++++ .../org/pgpainless/wot/dijkstra/sq/Roots.kt | 48 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Root.kt create mode 100644 wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Roots.kt diff --git a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Root.kt b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Root.kt new file mode 100644 index 00000000..0a458322 --- /dev/null +++ b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Root.kt @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2023 Heiko Schaefer +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.wot.dijkstra.sq + +data class Root(val fingerprint: Fingerprint, val amount: Int) { + + override fun toString() = "$fingerprint [$amount]" +} \ No newline at end of file diff --git a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Roots.kt b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Roots.kt new file mode 100644 index 00000000..41c25b52 --- /dev/null +++ b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/dijkstra/sq/Roots.kt @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2023 Heiko Schaefer +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.wot.dijkstra.sq + +/** + * A set of `Root`s (that can be used as the basis for authentication lookups). + */ +class Roots { + + // Map for efficient lookup by Fingerprint + private val roots: Map + + constructor(roots: List) { + this.roots = roots.associateBy { it.fingerprint } + } + + constructor() : this(listOf()) + + /** + * Returns the specified root. + */ + fun get(fpr: Fingerprint): Root? = roots[fpr] + + /** + * Check if `fpr` is contained in this set of roots. + */ + fun isRoot(fpr: Fingerprint) = roots.containsKey(fpr) + + /** + * The set of fingerprints of all roots. + */ + fun fingerprints() = roots.keys + + /** + * A collection of all roots. + */ + fun roots() = roots.values + + /** + * The number of roots + */ + fun size() = roots.size + + override fun toString() = roots.keys.sorted().joinToString(", ") + +}