- Timestamp:
- 06/28/13 14:38:46 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/java-utils/src/main/java/de/ugoe/cs/util/StringTools.java
r1241 r1243 14 14 15 15 package de.ugoe.cs.util; 16 17 import java.util.ArrayList; 18 import java.util.List; 16 19 17 20 /** … … 63 66 return result; 64 67 } 68 69 /** 70 * <p> 71 * Performs an auto completion of the provided prefix using the given potential completions. 72 * The method searches for all completions that start with the given prefix and stores them in 73 * a subset. It then extends the prefix with characters of the subset of completions as long as 74 * the prefix still prefixes all completions in the subset. The result is then returned. If 75 * there is no matching completion or if there are several matching completions differing 76 * in the succeeding letter, the prefix is returned as is. If there is only one matching 77 * completion this completion is returned. 78 * </p> 79 * 80 * @param prefix the prefix to be further completed 81 * @param completions the potential completions of the prefix 82 * 83 * @return as described 84 */ 85 public static String autocomplete(String prefix, String[] completions) { 86 List<String> matchingCompletions = new ArrayList<String>(); 87 for (String completion : completions) { 88 if (completion.startsWith(prefix)) { 89 matchingCompletions.add(completion); 90 } 91 } 92 93 StringBuffer completedPrefix = new StringBuffer(prefix); 94 95 boolean foundCompletion = false; 96 97 while (!foundCompletion) { 98 char nextCompletionChar = 0; 99 for (String completion : matchingCompletions) { 100 if (completion.length() > completedPrefix.length()) { 101 if (nextCompletionChar == 0) { 102 nextCompletionChar = completion.charAt(completedPrefix.length()); 103 } 104 else if (nextCompletionChar != completion.charAt(completedPrefix.length())) { 105 foundCompletion = true; 106 } 107 } 108 else { 109 foundCompletion = true; 110 } 111 } 112 113 if (!foundCompletion && (nextCompletionChar != 0)) { 114 completedPrefix.append(nextCompletionChar); 115 } 116 else { 117 foundCompletion = true; 118 } 119 } 120 121 return completedPrefix.toString(); 122 } 65 123 }
Note: See TracChangeset
for help on using the changeset viewer.