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.

ColourActionComparison.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2006-2015 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.ClientModule;
  24. import com.dmdirc.interfaces.actions.ActionComparison;
  25. import com.dmdirc.ui.messages.ColourManager;
  26. import com.dmdirc.util.colours.Colour;
  27. import javax.inject.Inject;
  28. import javax.inject.Provider;
  29. /**
  30. * Core action comparisons dependent on a colour manager.
  31. */
  32. public class ColourActionComparison {
  33. /** Instance of the equals comparison. */
  34. private final Equals equalsComparison;
  35. /** Instance of the not equals comparison. */
  36. private final NotEquals notEqualsComparison;
  37. /** Colour manager to use to parse colours. */
  38. private final Provider<ColourManager> colourManager;
  39. /**
  40. * Creates a new instance of {@link ColourActionComparison}.
  41. *
  42. * @param colourManager The colour manager to use.
  43. */
  44. @Inject
  45. public ColourActionComparison(@ClientModule.GlobalConfig final Provider<ColourManager> colourManager) {
  46. this.equalsComparison = new Equals();
  47. this.notEqualsComparison = new NotEquals();
  48. this.colourManager = colourManager;
  49. }
  50. /**
  51. * Gets the available colour comparisons.
  52. *
  53. * @return The available comparisons.
  54. */
  55. public ActionComparison[] getComparisons() {
  56. return new ActionComparison[]{equalsComparison, notEqualsComparison};
  57. }
  58. /** Checks if the colour is the same as another colour. */
  59. private class Equals implements ActionComparison {
  60. @Override
  61. public boolean test(final Object arg1, final Object arg2) {
  62. return arg1.equals(colourManager.get().getColourFromString((String) arg2, null));
  63. }
  64. @Override
  65. public Class<?> appliesTo() {
  66. return Colour.class;
  67. }
  68. @Override
  69. public String getName() {
  70. return "equals";
  71. }
  72. @Override
  73. public String name() {
  74. return "COLOUR_EQUALS";
  75. }
  76. }
  77. /** Checks if the colour is not the same as another colour. */
  78. private class NotEquals implements ActionComparison {
  79. @Override
  80. public boolean test(final Object arg1, final Object arg2) {
  81. return !equalsComparison.test(arg1, arg2);
  82. }
  83. @Override
  84. public Class<?> appliesTo() {
  85. return Colour.class;
  86. }
  87. @Override
  88. public String getName() {
  89. return "does not equal";
  90. }
  91. @Override
  92. public String name() {
  93. return "COLOUR_NEQUALS";
  94. }
  95. }
  96. }