File Source: TabCompleterResult.java
/*
P/P * Method: com.dmdirc.ui.input.TabCompleterResult__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
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
23 package com.dmdirc.ui.input;
24
25 import com.dmdirc.config.IdentityManager;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Locale;
30
31 /**
32 * Represents the result set from a tab completion operation.
33 * @author chris
34 */
35 public final class TabCompleterResult {
36
37 /**
38 * The result list for this tab completer.
39 */
40 private final List<String> results;
41
42 /**
43 * Creates a new instance of TabCompleterResult with an empty result set.
44 */
/*
P/P * Method: void com.dmdirc.ui.input.TabCompleterResult()
*
* Postconditions:
* this.results == &new ArrayList(TabCompleterResult#1)
* new ArrayList(TabCompleterResult#1) num objects == 1
*/
45 public TabCompleterResult() {
46 this.results = new ArrayList<String>();
47 }
48
49 /**
50 * Creates a new instance of TabCompleterResult.
51 * @param newResults The list of results that this result set contains
52 */
/*
P/P * Method: void com.dmdirc.ui.input.TabCompleterResult(List)
*
* Postconditions:
* this.results == newResults
* init'ed(this.results)
*/
53 public TabCompleterResult(final List<String> newResults) {
54 results = newResults;
55 }
56
57 /**
58 * Adds a result to this result set.
59 * @param result The result to be added
60 */
61 public void addResult(final String result) {
/*
P/P * Method: void addResult(String)
*
* Preconditions:
* this.results != null
*/
62 results.add(result);
63 }
64
65 /**
66 * Determines if this result set contains the specified result.
67 * @param result The result to be tested
68 * @return True if this set contains the specified result, false otherwise
69 */
70 public boolean hasResult(final String result) {
/*
P/P * Method: bool hasResult(String)
*
* Preconditions:
* this.results != null
*
* Postconditions:
* init'ed(return_value)
*/
71 return results.contains(result);
72 }
73
74 /**
75 * Merges the specified additional results with this result set.
76 * @param additional The results to merge
77 */
78 public void merge(final TabCompleterResult additional) {
/*
P/P * Method: void merge(TabCompleterResult)
*
* Preconditions:
* additional != null
* additional.results != null
* (soft) this.results != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@79: {0}, {1}
* java.util.List:contains(...)@71: {1}, {0}
*/
79 for (String result : additional.getResults()) {
80 if (!hasResult(result)) {
81 addResult(result);
82 }
83 }
84 }
85
86 /**
87 * Gets the total size of this result set.
88 * @return the size of this result set
89 */
90 public int getResultCount() {
/*
P/P * Method: int getResultCount()
*
* Preconditions:
* this.results != null
*
* Postconditions:
* init'ed(return_value)
*/
91 return results.size();
92 }
93
94 /**
95 * Returns the longest substring that matches all results.
96 * @return longest possible substring matching all results
97 */
98 public String getBestSubstring() {
/*
P/P * Method: String getBestSubstring()
*
* Preconditions:
* this.results != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@103 != null
* java.util.Iterator:next(...)@107 != null
* java.util.List:get(...)@106 != null
*
* Postconditions:
* java.lang.String:substring(...)._tainted == 0
* return_value != null
*
* Test Vectors:
* com.dmdirc.config.ConfigManager:getOptionBool(...)@103: {0}, {1}
* java.lang.String:startsWith(...)@109: {1}, {0}
* java.lang.String:startsWith(...)@113: {1}, {0}
* java.util.Iterator:hasNext(...)@107: {0}, {1}
* java.util.List:size(...)@91: {-231..-1, 1..232-1}, {0}
*/
99 if (getResultCount() == 0) {
100 return "";
101 }
102
103 final boolean caseSensitive = IdentityManager.getGlobalConfig()
104 .getOptionBool("tabcompletion", "casesensitive");
105
106 String res = results.get(0);
107 for (String entry : results) {
108 if (caseSensitive) {
109 while (!entry.startsWith(res)) {
110 res = res.substring(0, res.length() - 1);
111 }
112 } else {
113 while (!entry.toLowerCase(Locale.getDefault()).startsWith(
114 res.toLowerCase(Locale.getDefault()))) {
115 res = res.substring(0, res.length() - 1);
116 }
117 }
118 }
119
120 return res;
121 }
122
123 /**
124 * Retrieves the list of results that this set contains.
125 * @return An arraylist containing the results
126 */
127 public List<String> getResults() {
/*
P/P * Method: List getResults()
*
* Postconditions:
* return_value == this.results
* init'ed(return_value)
*/
128 return results;
129 }
130
131 /** {@inheritDoc} */
132 @Override
133 public String toString() {
/*
P/P * Method: String toString()
*
* Preconditions:
* this.results != null
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* return_value == &java.lang.StringBuffer:toString(...)
*
* Test Vectors:
* java.lang.StringBuffer:length(...)@137: {-231..0}, {1..232-1}
* java.util.Iterator:hasNext(...)@136: {0}, {1}
*/
134 final StringBuffer buff = new StringBuffer();
135
136 for (String entry : results) {
137 if (buff.length() > 0) {
138 buff.append(", ");
139 }
140
141 buff.append(entry);
142 }
143
144 return buff.toString();
145 }
146
147 }
SofCheck Inspector Build Version : 2.17854
| TabCompleterResult.java |
2009-Jun-25 01:54:24 |
| TabCompleterResult.class |
2009-Sep-02 17:04:12 |