Skip to content

Commit 6df9ff3

Browse files
committed
chg: add a FilteredClockListener that trigger an existing ClockListener at times defined by a method
1 parent ff6f7a6 commit 6df9ff3

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2008 - 2020, Arnaud Casteigts and the JBotSim contributors <contact@jbotsim.io>
3+
*
4+
*
5+
* This file is part of JBotSim.
6+
*
7+
* JBotSim is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* JBotSim is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with JBotSim. If not, see <https://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
package io.jbotsim.core.event;
23+
24+
import io.jbotsim.core.Topology;
25+
26+
public abstract class FilteredClockListener implements ClockListener {
27+
private Topology topology;
28+
private ClockListener listener;
29+
30+
public FilteredClockListener(Topology tp, ClockListener listener) {
31+
this.topology = tp;
32+
this.listener = listener;
33+
}
34+
35+
@Override
36+
public void onClock() {
37+
if (shouldTriggerListener(topology, topology.getTime()))
38+
listener.onClock();
39+
}
40+
41+
protected abstract boolean shouldTriggerListener(Topology tp, int currentTime);
42+
}

0 commit comments

Comments
 (0)