Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CoreActionComparison.java 7.8KB

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