0e711b967ec50ab6c1d4ac3fe612aa6573a3a7aa
2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import java
.util
.Date
;
36 * TTimer implements a simple timer.
38 public final class TTimer
{
41 * If true, re-schedule after every tick. Note package private access.
43 boolean recurring
= false;
46 * Duration (in millis) between ticks if this is a recurring timer.
48 private long duration
= 0;
51 * The next time this timer needs to be ticked.
53 private Date nextTick
;
56 * Get the next time this timer needs to be ticked. Note package private
59 * @return time at which action should be called
66 * The action to perfom on a tick.
68 private TAction action
;
71 * Tick this timer. Note package private access.
78 Date ticked
= new Date();
80 nextTick
= new Date(ticked
.getTime() + duration
);
85 * Get the number of milliseconds between now and the next tick time.
87 * @return number of millis
89 public long getMillis() {
90 return nextTick
.getTime() - (new Date()).getTime();
94 * Package private constructor.
96 * @param duration number of milliseconds to wait between ticks
97 * @param recurring if true, re-schedule this timer after every tick
98 * @param action to perform on next tick
100 TTimer(final long duration
, final boolean recurring
, final TAction action
) {
102 this.recurring
= recurring
;
103 this.duration
= duration
;
104 this.action
= action
;
106 nextTick
= new Date((new Date()).getTime() + duration
);