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.

ActionCondition.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2006-2012 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
  41. * a component to a 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. super();
  51. this.arg = arg;
  52. this.component = component;
  53. this.comparison = comparison;
  54. this.target = target;
  55. }
  56. /**
  57. * Creates a new instance of ActionCondition that compares two strings.
  58. *
  59. * @param starget The first target for comparison.
  60. * @param comparison The comparison to be used
  61. * @param target The second target for the comparison
  62. */
  63. public ActionCondition(final String starget, final ActionComparison comparison,
  64. final String target) {
  65. super();
  66. this.arg = -1;
  67. this.starget = starget;
  68. this.comparison = comparison;
  69. this.target = target;
  70. }
  71. /**
  72. * Tests to see if this condition holds.
  73. *
  74. * @param sub The substitutor to use for this
  75. * @param args The event arguments to be tested
  76. * @return True if the condition holds, false otherwise
  77. */
  78. public boolean test(final ActionSubstitutor sub, final Object ... args) {
  79. final String thisTarget = sub.doSubstitution(getTarget(), args);
  80. if (arg == -1) {
  81. final String thisStarget = sub.doSubstitution(starget, args);
  82. return getComparison().test(thisStarget, thisTarget);
  83. } else {
  84. return getComparison().test(getComponent().get(args[getArg()]), thisTarget);
  85. }
  86. }
  87. /**
  88. * Returns the argument number this condition applies to.
  89. *
  90. * @return Argument number
  91. */
  92. public int getArg() {
  93. return arg;
  94. }
  95. /**
  96. * Returns the component this condition applies to.
  97. *
  98. * @return Component to apply condition to
  99. */
  100. public ActionComponent getComponent() {
  101. return component;
  102. }
  103. /**
  104. * Returns the comparison this condition applies to.
  105. *
  106. * @return Comparison to be used
  107. */
  108. public ActionComparison getComparison() {
  109. return comparison;
  110. }
  111. /**
  112. * Returns the target of the comparison for this condition.
  113. *
  114. * @return Target for comparison
  115. */
  116. public String getTarget() {
  117. return target;
  118. }
  119. /**
  120. * Sets the argument number this condition applies to.
  121. *
  122. * @param arg Argument number
  123. */
  124. public void setArg(final int arg) {
  125. this.arg = arg;
  126. }
  127. /**
  128. * Sets the component this condition applies to.
  129. *
  130. * @param component Component to apply condition to
  131. */
  132. public void setComponent(final ActionComponent component) {
  133. this.component = component;
  134. }
  135. /**
  136. * Sets the comparison this condition applies to.
  137. *
  138. * @param comparison Comparison to be used
  139. */
  140. public void setComparison(final ActionComparison comparison) {
  141. this.comparison = comparison;
  142. }
  143. /**
  144. * Sets the target of the comparison for this condition.
  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. /** {@inheritDoc} */
  167. @Override
  168. public String toString() {
  169. return "[ arg=" + arg + ", component=" + component + ", comparison="
  170. + comparison + ", target=" + target + ", starget=" + starget + " ]";
  171. }
  172. /** {@inheritDoc} */
  173. @Override
  174. public boolean equals(final Object obj) {
  175. if (!(obj instanceof ActionCondition)) {
  176. return false;
  177. }
  178. final ActionCondition o = (ActionCondition) obj;
  179. return arg == o.getArg() && component == o.getComponent()
  180. && comparison == o.getComparison() && target.equals(o.getTarget())
  181. && starget.equals(o.getStarget());
  182. }
  183. /** {@inheritDoc} */
  184. @Override
  185. public int hashCode() {
  186. return arg + 100 * (arg == -1 ? starget.hashCode() : component.hashCode())
  187. + 10000 * comparison.hashCode() + 100000 * target.hashCode();
  188. }
  189. }