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.

CoreActionComparison.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.ui.Colour;
  25. import com.dmdirc.ui.messages.ColourManager;
  26. import java.util.regex.PatternSyntaxException;
  27. /**
  28. * A CoreActionComparison represents a possible comparison between two types of
  29. * data used in an action condition.
  30. */
  31. public enum CoreActionComparison implements ActionComparison {
  32. /** Compares a string to another using a regular expression. */
  33. STRING_REGEX {
  34. /** {@inheritDoc} */
  35. @Override
  36. public boolean test(final Object arg1, final Object arg2) {
  37. try {
  38. return ((String) arg1).matches((String) arg2);
  39. } catch (PatternSyntaxException pse) {
  40. return false;
  41. }
  42. }
  43. /** {@inheritDoc} */
  44. @Override
  45. public Class<?> appliesTo() { return String.class; }
  46. /** {@inheritDoc} */
  47. @Override
  48. public String getName() { return "matches regex"; }
  49. },
  50. /** Compares if two strings content are the same, case insensitive. */
  51. STRING_EQUALS {
  52. /** {@inheritDoc} */
  53. @Override
  54. public boolean test(final Object arg1, final Object arg2) {
  55. return ((String) arg1).equalsIgnoreCase((String) arg2);
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. public Class<?> appliesTo() { return String.class; }
  60. /** {@inheritDoc} */
  61. @Override
  62. public String getName() { return "equals"; }
  63. },
  64. /** Compares if two strings content aren't the same, case insensitive. */
  65. STRING_NEQUALS {
  66. /** {@inheritDoc} */
  67. @Override
  68. public boolean test(final Object arg1, final Object arg2) {
  69. return !STRING_EQUALS.test(arg1, arg2);
  70. }
  71. /** {@inheritDoc} */
  72. @Override
  73. public Class<?> appliesTo() { return String.class; }
  74. /** {@inheritDoc} */
  75. @Override
  76. public String getName() { return "does not equal"; }
  77. },
  78. /** Checks if the string starts with another strings. */
  79. STRING_STARTSWITH {
  80. /** {@inheritDoc} */
  81. @Override
  82. public boolean test(final Object arg1, final Object arg2) {
  83. return ((String) arg1).startsWith((String) arg2);
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public Class<?> appliesTo() { return String.class; }
  88. /** {@inheritDoc} */
  89. @Override
  90. public String getName() { return "starts with"; }
  91. },
  92. /** Checks if the string containts another string. */
  93. STRING_CONTAINS {
  94. /** {@inheritDoc} */
  95. @Override
  96. public boolean test(final Object arg1, final Object arg2) {
  97. return ((String) arg1).indexOf((String) arg2) != -1;
  98. }
  99. /** {@inheritDoc} */
  100. @Override
  101. public Class<?> appliesTo() { return String.class; }
  102. /** {@inheritDoc} */
  103. @Override
  104. public String getName() { return "contains"; }
  105. },
  106. /** Checks if the string doesn't containt another string. */
  107. STRING_NCONTAINS {
  108. /** {@inheritDoc} */
  109. @Override
  110. public boolean test(final Object arg1, final Object arg2) {
  111. return ((String) arg1).indexOf((String) arg2) == -1;
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. public Class<?> appliesTo() { return String.class; }
  116. /** {@inheritDoc} */
  117. @Override
  118. public String getName() { return "doesn't contain"; }
  119. },
  120. /** Checks if two boolean values are equal. */
  121. BOOL_IS {
  122. /** {@inheritDoc} */
  123. @Override
  124. public boolean test(final Object arg1, final Object arg2) {
  125. return ((Boolean) arg1).equals(Boolean.valueOf((String) arg2));
  126. }
  127. /** {@inheritDoc} */
  128. @Override
  129. public Class<?> appliesTo() { return Boolean.class; }
  130. /** {@inheritDoc} */
  131. @Override
  132. public String getName() { return "is"; }
  133. },
  134. /** Checks if the colour is the same as another colour. */
  135. COLOUR_EQUALS {
  136. /** {@inheritDoc} */
  137. @Override
  138. public boolean test(final Object arg1, final Object arg2) {
  139. return ((Colour) arg1).equals(ColourManager.parseColour((String) arg2));
  140. }
  141. /** {@inheritDoc} */
  142. @Override
  143. public Class<?> appliesTo() { return Colour.class; }
  144. /** {@inheritDoc} */
  145. @Override
  146. public String getName() { return "equals"; }
  147. },
  148. /** Checks if the colour is not the same as another colour. */
  149. COLOUR_NEQUALS {
  150. /** {@inheritDoc} */
  151. @Override
  152. public boolean test(final Object arg1, final Object arg2) {
  153. return !COLOUR_EQUALS.test(arg1, arg2);
  154. }
  155. /** {@inheritDoc} */
  156. @Override
  157. public Class<?> appliesTo() { return Colour.class; }
  158. /** {@inheritDoc} */
  159. @Override
  160. public String getName() { return "does not equal"; }
  161. },
  162. /** Checks if the int is equals to another int. */
  163. INT_EQUALS {
  164. /** {@inheritDoc} */
  165. @Override
  166. public boolean test(final Object arg1, final Object arg2) {
  167. try {
  168. return 0 == ((Integer) arg1).compareTo(Integer.parseInt((String) arg2));
  169. } catch (NumberFormatException ex) {
  170. return false;
  171. }
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public Class<?> appliesTo() { return Integer.class; }
  176. /** {@inheritDoc} */
  177. @Override
  178. public String getName() { return "equals"; }
  179. },
  180. /** Checks if the int is larger than another int. */
  181. INT_GREATER {
  182. /** {@inheritDoc} */
  183. @Override
  184. public boolean test(final Object arg1, final Object arg2) {
  185. try {
  186. return 0 < ((Integer) arg1).compareTo(Integer.parseInt((String) arg2));
  187. } catch (NumberFormatException ex) {
  188. return false;
  189. }
  190. }
  191. /** {@inheritDoc} */
  192. @Override
  193. public Class<?> appliesTo() { return Integer.class; }
  194. /** {@inheritDoc} */
  195. @Override
  196. public String getName() { return "is greater than"; }
  197. },
  198. /** Checks if the int is smaller than another int. */
  199. INT_LESS {
  200. /** {@inheritDoc} */
  201. @Override
  202. public boolean test(final Object arg1, final Object arg2) {
  203. try {
  204. return 0 > ((Integer) arg1).compareTo(Integer.parseInt((String) arg2));
  205. } catch (NumberFormatException ex) {
  206. return false;
  207. }
  208. }
  209. /** {@inheritDoc} */
  210. @Override
  211. public Class<?> appliesTo() { return Integer.class; }
  212. /** {@inheritDoc} */
  213. @Override
  214. public String getName() { return "is less than"; }
  215. };
  216. }