| 1 | /** |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * License: LGPLv3 or later |
| 5 | * |
| 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. |
| 9 | * |
| 10 | * Copyright (C) 2015 Kevin Lamonte |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 |
| 26 | * 02110-1301 USA |
| 27 | * |
| 28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 29 | * @version 1 |
| 30 | */ |
| 31 | package jexer; |
| 32 | |
| 33 | import java.util.Date; |
| 34 | |
| 35 | /** |
| 36 | * TTimer implements a simple timer. |
| 37 | */ |
| 38 | public final class TTimer { |
| 39 | |
| 40 | /** |
| 41 | * If true, re-schedule after every tick. Note package private access. |
| 42 | */ |
| 43 | boolean recurring = false; |
| 44 | |
| 45 | /** |
| 46 | * Duration (in millis) between ticks if this is a recurring timer. |
| 47 | */ |
| 48 | private long duration = 0; |
| 49 | |
| 50 | /** |
| 51 | * The next time this timer needs to be ticked. |
| 52 | */ |
| 53 | private Date nextTick; |
| 54 | |
| 55 | /** |
| 56 | * Get the next time this timer needs to be ticked. Note package private |
| 57 | * access. |
| 58 | * |
| 59 | * @return time at which action should be called |
| 60 | */ |
| 61 | Date getNextTick() { |
| 62 | return nextTick; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * The action to perfom on a tick. |
| 67 | */ |
| 68 | private TAction action; |
| 69 | |
| 70 | /** |
| 71 | * Tick this timer. Note package private access. |
| 72 | */ |
| 73 | void tick() { |
| 74 | if (action != null) { |
| 75 | action.DO(); |
| 76 | } |
| 77 | // Set next tick |
| 78 | Date ticked = new Date(); |
| 79 | if (recurring) { |
| 80 | nextTick = new Date(ticked.getTime() + duration); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the number of milliseconds between now and the next tick time. |
| 86 | * |
| 87 | * @return number of millis |
| 88 | */ |
| 89 | public long getMillis() { |
| 90 | return nextTick.getTime() - (new Date()).getTime(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Package private constructor. |
| 95 | * |
| 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 |
| 99 | */ |
| 100 | TTimer(final long duration, final boolean recurring, final TAction action) { |
| 101 | |
| 102 | this.recurring = recurring; |
| 103 | this.duration = duration; |
| 104 | this.action = action; |
| 105 | |
| 106 | nextTick = new Date((new Date()).getTime() + duration); |
| 107 | } |
| 108 | |
| 109 | } |