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

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