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.

TabCompleterResult.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.ui.input;
  23. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Locale;
  27. /**
  28. * Represents the result set from a tab completion operation.
  29. */
  30. public class TabCompleterResult {
  31. /**
  32. * The config provider to read settings from.
  33. */
  34. private final AggregateConfigProvider configProvider;
  35. /**
  36. * The result list for this tab completer.
  37. */
  38. private final List<String> results;
  39. /**
  40. * Creates a new instance of TabCompleterResult with an empty result set.
  41. *
  42. * @param configProvider The config provider to read settings from.
  43. */
  44. public TabCompleterResult(final AggregateConfigProvider configProvider) {
  45. this.configProvider = configProvider;
  46. this.results = new ArrayList<>();
  47. }
  48. /**
  49. * Adds a result to this result set.
  50. *
  51. * @param result The result to be added
  52. */
  53. public void addResult(final String result) {
  54. results.add(result);
  55. }
  56. /**
  57. * Determines if this result set contains the specified result.
  58. *
  59. * @param result The result to be tested
  60. *
  61. * @return True if this set contains the specified result, false otherwise
  62. */
  63. public boolean hasResult(final String result) {
  64. return results.contains(result);
  65. }
  66. /**
  67. * Merges the specified additional results with this result set.
  68. *
  69. * @param additional The results to merge
  70. */
  71. public void merge(final TabCompleterResult additional) {
  72. for (String result : additional.getResults()) {
  73. if (!hasResult(result)) {
  74. addResult(result);
  75. }
  76. }
  77. }
  78. /**
  79. * Gets the total size of this result set.
  80. *
  81. * @return the size of this result set
  82. */
  83. public int getResultCount() {
  84. return results.size();
  85. }
  86. /**
  87. * Returns the longest substring that matches all results.
  88. *
  89. * @return longest possible substring matching all results
  90. */
  91. public String getBestSubstring() {
  92. if (getResultCount() == 0) {
  93. return "";
  94. }
  95. final boolean caseSensitive = configProvider.getOptionBool("tabcompletion", "casesensitive");
  96. String res = results.get(0);
  97. for (String entry : results) {
  98. if (caseSensitive) {
  99. while (!entry.startsWith(res)) {
  100. res = res.substring(0, res.length() - 1);
  101. }
  102. } else {
  103. while (!entry.toLowerCase(Locale.getDefault()).startsWith(
  104. res.toLowerCase(Locale.getDefault()))) {
  105. res = res.substring(0, res.length() - 1);
  106. }
  107. }
  108. }
  109. return res;
  110. }
  111. /**
  112. * Retrieves the list of results that this set contains.
  113. *
  114. * @return An arraylist containing the results
  115. */
  116. public List<String> getResults() {
  117. return results;
  118. }
  119. /** {@inheritDoc} */
  120. @Override
  121. public String toString() {
  122. final StringBuilder buff = new StringBuilder();
  123. for (String entry : results) {
  124. if (buff.length() > 0) {
  125. buff.append(", ");
  126. }
  127. buff.append(entry);
  128. }
  129. return buff.toString();
  130. }
  131. }