Gridarta Editor
DelayedChangeManager.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils;
21 
22 import java.util.Timer;
23 import java.util.TimerTask;
24 import org.jetbrains.annotations.NotNull;
25 
32 public class DelayedChangeManager {
33 
39  private final int initialDelay;
40 
45  private final int notificationDelay;
46 
50  @NotNull
52 
56  @NotNull
57  private final Timer timer = new Timer();
58 
63  private class TimeoutTimerTask extends TimerTask {
64 
65  @Override
66  public void run() {
67  state = state.timeout();
68  }
69 
70  }
71 
75  private interface State {
76 
81  @NotNull
82  State change();
83 
88  @NotNull
89  State finish();
90 
95  @NotNull
96  State timeout();
97 
98  }
99 
103  @NotNull
104  private final State idle = new State() {
105 
106  @NotNull
107  @Override
108  public State change() {
109  timer.schedule(new TimeoutTimerTask(), initialDelay);
110  return pending;
111  }
112 
113  @NotNull
114  @Override
115  public State finish() {
116  return idle;
117  }
118 
119  @NotNull
120  @Override
121  public State timeout() {
122  assert false;
123  return idle;
124  }
125 
126  };
127 
131  @NotNull
132  private final State pending = new State() {
133 
134  @NotNull
135  @Override
136  public State change() {
137  return pending;
138  }
139 
140  @NotNull
141  @Override
142  public State finish() {
144  return wait;
145  }
146 
147  @NotNull
148  @Override
149  public State timeout() {
150  timer.schedule(new TimeoutTimerTask(), notificationDelay);
152  return wait;
153  }
154 
155  };
156 
160  @NotNull
161  private final State wait = new State() {
162 
163  @NotNull
164  @Override
165  public State change() {
166  return pending;
167  }
168 
169  @NotNull
170  @Override
171  public State finish() {
172  return wait;
173  }
174 
175  @NotNull
176  @Override
177  public State timeout() {
178  return idle;
179 
180  }
181 
182  };
183 
187  @NotNull
188  private State state = idle;
189 
200  this.initialDelay = initialDelay;
201  this.notificationDelay = notificationDelay;
202  this.delayedChangeListener = delayedChangeListener;
203  }
204 
208  public void change() {
209  state = state.change();
210  }
211 
218  public void finish() {
219  state = state.finish();
220  }
221 
222 }
net.sf.gridarta.utils.DelayedChangeManager.notificationDelay
final int notificationDelay
The interval in milliseconds events are forwarded during a long series of events.
Definition: DelayedChangeManager.java:45
net.sf.gridarta.utils.DelayedChangeManager.initialDelay
final int initialDelay
The delay in millisecond the first forwarded event is delayed.
Definition: DelayedChangeManager.java:39
net.sf.gridarta.utils.DelayedChangeListener.change
void change()
Called for forwarded change events.
net.sf.gridarta.utils.DelayedChangeManager.TimeoutTimerTask.run
void run()
Definition: DelayedChangeManager.java:66
net.sf.gridarta.utils.DelayedChangeManager.delayedChangeListener
final DelayedChangeListener delayedChangeListener
The DelayedChangeListener events are forwarded to.
Definition: DelayedChangeManager.java:51
net.sf.gridarta.utils.DelayedChangeManager.idle
final State idle
The state "idle" of the FSM.
Definition: DelayedChangeManager.java:104
net.sf.gridarta.utils.DelayedChangeManager.state
State state
The FSM's current state.
Definition: DelayedChangeManager.java:188
net.sf.gridarta.utils.DelayedChangeManager.State.finish
State finish()
Executes the event "finish".
net.sf.gridarta.utils.DelayedChangeManager.State.change
State change()
Executes the event "change".
net.sf.gridarta.utils.DelayedChangeManager.State.timeout
State timeout()
Executes the event "timeout".
net.sf.gridarta.utils.DelayedChangeManager.pending
final State pending
The state "pending" of the FSM.
Definition: DelayedChangeManager.java:132
net.sf.gridarta.utils.DelayedChangeManager.timer
final Timer timer
The Timer for delaying events.
Definition: DelayedChangeManager.java:57
net.sf.gridarta.utils.DelayedChangeManager.State
The states of the FSM.
Definition: DelayedChangeManager.java:75
net.sf.gridarta.utils.DelayedChangeListener
Listener for forwarded events of DelayedChangeManager.
Definition: DelayedChangeListener.java:26
net.sf.gridarta.utils.DelayedChangeManager.change
void change()
Delivers a change event to the associated DelayedChangeListener.
Definition: DelayedChangeManager.java:208
net.sf.gridarta.utils.DelayedChangeManager.TimeoutTimerTask
A TimerTask that calls State#timeout() when the timer expires.
Definition: DelayedChangeManager.java:63
net.sf.gridarta.utils.DelayedChangeManager.finish
void finish()
Finishes a series of change events.
Definition: DelayedChangeManager.java:218
net.sf.gridarta.utils.DelayedChangeManager
Helper for reducing the number of change events: calls to change() will be forwarded to DelayedChange...
Definition: DelayedChangeManager.java:32
net.sf.gridarta.utils.DelayedChangeManager.DelayedChangeManager
DelayedChangeManager(final int initialDelay, final int notificationDelay, @NotNull final DelayedChangeListener delayedChangeListener)
Creates a new instance.
Definition: DelayedChangeManager.java:199
net.sf.gridarta.utils.DelayedChangeManager.wait
final State wait
The state "wait" of the FSM.
Definition: DelayedChangeManager.java:161