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.

ModeManager.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2006-2017 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.parser.irc;
  23. /**
  24. * Generic mode manager.
  25. */
  26. public class ModeManager {
  27. /** All known modes, in increasing order of importance. */
  28. private String modes = "";
  29. /**
  30. * Resets the state of this manager, clearing all known modes.
  31. */
  32. public void clear() {
  33. modes = "";
  34. }
  35. /**
  36. * Replaces all existing modes with the specified ones.
  37. *
  38. * @param modes The new modes, in increasing order of importance.
  39. */
  40. public void set(final String modes) {
  41. this.modes = modes;
  42. }
  43. /**
  44. * Adds a new mode. Modes must be added in increasing order of importance.
  45. *
  46. * @param mode The mode that appears in mode strings (e.g. 'o').
  47. */
  48. public void add(final char mode) {
  49. modes += mode;
  50. }
  51. /**
  52. * Determines if the specified character is a mode (e.g. 'o', 'v').
  53. *
  54. * @param mode The mode to be tested
  55. * @return True if the mode is a mode, false otherwise.
  56. */
  57. public boolean isMode(final char mode) {
  58. return modes.indexOf(mode) > -1;
  59. }
  60. /**
  61. * Gets the set of all known prefix modes.
  62. *
  63. * @return Set of known modes, in increasing order of importance.
  64. */
  65. public String getModes() {
  66. return modes;
  67. }
  68. /**
  69. * Compares the most important mode of the given mode lists.
  70. *
  71. * @param modes1 The first set of modes to compare. Must be ordered by importance.
  72. * @param modes2 The second set of modes to compare. Must be ordered by importance.
  73. * @return A negative number of modes2 is more important than modes1; a positive number if
  74. * modes1 is more important than modes2; zero if the two are equivalent.
  75. */
  76. public int compareImportantModes(final String modes1, final String modes2) {
  77. final char mode1 = modes1.isEmpty() ? ' ' : modes1.charAt(0);
  78. final char mode2 = modes2.isEmpty() ? ' ' : modes2.charAt(0);
  79. final int modeValue1 = modes.indexOf(mode1);
  80. final int modeValue2 = modes.indexOf(mode2);
  81. return modeValue1 - modeValue2;
  82. }
  83. /**
  84. * Inserts the specified mode into the correct place in the mode string, maintaining importance
  85. * order.
  86. *
  87. * @param modeString The existing modes to add the new one to.
  88. * @param mode The new mode to be added.
  89. * @return A mode string containing all the modes.
  90. */
  91. public String insertMode(final String modeString, final char mode) {
  92. if (modeString.indexOf(mode) > -1) {
  93. // Don't duplicate an existing mode
  94. return modeString;
  95. }
  96. final StringBuilder result = new StringBuilder(modeString.length() + 1);
  97. boolean missing = true;
  98. final int value = modes.indexOf(mode);
  99. for (char existingMode : modeString.toCharArray()) {
  100. if (modes.indexOf(existingMode) < value && missing) {
  101. // Our new mode is more important, insert it first.
  102. result.append(mode);
  103. missing = false;
  104. }
  105. result.append(existingMode);
  106. }
  107. if (missing) {
  108. result.append(mode);
  109. }
  110. return result.toString();
  111. }
  112. /**
  113. * Removes the specified mode from the mode string.
  114. *
  115. * @param modeString The mode string to modify.
  116. * @param mode The mode to be removed.
  117. * @return A copy of the mode string with the mode removed.
  118. */
  119. public String removeMode(final String modeString, final char mode) {
  120. return modeString.replace(Character.toString(mode), "");
  121. }
  122. }