Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delayed listener #91

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,8 +35,7 @@ public class ClockManager {
final static int CLOCK_INITIAL_VALUE = 0;

Topology tp;
HashMap<ClockListener, Integer> listeners = new HashMap<>();
HashMap<ClockListener, Integer> countdown = new HashMap<>();
HashMap<ClockListener, ClockListener> listeners = new HashMap<>();
Class<? extends Clock> clockModel = null;
Clock clock = null;
int time = CLOCK_INITIAL_VALUE;
Expand Down Expand Up @@ -65,14 +65,7 @@ private void incrementTime() {
}

private void callScheduler() {
List<ClockListener> 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<ClockListener> expiredListeners = new ArrayList<>(listeners.values());
tp.getScheduler().onClock(tp, expiredListeners);
}

Expand Down Expand Up @@ -109,8 +102,7 @@ public void setClockModel(Class<? extends Clock> 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));
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -131,7 +122,6 @@ public void addClockListener(ClockListener listener) {
*/
public void removeClockListener(ClockListener listener) {
listeners.remove(listener);
countdown.remove(listener);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2008 - 2020, Arnaud Casteigts and the JBotSim contributors <contact@jbotsim.io>
*
*
* 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 <https://www.gnu.org/licenses/>.
*
*/

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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2008 - 2020, Arnaud Casteigts and the JBotSim contributors <contact@jbotsim.io>
*
*
* 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 <https://www.gnu.org/licenses/>.
*
*/

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);
}
}