Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CoreActionComparison.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 uk.org.ownage.dmdirc.actions;
  23. import java.awt.Color;
  24. import java.util.regex.PatternSyntaxException;
  25. import uk.org.ownage.dmdirc.ui.messages.ColourManager;
  26. /**
  27. * A CoreActionComparison represents a possible comparison between two types of
  28. * data used in an action condition.
  29. * @author chris
  30. */
  31. public enum CoreActionComparison implements ActionComparison {
  32. STRING_REGEX {
  33. public boolean test(final Object arg1, final Object arg2) {
  34. try {
  35. return ((String) arg1).matches((String) arg2);
  36. } catch (PatternSyntaxException pse) {
  37. return false;
  38. }
  39. }
  40. public Class appliesTo() { return String.class; }
  41. public String getName() { return "matches regex"; }
  42. },
  43. STRING_EQUALS {
  44. public boolean test(final Object arg1, final Object arg2) {
  45. return ((String) arg1).equalsIgnoreCase((String) arg2);
  46. }
  47. public Class appliesTo() { return String.class; }
  48. public String getName() { return "equals"; }
  49. },
  50. STRING_NEQUALS {
  51. public boolean test(final Object arg1, final Object arg2) {
  52. return !STRING_EQUALS.test(arg1, arg2);
  53. }
  54. public Class appliesTo() { return String.class; }
  55. public String getName() { return "does not equal"; }
  56. },
  57. STRING_STARTSWITH {
  58. public boolean test(final Object arg1, final Object arg2) {
  59. return ((String) arg1).startsWith((String) arg2);
  60. }
  61. public Class appliesTo() { return String.class; }
  62. public String getName() { return "starts with"; }
  63. },
  64. STRING_CONTAINS {
  65. public boolean test(final Object arg1, final Object arg2) {
  66. return ((String) arg1).indexOf((String) arg2) != -1;
  67. }
  68. public Class appliesTo() { return String.class; }
  69. public String getName() { return "contains"; }
  70. },
  71. COLOUR_EQUALS {
  72. public boolean test(final Object arg1, final Object arg2) {
  73. return ((Color) arg1).equals(ColourManager.parseColour((String) arg2));
  74. }
  75. public Class appliesTo() { return Color.class; }
  76. public String getName() { return "equals"; }
  77. },
  78. COLOUR_NEQUALS {
  79. public boolean test(final Object arg1, final Object arg2) {
  80. return !COLOUR_EQUALS.test(arg1, arg2);
  81. }
  82. public Class appliesTo() { return Color.class; }
  83. public String getName() { return "does not equal"; }
  84. },
  85. INT_EQUALS {
  86. public boolean test(final Object arg1, final Object arg2) {
  87. try {
  88. return 0 == ((Integer) arg1).compareTo(Integer.parseInt((String) arg2));
  89. } catch (NumberFormatException ex) {
  90. return false;
  91. }
  92. }
  93. public Class appliesTo() { return Integer.class; }
  94. public String getName() { return "equals"; }
  95. },
  96. INT_GREATER {
  97. public boolean test(final Object arg1, final Object arg2) {
  98. try {
  99. return 0 < ((Integer) arg1).compareTo(Integer.parseInt((String) arg2));
  100. } catch (NumberFormatException ex) {
  101. return false;
  102. }
  103. }
  104. public Class appliesTo() { return Integer.class; }
  105. public String getName() { return "is greater than"; }
  106. },
  107. INT_LESS {
  108. public boolean test(final Object arg1, final Object arg2) {
  109. try {
  110. return 0 > ((Integer) arg1).compareTo(Integer.parseInt((String) arg2));
  111. } catch (NumberFormatException ex) {
  112. return false;
  113. }
  114. }
  115. public Class appliesTo() { return Integer.class; }
  116. public String getName() { return "is less than"; }
  117. };
  118. /** {@inheritDoc} */
  119. public abstract boolean test(final Object arg1, final Object arg2);
  120. /** {@inheritDoc} */
  121. public abstract Class appliesTo();
  122. /** {@inheritDoc} */
  123. public abstract String getName();
  124. }