//# 0 errors, 110 messages
//#
/*
    //#TabCompleterResult.java:1:1: class: com.dmdirc.ui.input.TabCompleterResult
    //#TabCompleterResult.java:1:1: method: com.dmdirc.ui.input.TabCompleterResult.com.dmdirc.ui.input.TabCompleterResult__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.ui.input;

import com.dmdirc.config.IdentityManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Represents the result set from a tab completion operation.
 * @author chris
 */
public final class TabCompleterResult {
    
    /**
     * The result list for this tab completer.
     */
    private final List<String> results;
    
    /**
     * Creates a new instance of TabCompleterResult with an empty result set.
     */
    public TabCompleterResult() {
    //#TabCompleterResult.java:45: method: void com.dmdirc.ui.input.TabCompleterResult.com.dmdirc.ui.input.TabCompleterResult()
    //#input(void com.dmdirc.ui.input.TabCompleterResult()): this
    //#output(void com.dmdirc.ui.input.TabCompleterResult()): new ArrayList(TabCompleterResult#1) num objects
    //#output(void com.dmdirc.ui.input.TabCompleterResult()): this.results
    //#new obj(void com.dmdirc.ui.input.TabCompleterResult()): new ArrayList(TabCompleterResult#1)
    //#post(void com.dmdirc.ui.input.TabCompleterResult()): this.results == &new ArrayList(TabCompleterResult#1)
    //#post(void com.dmdirc.ui.input.TabCompleterResult()): new ArrayList(TabCompleterResult#1) num objects == 1
        this.results = new ArrayList<String>();
    }
    //#TabCompleterResult.java:47: end of method: void com.dmdirc.ui.input.TabCompleterResult.com.dmdirc.ui.input.TabCompleterResult()
    
    /**
     * Creates a new instance of TabCompleterResult.
     * @param newResults The list of results that this result set contains
     */
    public TabCompleterResult(final List<String> newResults) {
    //#TabCompleterResult.java:53: method: void com.dmdirc.ui.input.TabCompleterResult.com.dmdirc.ui.input.TabCompleterResult(List)
    //#input(void com.dmdirc.ui.input.TabCompleterResult(List)): newResults
    //#input(void com.dmdirc.ui.input.TabCompleterResult(List)): this
    //#output(void com.dmdirc.ui.input.TabCompleterResult(List)): this.results
    //#post(void com.dmdirc.ui.input.TabCompleterResult(List)): this.results == newResults
    //#post(void com.dmdirc.ui.input.TabCompleterResult(List)): init'ed(this.results)
        results = newResults;
    }
    //#TabCompleterResult.java:55: end of method: void com.dmdirc.ui.input.TabCompleterResult.com.dmdirc.ui.input.TabCompleterResult(List)
    
    /**
     * Adds a result to this result set.
     * @param result The result to be added
     */
    public void addResult(final String result) {
        results.add(result);
    //#TabCompleterResult.java:62: method: void com.dmdirc.ui.input.TabCompleterResult.addResult(String)
    //#input(void addResult(String)): result
    //#input(void addResult(String)): this
    //#input(void addResult(String)): this.results
    //#pre[3] (void addResult(String)): this.results != null
    }
    //#TabCompleterResult.java:63: end of method: void com.dmdirc.ui.input.TabCompleterResult.addResult(String)
    
    /**
     * Determines if this result set contains the specified result.
     * @param result The result to be tested
     * @return True if this set contains the specified result, false otherwise
     */
    public boolean hasResult(final String result) {
        return results.contains(result);
    //#TabCompleterResult.java:71: method: bool com.dmdirc.ui.input.TabCompleterResult.hasResult(String)
    //#input(bool hasResult(String)): result
    //#input(bool hasResult(String)): this
    //#input(bool hasResult(String)): this.results
    //#output(bool hasResult(String)): return_value
    //#pre[3] (bool hasResult(String)): this.results != null
    //#post(bool hasResult(String)): init'ed(return_value)
    //#TabCompleterResult.java:71: end of method: bool com.dmdirc.ui.input.TabCompleterResult.hasResult(String)
    }
    
    /**
     * Merges the specified additional results with this result set.
     * @param additional The results to merge
     */
    public void merge(final TabCompleterResult additional) {
        for (String result : additional.getResults()) {
    //#TabCompleterResult.java:79: method: void com.dmdirc.ui.input.TabCompleterResult.merge(TabCompleterResult)
    //#input(void merge(TabCompleterResult)): additional
    //#input(void merge(TabCompleterResult)): additional.results
    //#input(void merge(TabCompleterResult)): this
    //#input(void merge(TabCompleterResult)): this.results
    //#pre[1] (void merge(TabCompleterResult)): additional != null
    //#pre[2] (void merge(TabCompleterResult)): additional.results != null
    //#pre[4] (void merge(TabCompleterResult)): (soft) this.results != null
    //#unanalyzed(void merge(TabCompleterResult)): Effects-of-calling:java.util.List:add
    //#unanalyzed(void merge(TabCompleterResult)): Effects-of-calling:java.util.List:contains
    //#test_vector(void merge(TabCompleterResult)): java.util.Iterator:hasNext(...)@79: {0}, {1}
    //#test_vector(void merge(TabCompleterResult)): java.util.List:contains(...)@71: {1}, {0}
            if (!hasResult(result)) {
                addResult(result);
            }
        }
    }
    //#TabCompleterResult.java:84: end of method: void com.dmdirc.ui.input.TabCompleterResult.merge(TabCompleterResult)
    
    /**
     * Gets the total size of this result set.
     * @return the size of this result set
     */
    public int getResultCount() {
        return results.size();
    //#TabCompleterResult.java:91: method: int com.dmdirc.ui.input.TabCompleterResult.getResultCount()
    //#input(int getResultCount()): this
    //#input(int getResultCount()): this.results
    //#output(int getResultCount()): return_value
    //#pre[2] (int getResultCount()): this.results != null
    //#post(int getResultCount()): init'ed(return_value)
    //#TabCompleterResult.java:91: end of method: int com.dmdirc.ui.input.TabCompleterResult.getResultCount()
    }
    
    /**
     * Returns the longest substring that matches all results.
     * @return longest possible substring matching all results
     */
    public String getBestSubstring() {
        if (getResultCount() == 0) {
    //#TabCompleterResult.java:99: method: String com.dmdirc.ui.input.TabCompleterResult.getBestSubstring()
    //#input(String getBestSubstring()): this
    //#input(String getBestSubstring()): this.results
    //#output(String getBestSubstring()): java.lang.String:substring(...)._tainted
    //#output(String getBestSubstring()): return_value
    //#new obj(String getBestSubstring()): java.lang.String:substring(...)
    //#pre[2] (String getBestSubstring()): this.results != null
    //#presumption(String getBestSubstring()): com.dmdirc.config.IdentityManager:getGlobalConfig(...)@103 != null
    //#presumption(String getBestSubstring()): java.util.Iterator:next(...)@107 != null
    //#presumption(String getBestSubstring()): java.util.List:get(...)@106 != null
    //#post(String getBestSubstring()): java.lang.String:substring(...)._tainted == 0
    //#post(String getBestSubstring()): return_value != null
    //#unanalyzed(String getBestSubstring()): Effects-of-calling:java.util.List:size
    //#test_vector(String getBestSubstring()): com.dmdirc.config.ConfigManager:getOptionBool(...)@103: {0}, {1}
    //#test_vector(String getBestSubstring()): java.lang.String:startsWith(...)@109: {1}, {0}
    //#test_vector(String getBestSubstring()): java.lang.String:startsWith(...)@113: {1}, {0}
    //#test_vector(String getBestSubstring()): java.util.Iterator:hasNext(...)@107: {0}, {1}
    //#test_vector(String getBestSubstring()): java.util.List:size(...)@91: {-2_147_483_648..-1, 1..4_294_967_295}, {0}
            return "";
        }
        
        final boolean caseSensitive = IdentityManager.getGlobalConfig()
    //#TabCompleterResult.java:103: Warning: method not available - call not analyzed
    //#    call on ConfigManager com.dmdirc.config.IdentityManager:getGlobalConfig()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.ui.input.TabCompleterResult
    //#    method: String getBestSubstring()
    //#    unanalyzed callee: ConfigManager com.dmdirc.config.IdentityManager:getGlobalConfig()
    //#TabCompleterResult.java:103: Warning: method not available - call not analyzed
    //#    call on bool com.dmdirc.config.ConfigManager:getOptionBool(String, String)
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.ui.input.TabCompleterResult
    //#    method: String getBestSubstring()
    //#    unanalyzed callee: bool com.dmdirc.config.ConfigManager:getOptionBool(String, String)
                .getOptionBool("tabcompletion", "casesensitive");
        
        String res = results.get(0);
        for (String entry : results) {
            if (caseSensitive) {
                while (!entry.startsWith(res)) {
                    res = res.substring(0, res.length() - 1);
                }
            } else {
                while (!entry.toLowerCase(Locale.getDefault()).startsWith(
                        res.toLowerCase(Locale.getDefault()))) {
                    res = res.substring(0, res.length() - 1);
                }
            }
        }
        
        return res;
    //#TabCompleterResult.java:120: end of method: String com.dmdirc.ui.input.TabCompleterResult.getBestSubstring()
    }
    
    /**
     * Retrieves the list of results that this set contains.
     * @return An arraylist containing the results
     */
    public List<String> getResults() {
        return results;
    //#TabCompleterResult.java:128: method: List com.dmdirc.ui.input.TabCompleterResult.getResults()
    //#input(List getResults()): this
    //#input(List getResults()): this.results
    //#output(List getResults()): return_value
    //#post(List getResults()): return_value == this.results
    //#post(List getResults()): init'ed(return_value)
    //#TabCompleterResult.java:128: end of method: List com.dmdirc.ui.input.TabCompleterResult.getResults()
    }
    
    /** {@inheritDoc} */
    @Override
    public String toString() {
        final StringBuffer buff = new StringBuffer();
    //#TabCompleterResult.java:134: method: String com.dmdirc.ui.input.TabCompleterResult.toString()
    //#input(String toString()): ", "._tainted
    //#input(String toString()): this
    //#input(String toString()): this.results
    //#output(String toString()): java.lang.StringBuffer:toString(...)._tainted
    //#output(String toString()): return_value
    //#new obj(String toString()): java.lang.StringBuffer:toString(...)
    //#pre[2] (String toString()): this.results != null
    //#post(String toString()): java.lang.StringBuffer:toString(...)._tainted == 0
    //#post(String toString()): return_value == &java.lang.StringBuffer:toString(...)
    //#test_vector(String toString()): java.lang.StringBuffer:length(...)@137: {-2_147_483_648..0}, {1..4_294_967_295}
    //#test_vector(String toString()): java.util.Iterator:hasNext(...)@136: {0}, {1}
        
        for (String entry : results) {
            if (buff.length() > 0) {
                buff.append(", ");
            }
            
            buff.append(entry);
        }
        
        return buff.toString();
    //#TabCompleterResult.java:144: end of method: String com.dmdirc.ui.input.TabCompleterResult.toString()
    }
    
}
    //#output(com.dmdirc.ui.input.TabCompleterResult__static_init): __Descendant_Table[com/dmdirc/ui/input/TabCompleterResult]
    //#output(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.addResult(Ljava/lang/String;)V
    //#output(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.getBestSubstring()Ljava/lang/String;
    //#output(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.getResultCount()I
    //#output(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.getResults()Ljava/util/List;
    //#output(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.hasResult(Ljava/lang/String;)Z
    //#output(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.merge(Lcom/dmdirc/ui/input/TabCompleterResult;)V
    //#output(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.toString()Ljava/lang/String;
    //#post(com.dmdirc.ui.input.TabCompleterResult__static_init): __Descendant_Table[com/dmdirc/ui/input/TabCompleterResult] == &__Dispatch_Table
    //#post(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.addResult(Ljava/lang/String;)V == &addResult
    //#post(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.getBestSubstring()Ljava/lang/String; == &getBestSubstring
    //#post(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.getResultCount()I == &getResultCount
    //#post(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.getResults()Ljava/util/List; == &getResults
    //#post(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.hasResult(Ljava/lang/String;)Z == &hasResult
    //#post(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.merge(Lcom/dmdirc/ui/input/TabCompleterResult;)V == &merge
    //#post(com.dmdirc.ui.input.TabCompleterResult__static_init): __Dispatch_Table.toString()Ljava/lang/String; == &toString
    //#TabCompleterResult.java:: end of method: com.dmdirc.ui.input.TabCompleterResult.com.dmdirc.ui.input.TabCompleterResult__static_init
    //#TabCompleterResult.java:: end of class: com.dmdirc.ui.input.TabCompleterResult
