From 6df9ff387a21b5b049e211e098acbd5255eec9a3 Mon Sep 17 00:00:00 2001 From: Gerald Point Date: Fri, 17 Jan 2020 08:35:38 +0100 Subject: [PATCH 1/3] chg: add a FilteredClockListener that trigger an existing ClockListener at times defined by a method --- .../core/event/FilteredClockListener.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/jbotsim-core/src/main/java/io/jbotsim/core/event/FilteredClockListener.java diff --git a/lib/jbotsim-core/src/main/java/io/jbotsim/core/event/FilteredClockListener.java b/lib/jbotsim-core/src/main/java/io/jbotsim/core/event/FilteredClockListener.java new file mode 100644 index 00000000..e4c8977c --- /dev/null +++ b/lib/jbotsim-core/src/main/java/io/jbotsim/core/event/FilteredClockListener.java @@ -0,0 +1,42 @@ +/* + * Copyright 2008 - 2020, Arnaud Casteigts and the JBotSim contributors + * + * + * This file is part of JBotSim. + * + * JBotSim is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * JBotSim is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with JBotSim. If not, see . + * + */ + +package io.jbotsim.core.event; + +import io.jbotsim.core.Topology; + +public abstract class FilteredClockListener implements ClockListener { + private Topology topology; + private ClockListener listener; + + public FilteredClockListener(Topology tp, ClockListener listener) { + this.topology = tp; + this.listener = listener; + } + + @Override + public void onClock() { + if (shouldTriggerListener(topology, topology.getTime())) + listener.onClock(); + } + + protected abstract boolean shouldTriggerListener(Topology tp, int currentTime); +} From 16d793d9d9b21092c055dde8ae435cfe0bcdb608 Mon Sep 17 00:00:00 2001 From: Gerald Point Date: Fri, 17 Jan 2020 08:37:40 +0100 Subject: [PATCH 2/3] chg: add a PeriodicClockListener that trigger a ClockListener at periodic times --- .../core/event/PeriodicClockListener.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/jbotsim-core/src/main/java/io/jbotsim/core/event/PeriodicClockListener.java diff --git a/lib/jbotsim-core/src/main/java/io/jbotsim/core/event/PeriodicClockListener.java b/lib/jbotsim-core/src/main/java/io/jbotsim/core/event/PeriodicClockListener.java new file mode 100644 index 00000000..c57bd6e2 --- /dev/null +++ b/lib/jbotsim-core/src/main/java/io/jbotsim/core/event/PeriodicClockListener.java @@ -0,0 +1,38 @@ +/* + * Copyright 2008 - 2020, Arnaud Casteigts and the JBotSim contributors + * + * + * This file is part of JBotSim. + * + * JBotSim is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * JBotSim is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with JBotSim. If not, see . + * + */ + +package io.jbotsim.core.event; + +import io.jbotsim.core.Topology; + +public class PeriodicClockListener extends FilteredClockListener { + private int period; + + public PeriodicClockListener(Topology tp, ClockListener listener, int period) { + super(tp, listener); + this.period = period; + } + + @Override + protected boolean shouldTriggerListener(Topology tp, int currentTime) { + return ((currentTime + 1) % period == 0); + } +} From 383656db41bff3b05eb3ad4e9a50d50acedd919b Mon Sep 17 00:00:00 2001 From: Gerald Point Date: Fri, 17 Jan 2020 08:39:12 +0100 Subject: [PATCH 3/3] chg: replace countdown table with PeriodicClockListeners. --- .../java/io/jbotsim/core/ClockManager.java | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/lib/jbotsim-core/src/main/java/io/jbotsim/core/ClockManager.java b/lib/jbotsim-core/src/main/java/io/jbotsim/core/ClockManager.java index e7668f00..e51cdb54 100644 --- a/lib/jbotsim-core/src/main/java/io/jbotsim/core/ClockManager.java +++ b/lib/jbotsim-core/src/main/java/io/jbotsim/core/ClockManager.java @@ -21,6 +21,7 @@ package io.jbotsim.core; import io.jbotsim.core.event.ClockListener; +import io.jbotsim.core.event.PeriodicClockListener; import java.lang.reflect.Constructor; import java.util.ArrayList; @@ -34,8 +35,7 @@ public class ClockManager { final static int CLOCK_INITIAL_VALUE = 0; Topology tp; - HashMap listeners = new HashMap<>(); - HashMap countdown = new HashMap<>(); + HashMap listeners = new HashMap<>(); Class clockModel = null; Clock clock = null; int time = CLOCK_INITIAL_VALUE; @@ -65,14 +65,7 @@ private void incrementTime() { } private void callScheduler() { - List expiredListeners = new ArrayList<>(); - for (ClockListener cl : listeners.keySet()) { - countdown.put(cl, countdown.get(cl) - 1); - if (countdown.get(cl) == 0) { - expiredListeners.add(cl); - countdown.put(cl, listeners.get(cl)); - } - } + List expiredListeners = new ArrayList<>(listeners.values()); tp.getScheduler().onClock(tp, expiredListeners); } @@ -109,8 +102,7 @@ public void setClockModel(Class clockModel) { * in time units. */ public void addClockListener(ClockListener listener, int period) { - listeners.put(listener, period); - countdown.put(listener, period); + listeners.put(listener, new PeriodicClockListener(tp, listener, period)); } /** @@ -119,8 +111,7 @@ public void addClockListener(ClockListener listener, int period) { * @param listener The listener to register. */ public void addClockListener(ClockListener listener) { - listeners.put(listener, 1); - countdown.put(listener, 1); + listeners.put(listener, listener); } /** @@ -131,7 +122,6 @@ public void addClockListener(ClockListener listener) { */ public void removeClockListener(ClockListener listener) { listeners.remove(listener); - countdown.remove(listener); } /**