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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.conditional_execute;
  18. import java.text.DateFormat;
  19. import java.text.SimpleDateFormat;
  20. /**
  21. * Class representing a ConditionalExecute Namespace.
  22. */
  23. public class ConditionalExecuteNamespace {
  24. /** Name of this namespace. */
  25. private final String name;
  26. /** Are we currently inhibited? */
  27. private boolean inhibited = false;
  28. /** Are we currently forced? */
  29. private boolean forced = false;
  30. /** What is the current limit time? */
  31. private long limitTime = 0L;
  32. /**
  33. * Create a new ConditionalExecuteNamespace.
  34. *
  35. * @param name Name of this Namespace
  36. */
  37. public ConditionalExecuteNamespace(final String name) {
  38. this.name = name;
  39. }
  40. /**
  41. * Can a command in this namespace be run?
  42. *
  43. * This checks as follows: - Is the namespace inhibited? - return False - Is the namespace in
  44. * "force" mode? - return True - If inverse is true, are we under the limit time? return True -
  45. * If inverse is false, are we over the limit time? return True - return False
  46. *
  47. * @param inverse Check if we are *before* the limit time rather than after?
  48. *
  49. * @return True if the command can be run.
  50. */
  51. public boolean canRun(final boolean inverse) {
  52. return !isInhibited() && (isForced() || inverse == System.currentTimeMillis() < limitTime);
  53. }
  54. /** Inhibit this namespace. */
  55. public void inhibit() {
  56. this.inhibited = true;
  57. this.forced = false;
  58. }
  59. /** Force this namespace. */
  60. public void force() {
  61. this.inhibited = false;
  62. this.forced = true;
  63. }
  64. /** UnForce and Uninhibit this namespace. */
  65. public void reset() {
  66. this.inhibited = false;
  67. this.forced = false;
  68. }
  69. /**
  70. * Change the limit time on this namespace.
  71. *
  72. * @param difference Change (in milliseconds) to make to the limit time.
  73. *
  74. * @return New limit time.
  75. */
  76. public long changeLimit(final long difference) {
  77. this.limitTime += difference;
  78. return this.limitTime;
  79. }
  80. /**
  81. * Set the limit time on this namespace.
  82. *
  83. * @param time new Limit time (in milliseconds)
  84. *
  85. * @return New limit time.
  86. */
  87. public long setLimit(final long time) {
  88. this.limitTime = time;
  89. return this.limitTime;
  90. }
  91. /**
  92. * Are we currently forced?
  93. *
  94. * @return Are we currently forced?
  95. */
  96. public boolean isForced() {
  97. return forced;
  98. }
  99. /**
  100. * Are we currently inhibited?
  101. *
  102. * @return Are we currently inhibited?
  103. */
  104. public boolean isInhibited() {
  105. return inhibited;
  106. }
  107. /**
  108. * Current limit time.
  109. *
  110. * @return The current limit time.
  111. */
  112. public long getLimitTime() {
  113. return limitTime;
  114. }
  115. /**
  116. * Get the name of this namespace.
  117. *
  118. * @return The name of this namespace.
  119. */
  120. public String getName() {
  121. return name;
  122. }
  123. @Override
  124. public String toString() {
  125. final StringBuilder sb = new StringBuilder(getName());
  126. if (isForced()) {
  127. sb.append(" - Forced");
  128. } else if (isInhibited()) {
  129. sb.append(" - Inhibited");
  130. } else {
  131. sb.append(" - Limit Time: ");
  132. final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  133. sb.append(dateFormat.format(limitTime));
  134. }
  135. if (canRun(false)) {
  136. sb.append(" [Can Run]");
  137. }
  138. if (canRun(true)) {
  139. sb.append(" [Can Run Inverse]");
  140. }
  141. return sb.toString();
  142. }
  143. }