Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ActionCondition.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.actions;
  23. import com.dmdirc.interfaces.actions.ActionComparison;
  24. import com.dmdirc.interfaces.actions.ActionComponent;
  25. /**
  26. * An action condition represents one condition within an action.
  27. */
  28. public class ActionCondition {
  29. /** The argument number that this action condition applies to. */
  30. private int arg;
  31. /** The component that this action condition applies to. */
  32. private ActionComponent component;
  33. /** The comparison that should be used for this condition. */
  34. private ActionComparison comparison;
  35. /** The target of the comparison for this condition. */
  36. private String target = "";
  37. /** The source target for this comparison. */
  38. private String starget = "";
  39. /**
  40. * Creates a new instance of ActionCondition that compares the output of a component to a
  41. * string.
  42. *
  43. * @param arg The argument number to be tested
  44. * @param component The component to be tested
  45. * @param comparison The comparison to be used
  46. * @param target The target of the comparison
  47. */
  48. public ActionCondition(final int arg, final ActionComponent component,
  49. final ActionComparison comparison, final String target) {
  50. this.arg = arg;
  51. this.component = component;
  52. this.comparison = comparison;
  53. this.target = target;
  54. }
  55. /**
  56. * Creates a new instance of ActionCondition that compares two strings.
  57. *
  58. * @param starget The first target for comparison.
  59. * @param comparison The comparison to be used
  60. * @param target The second target for the comparison
  61. */
  62. public ActionCondition(final String starget, final ActionComparison comparison,
  63. final String target) {
  64. this.arg = -1;
  65. this.starget = starget;
  66. this.comparison = comparison;
  67. this.target = target;
  68. }
  69. /**
  70. * Tests to see if this condition holds.
  71. *
  72. * @param sub The substitutor to use for this
  73. * @param args The event arguments to be tested
  74. *
  75. * @return True if the condition holds, false otherwise
  76. */
  77. public boolean test(final ActionSubstitutor sub, final Object... args) {
  78. final String thisTarget = sub.doSubstitution(getTarget(), args);
  79. if (arg == -1) {
  80. final String thisStarget = sub.doSubstitution(starget, args);
  81. return getComparison().test(thisStarget, thisTarget);
  82. } else {
  83. return getComparison().test(getComponent().get(args[getArg()]), thisTarget);
  84. }
  85. }
  86. /**
  87. * Returns the argument number this condition applies to.
  88. *
  89. * @return Argument number
  90. */
  91. public int getArg() {
  92. return arg;
  93. }
  94. /**
  95. * Returns the component this condition applies to.
  96. *
  97. * @return Component to apply condition to
  98. */
  99. public ActionComponent getComponent() {
  100. return component;
  101. }
  102. /**
  103. * Returns the comparison this condition applies to.
  104. *
  105. * @return Comparison to be used
  106. */
  107. public ActionComparison getComparison() {
  108. return comparison;
  109. }
  110. /**
  111. * Returns the target of the comparison for this condition.
  112. *
  113. * @return Target for comparison
  114. */
  115. public String getTarget() {
  116. return target;
  117. }
  118. /**
  119. * Sets the argument number this condition applies to.
  120. *
  121. * @param arg Argument number
  122. */
  123. public void setArg(final int arg) {
  124. this.arg = arg;
  125. }
  126. /**
  127. * Sets the component this condition applies to.
  128. *
  129. * @param component Component to apply condition to
  130. */
  131. public void setComponent(final ActionComponent component) {
  132. this.component = component;
  133. }
  134. /**
  135. * Sets the comparison this condition applies to.
  136. *
  137. * @param comparison Comparison to be used
  138. */
  139. public void setComparison(final ActionComparison comparison) {
  140. this.comparison = comparison;
  141. }
  142. /**
  143. * Sets the target of the comparison for this condition.
  144. *
  145. * @param target Target for comparison
  146. */
  147. public void setTarget(final String target) {
  148. this.target = target;
  149. }
  150. /**
  151. * Retrieves the starget of this condition.
  152. *
  153. * @return This condition's starget, or null if none was set.
  154. */
  155. public String getStarget() {
  156. return starget;
  157. }
  158. /**
  159. * Sets the starget for this condition.
  160. *
  161. * @param starget The new starget for this condition.
  162. */
  163. public void setStarget(final String starget) {
  164. this.starget = starget;
  165. }
  166. @Override
  167. public String toString() {
  168. return "[ arg=" + arg + ", component=" + component + ", comparison="
  169. + comparison + ", target=" + target + ", starget=" + starget + " ]";
  170. }
  171. @Override
  172. public boolean equals(final Object obj) {
  173. if (!(obj instanceof ActionCondition)) {
  174. return false;
  175. }
  176. final ActionCondition o = (ActionCondition) obj;
  177. return arg == o.getArg() && component == o.getComponent()
  178. && comparison == o.getComparison() && target.equals(o.getTarget())
  179. && starget.equals(o.getStarget());
  180. }
  181. @Override
  182. public int hashCode() {
  183. return arg + 100 * (arg == -1 ? starget.hashCode() : component.hashCode())
  184. + 10000 * comparison.hashCode() + 100000 * target.hashCode();
  185. }
  186. }