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.

ConditionalExecuteNamespace.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2006-2014 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.conditional_execute;
  23. import java.text.DateFormat;
  24. import java.text.SimpleDateFormat;
  25. /**
  26. * Class representing a ConditionalExecute Namespace.
  27. */
  28. public class ConditionalExecuteNamespace {
  29. /** Name of this namespace. */
  30. private final String name;
  31. /** Are we currently inhibited? */
  32. private boolean inhibited = false;
  33. /** Are we currently forced? */
  34. private boolean forced = false;
  35. /** What is the current limit time? */
  36. private long limitTime = 0L;
  37. /**
  38. * Create a new ConditionalExecuteNamespace.
  39. *
  40. * @param name Name of this Namespace
  41. */
  42. public ConditionalExecuteNamespace(final String name) {
  43. this.name = name;
  44. }
  45. /**
  46. * Can a command in this namespace be run?
  47. *
  48. * This checks as follows: - Is the namespace inhibited? - return False - Is the namespace in
  49. * "force" mode? - return True - If inverse is true, are we under the limit time? return True -
  50. * If inverse is false, are we over the limit time? return True - return False
  51. *
  52. * @param inverse Check if we are *before* the limit time rather than after?
  53. *
  54. * @return True if the command can be run.
  55. */
  56. public boolean canRun(final boolean inverse) {
  57. return !isInhibited() && (isForced() || inverse == System.currentTimeMillis() < limitTime);
  58. }
  59. /** Inhibit this namespace. */
  60. public void inhibit() {
  61. this.inhibited = true;
  62. this.forced = false;
  63. }
  64. /** Force this namespace. */
  65. public void force() {
  66. this.inhibited = false;
  67. this.forced = true;
  68. }
  69. /** UnForce and Uninhibit this namespace. */
  70. public void reset() {
  71. this.inhibited = false;
  72. this.forced = false;
  73. }
  74. /**
  75. * Change the limit time on this namespace.
  76. *
  77. * @param difference Change (in milliseconds) to make to the limit time.
  78. *
  79. * @return New limit time.
  80. */
  81. public long changeLimit(final long difference) {
  82. this.limitTime += difference;
  83. return this.limitTime;
  84. }
  85. /**
  86. * Set the limit time on this namespace.
  87. *
  88. * @param time new Limit time (in milliseconds)
  89. *
  90. * @return New limit time.
  91. */
  92. public long setLimit(final long time) {
  93. this.limitTime = time;
  94. return this.limitTime;
  95. }
  96. /**
  97. * Are we currently forced?
  98. *
  99. * @return Are we currently forced?
  100. */
  101. public boolean isForced() {
  102. return forced;
  103. }
  104. /**
  105. * Are we currently inhibited?
  106. *
  107. * @return Are we currently inhibited?
  108. */
  109. public boolean isInhibited() {
  110. return inhibited;
  111. }
  112. /**
  113. * Current limit time.
  114. *
  115. * @return The current limit time.
  116. */
  117. public long getLimitTime() {
  118. return limitTime;
  119. }
  120. /**
  121. * Get the name of this namespace.
  122. *
  123. * @return The name of this namespace.
  124. */
  125. public String getName() {
  126. return name;
  127. }
  128. @Override
  129. public String toString() {
  130. final StringBuilder sb = new StringBuilder(getName());
  131. if (isForced()) {
  132. sb.append(" - Forced");
  133. } else if (isInhibited()) {
  134. sb.append(" - Inhibited");
  135. } else {
  136. sb.append(" - Limit Time: ");
  137. final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  138. sb.append(dateFormat.format(limitTime));
  139. }
  140. if (canRun(false)) {
  141. sb.append(" [Can Run]");
  142. }
  143. if (canRun(true)) {
  144. sb.append(" [Can Run Inverse]");
  145. }
  146. return sb.toString();
  147. }
  148. }