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-2015 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() {
143  delayedChangeListener.change();
144  return wait;
145  }
146 
147  @NotNull
148  @Override
149  public State timeout() {
150  timer.schedule(new TimeoutTimerTask(), notificationDelay);
151  delayedChangeListener.change();
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 
199  public DelayedChangeManager(final int initialDelay, final int notificationDelay, @NotNull final DelayedChangeListener delayedChangeListener) {
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 }
DelayedChangeManager(final int initialDelay, final int notificationDelay, @NotNull final DelayedChangeListener delayedChangeListener)
Creates a new instance.
Helper for reducing the number of change events: calls to change() will be forwarded to DelayedChange...
final State pending
The state "pending" of the FSM.
void finish()
Finishes a series of change events.
State state
The FSM's current state.
void change()
Called for forwarded change events.
A TimerTask that calls State#timeout() when the timer expires.
Listener for forwarded events of DelayedChangeManager.
final DelayedChangeListener delayedChangeListener
The DelayedChangeListener events are forwarded to.
final int initialDelay
The delay in millisecond the first forwarded event is delayed.
final int notificationDelay
The interval in milliseconds events are forwarded during a long series of events. ...
final State wait
The state "wait" of the FSM.
State finish()
Executes the event "finish".
State change()
Executes the event "change".
final Timer timer
The Timer for delaying events.
void change()
Delivers a change event to the associated DelayedChangeListener.
final State idle
The state "idle" of the FSM.
State timeout()
Executes the event "timeout".