Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ActionCondition.java 6.6KB

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