diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2017-09-05 23:25:52 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2017-09-05 23:27:31 +0200 |
| commit | 482a4e93699cf8725da3505f7b883ca875210c63 (patch) | |
| tree | b821ec64052acae5609121cf745576718e007256 /opendc-core | |
| parent | 96f75806dbdcb8c43a22c7db98e85ac5e854e68b (diff) | |
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.
Diffstat (limited to 'opendc-core')
| -rw-r--r-- | opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt | 24 | ||||
| -rw-r--r-- | opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt | 2 |
2 files changed, 23 insertions, 3 deletions
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 @@ -48,13 +48,33 @@ internal class AdjacencyListTopology: MutableTopology { private val nodes: MutableList<Node<*>> = 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<Node<*>>): 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]. * * @param entity The entity to create a node for. * @return The node created for the given entity. */ override fun <T : Entity<*>> node(entity: T): Node<T> { - 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<Node<*>> = nodes.iterator() + override fun iterator(): MutableIterator<Node<*>> = nodes.iterator() internal inner class AdjacencyListNode<out T: Entity<*>>(override val id: Int, override val label: T): Node<T> { internal var ingoingEdges: MutableSet<Edge<*>> = 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<Node<*>> +interface Topology: Set<Node<*>> |
