You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimerManager.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.time;
  23. import com.dmdirc.interfaces.WindowModel;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.Map.Entry;
  27. import java.util.Set;
  28. import javax.inject.Inject;
  29. /**
  30. * Class to manage Timers.
  31. */
  32. public class TimerManager {
  33. /** Map of all the timers that are running. */
  34. private final Map<Integer, TimedCommand> timerList = new HashMap<>();
  35. /** The timer factory to create timers with. */
  36. private final TimerFactory timerFactory;
  37. @Inject
  38. public TimerManager(final TimerFactory timerFactory) {
  39. this.timerFactory = timerFactory;
  40. }
  41. /**
  42. * Adds a timer to the internal list and starts the timer.
  43. *
  44. * @param repetitions Amount of times the timer repeats
  45. * @param interval Interval between repetitions
  46. * @param command Command to be run when the timer fires
  47. * @param origin The frame container to use for the execution
  48. */
  49. public void addTimer(final int repetitions, final int interval,
  50. final String command, final WindowModel origin) {
  51. synchronized (this) {
  52. final int timerKey = findFreeKey();
  53. final TimedCommand timedCommand = new TimedCommand(this, timerKey,
  54. repetitions, interval, command, origin);
  55. timerList.put(timerKey, timedCommand);
  56. timedCommand.schedule(timerFactory);
  57. }
  58. }
  59. /**
  60. * Removes a timer from our internal list of active timers. This should only be called when a
  61. * timer is cancelled.
  62. *
  63. * @param timerKey Key of the timer to remove
  64. */
  65. public void removeTimer(final int timerKey) {
  66. timerList.remove(timerKey);
  67. }
  68. /**
  69. * Gets a list of all current active timers.
  70. *
  71. * @return Returns a set of keys for all active timers
  72. */
  73. public Set<Entry<Integer, TimedCommand>> listTimers() {
  74. return timerList.entrySet();
  75. }
  76. /**
  77. * Returns a list of all known Timer IDs.
  78. *
  79. * @return returns an Integer Set of Timer IDs
  80. */
  81. public Set<Integer> getTimerIDs() {
  82. return timerList.keySet();
  83. }
  84. /**
  85. * Locates a key in our list that is currently not being used by a timer.
  86. *
  87. * @return Returns a key that can be used for creating a new timer
  88. */
  89. private int findFreeKey() {
  90. int i = 0;
  91. while (timerList.containsKey(i)) {
  92. i++;
  93. }
  94. return i;
  95. }
  96. /**
  97. * Matches a Timer ID to a Timer. This will return null if there is no Timer with the provided
  98. * ID.
  99. *
  100. * @param id ID that we want the timer for
  101. *
  102. * @return Returns the Timer for the ID provided or null if none exist
  103. */
  104. public TimedCommand getTimerByID(final int id) {
  105. return timerList.get(id);
  106. }
  107. /**
  108. * Checkes if there is a timer with the provided ID.
  109. *
  110. * @param id ID that we want to check if there is a timer for
  111. *
  112. * @return Returns True iif there is a timer with the ID
  113. */
  114. public boolean hasTimerWithID(final int id) {
  115. return timerList.containsKey(id);
  116. }
  117. }