summaryrefslogtreecommitdiff
path: root/opendc-core/src
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-core/src')
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt20
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt6
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/TopologyListener.kt61
3 files changed, 85 insertions, 2 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 e2824f5d..ef9e0396 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
@@ -75,6 +75,11 @@ internal class AdjacencyListTopology : MutableTopology {
// Topology
/**
+ * The listeners of this topology.
+ */
+ override val listeners: MutableSet<TopologyListener> = HashSet()
+
+ /**
* A unique identifier of this node within the topology.
*/
override val Entity<*>.id: Int
@@ -109,6 +114,7 @@ internal class AdjacencyListTopology : MutableTopology {
val edge = Edge(label, tag, from, to)
from.outgoingEdges.add(edge)
to.ingoingEdges.add(edge)
+ listeners.forEach { it.run { this@AdjacencyListTopology.onEdgeAdded(edge) } }
return edge
}
@@ -156,7 +162,13 @@ internal class AdjacencyListTopology : MutableTopology {
* @param element The element to add to this graph.
* @return `true` if the graph has changed, `false` otherwise.
*/
- override fun add(element: Entity<*>): Boolean = nodes.putIfAbsent(element, Node(nextId.getAndIncrement())) == null
+ override fun add(element: Entity<*>): Boolean {
+ if (nodes.putIfAbsent(element, Node(nextId.getAndIncrement())) == null) {
+ listeners.forEach { it.run { this@AdjacencyListTopology.onNodeAdded(element) } }
+ return true
+ }
+ return false
+ }
/**
* Add all nodes in the specified collection to the graph.
@@ -184,7 +196,11 @@ internal class AdjacencyListTopology : MutableTopology {
nodes[element]?.outgoingEdges?.forEach {
it.to.ingoingEdges.remove(it)
}
- return nodes.keys.remove(element)
+ if (nodes.keys.remove(element)) {
+ listeners.forEach { it.run { this@AdjacencyListTopology.onNodeRemoved(element) } }
+ return true
+ }
+ return false
}
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 5b697bfb..27c1c889 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
@@ -34,9 +34,15 @@ package nl.atlarge.opendc.topology
*/
interface Topology : TopologyContext, Cloneable, Set<Entity<*>> {
/**
+ * The listeners of this topology.
+ */
+ val listeners: MutableSet<TopologyListener>
+
+ /**
* Create a copy of the topology.
*
* @return A new [Topology] with a copy of the graph.
*/
public override fun clone(): Topology
}
+
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/TopologyListener.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/TopologyListener.kt
new file mode 100644
index 00000000..b5062709
--- /dev/null
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/TopologyListener.kt
@@ -0,0 +1,61 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017 atlarge-research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package nl.atlarge.opendc.topology;
+
+/**
+ * A listener interface for [Topology] instances. The methods of this interface are invoked on
+ * mutation of the topology the listener is listening to.
+ *
+ * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
+ */
+interface TopologyListener {
+ /**
+ * This method is invoked when an [Entity] is added to the [Topology].
+ *
+ * @param node The entity that has been added to the [Topology].
+ */
+ fun Topology.onNodeAdded(node: Entity<*>) {}
+
+ /**
+ * This method is invoked when an [Entity] is removed from the [Topology].
+ *
+ * @param node The entity that has been removed from the [Topology].
+ */
+ fun Topology.onNodeRemoved(node: Entity<*>) {}
+
+ /**
+ * This method is invoked when an [Edge] is added to the [Topology].
+ *
+ * @param edge The edge that has been added to the [Topology].
+ */
+ fun Topology.onEdgeAdded(edge: Edge<*>) {}
+
+ /**
+ * This method is invoked when an [Edge] is removed from the [Topology].
+ *
+ * @param edge The entity that has been removed from the [Topology].
+ */
+ fun Topology.onEdgeRemoved(edge: Edge<*>) {}
+}