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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2006-2007 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. /**
  24. * An action condition represents one condition within an action.
  25. * @author chris
  26. */
  27. public class ActionCondition {
  28. /** The argument number that this action condition applies to. */
  29. private int arg;
  30. /** The component that this action condition applies to. */
  31. private ActionComponent component;
  32. /** The comparison that should be used for this condition. */
  33. private ActionComparison comparison;
  34. /** The target of the comparison for this condition. */
  35. private String target;
  36. /**
  37. * Creates a new instance of ActionCondition.
  38. * @param arg The argument number to be tested
  39. * @param component The component to be tested
  40. * @param comparison The comparison to be used
  41. * @param target The target of the comparison
  42. */
  43. public ActionCondition(final int arg, final ActionComponent component,
  44. final ActionComparison comparison, final String target) {
  45. this.arg = arg;
  46. this.component = component;
  47. this.comparison = comparison;
  48. this.target = target;
  49. }
  50. /**
  51. * Tests to see if this condition holds.
  52. * @param args The event arguments to be tested
  53. * @return True if the condition holds, false otherwise
  54. */
  55. public boolean test(final Object ... args) {
  56. final String thisTarget = ActionManager.substituteVars(getTarget(), args);
  57. return getComparison().test(getComponent().get(args[getArg()]), thisTarget);
  58. }
  59. /**
  60. * Returns the argument number this condition applies to.
  61. *
  62. * @return Argument number
  63. */
  64. public int getArg() {
  65. return arg;
  66. }
  67. /**
  68. * Returns the component this condition applies to.
  69. *
  70. * @return Component to apply condition to
  71. */
  72. public ActionComponent getComponent() {
  73. return component;
  74. }
  75. /**
  76. * Returns the comparison this condition applies to.
  77. *
  78. * @return Comparison to be used
  79. */
  80. public ActionComparison getComparison() {
  81. return comparison;
  82. }
  83. /**
  84. * Returns the target of the comparison for this condition
  85. *
  86. * @return Target for comparison
  87. */
  88. public String getTarget() {
  89. return target;
  90. }
  91. /**
  92. * Sets the argument number this condition applies to.
  93. *
  94. * @param arg Argument number
  95. */
  96. public void setArg(final int arg) {
  97. this.arg = arg;
  98. }
  99. /**
  100. * Sets the component this condition applies to.
  101. *
  102. * @param component Component to apply condition to
  103. */
  104. public void setComponent(final ActionComponent component) {
  105. this.component = component;
  106. }
  107. /**
  108. * Sets the comparison this condition applies to.
  109. *
  110. * @param comparison Comparison to be used
  111. */
  112. public void setComparison(final ActionComparison comparison) {
  113. this.comparison = comparison;
  114. }
  115. /**
  116. * Sets the target of the comparison for this condition
  117. * @param target Target for comparison
  118. */
  119. public void setTarget(final String target) {
  120. this.target = target;
  121. }
  122. }