From 482a4e93699cf8725da3505f7b883ca875210c63 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Tue, 5 Sep 2017 23:25:52 +0200 Subject: Provide Set interface for Topology This change makes the Topology interface implement the Set interface to provide common methods like checking whether a node is part of the given topology. --- .../nl/atlarge/opendc/topology/AdjacencyList.kt | 24 ++++++++++++++++++++-- .../kotlin/nl/atlarge/opendc/topology/Topology.kt | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'opendc-core') diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt index bdf60056..50f8fa0c 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt @@ -47,6 +47,26 @@ internal class AdjacencyListTopology: MutableTopology { private val nextId: AtomicInteger = AtomicInteger(0) private val nodes: MutableList> = ArrayList() + /** + * Returns the size of the collection. + */ + override val size: Int = nodes.size + + /** + * Checks if the specified element is contained in this collection. + */ + override fun contains(element: Node<*>): Boolean = nodes.contains(element) + + /** + * Checks if all elements in the specified collection are contained in this collection. + */ + override fun containsAll(elements: Collection>): Boolean = nodes.containsAll(elements) + + /** + * Returns `true` if the collection is empty (contains no elements), `false` otherwise. + */ + override fun isEmpty(): Boolean = nodes.isEmpty() + /** * Create a [Node] in this [Topology] for the given [Entity]. * @@ -54,7 +74,7 @@ internal class AdjacencyListTopology: MutableTopology { * @return The node created for the given entity. */ override fun > node(entity: T): Node { - val node = AdjacencyListNode(nextId.incrementAndGet(), entity) + val node = AdjacencyListNode(nextId.getAndIncrement(), entity) nodes.add(node) return node } @@ -82,7 +102,7 @@ internal class AdjacencyListTopology: MutableTopology { /** * Returns an iterator over the elements of this object. */ - override fun iterator(): Iterator> = nodes.iterator() + override fun iterator(): MutableIterator> = nodes.iterator() internal inner class AdjacencyListNode>(override val id: Int, override val label: T): Node { internal var ingoingEdges: MutableSet> = HashSet() diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt index d8f966d1..5d88334c 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt @@ -32,4 +32,4 @@ package nl.atlarge.opendc.topology * * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -interface Topology: Iterable> +interface Topology: Set> -- cgit v1.2.3