t9search.java

来自「moblie syncml mail javame」· Java 代码 · 共 466 行 · 第 1/2 页

JAVA
466
字号
            return M;
        } else if ((char)keyCode == 'l') {
            return L;
        } else if (keyCode>0) {
            String [] s = new String[1];
            s[0] = "" + (char)keyCode;
            return s;
        }
        // if nothing works, we return nothing!
        return new String[0];
    }

    
    
    /**
     * check if the contactitem visible name, first name or last name starts with
     * one of the  t9 strings.
     * @return true if given contactitem's contact has visible name, firstname or
     * lastname with a word that starts with one of the strings currently in
     * the strings vector, or if strings is null
     *
     */
    public boolean match(ContactItem c) {
        if (strings == null) {
            return true;
        }
        Enumeration e = strings.elements();
        if (!e.hasMoreElements()) {
            Log.info("No elements in strings, returning true");
            return true;
        }
        while (e.hasMoreElements()) {
            String searchString = (String) e.nextElement();
            if (searchString == null) {
                return true;
            }
            // selected addresses must always be shown
            if(c.getState() != ContactItem.ADDRESS_STATE_NONE) {
                return true;
            }
            
            if ( smartStartsWith( c.getContact().getVisibleName(),searchString ) ||
                    smartStartsWith( c.getContact().getFirstName(), searchString ) ||
                    smartStartsWith( c.getContact().getLastName(), searchString )) {
                return true;
            }
            
        }
        return false;
    }
    
    
    
    /**
     * return true if tocheck contains a word that starts with start
     * (case insensitive).
     */
    private boolean smartStartsWith(String toCheck, String start) {
        
        if (StringUtil.isNullOrEmpty(toCheck)) {
            return false;
        }
        
        ChunkedString c = new ChunkedString(toCheck);
        String lower = start.toLowerCase();
        
        while (!c.isEmpty()) {
            if(c.getNextString(" ").toLowerCase().startsWith(lower)) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     *
     * filter the list of contactitems removing non matching elements.
     * the filter uses the vector of strings (that can be retrieved with
     * the getStrings() method) to find the matching set of contactitems
     * @param contactItems the full contactitems list. used if strings is null
     * (no filter should be performed)
     * @param filteredVector the filtered contact list, used as source of items
     * if some filtering should be performed
     * @throws NoMatchingContactsException if no matching contacts are found.
     * if the exception is thrown, a rollback on the  strings  element is performed
     *
     */
    
    public synchronized Vector filter(Vector contactItems, Vector filteredVector)
    throws NoMatchingContactsException {
        if (strings == null ) {
            return contactItems;
        }
        
        Vector ret = new Vector(filteredVector.size());
        if (strings.size() == 0) {
            Log.info("size=0, returning empty vector!");
            return ret;
        }
        
        Enumeration s = strings.elements();
        Vector removedStrings = new Vector(strings.size());
        Log.info("strings size= " + strings.size());
        Log.info("using filteredvector. Size =" + filteredVector.size());
        
        ContactItem c;
        boolean used = false;
        for (int i = 0; i<strings.size(); i++){
            StringBuffer sb = (StringBuffer)strings.elementAt(i);
            String searchString = sb.toString();
            Log.info("checking string " + searchString);
            Enumeration e = filteredVector.elements();
            used = false;
            while (e.hasMoreElements()) {
                c = (ContactItem)e.nextElement();
                if (matchString(c, searchString)) {
                    // this means this string is useful we add the element and
                    // track that we used this string
                    used=true;
                    Log.info("Used string '" + searchString + "' for contact " + c.getContact().getVisibleName());
                    if (ret.indexOf(c) == -1) {
                        ret.addElement(c);
                    }
                }
            }
            
            if (!used ) {
                // prune the dead branches, but keep them if
                // we need to rollback
                removedStrings.addElement(sb);
                strings.removeElementAt(i);
                //back to save the iteration loop
                i--;
                Log.info("Pruning string "+ sb.toString());
            }
        }
        
        
        if (ret.size() == 0) {
            // we need to go back to initial state of strings
            
            strings.removeAllElements();
            
            StringBuffer b;
            for (int i = 0; i< removedStrings.size(); i++) {
                b = (StringBuffer) removedStrings.elementAt(i);
                b.deleteCharAt(b.length()-1);
                if (!containString(b, strings)) {
                    strings.addElement(b);
                }
            }
            throw  new NoMatchingContactsException();
        }
        return ret;
    }
    
    /**
     * search in the given vector for a stringbuffer matching the given one.
     * @param vector a vector of stringbuffer
     * @param s the stringbuffer to compare
     */
    private boolean containString(StringBuffer sb, Vector v) {
        
        StringBuffer sbuf;
        for (int i = 0; i < v.size() ; i++ ) {
            sbuf = (StringBuffer)v.elementAt(i);
            if (sbuf.toString().compareTo(sb.toString()) == 0) {
                return true;
            }
        }
        
        return false;
    }
    
    
    private boolean matchString(ContactItem c, String s) {
        return ( smartStartsWith( c.getContact().getVisibleName(),s ) ||
                smartStartsWith( c.getContact().getFirstName(), s ) ||
                smartStartsWith( c.getContact().getLastName(), s) ||
                emailNameOrDomainStartsWith(c, s)
                ) ;
    }
    
    /**
     * set strings to null.
     * be careful when calling this in multi threaded environment, since
     * setting strings to null can raise nullpointer exceptions if the class
     * is performing a filter operation or something else
     */
    public void clear() {
        strings = null;
    }
    
    
    /**
     * check if given contact's default email starts with given string or domain
     * (the part after '@' in the default email) starts with the given string
     */
    private boolean emailNameOrDomainStartsWith(ContactItem cItem, String s) {
        if (StringUtil.isNullOrEmpty(s)) {
            return false;
        }
        
        ChunkedString c = new ChunkedString(cItem.getContact().getDefaultEmail());
        String lower = s.toLowerCase();
        
        while (!c.isEmpty()) {
            if(c.getNextString("@").toLowerCase().startsWith(lower)) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * set the starting string from wich the t9 search will start
     */
    void setString(String string) {
        if (strings==null) {
            strings = new Vector();
        } else {
            strings.removeAllElements();
        }
        
        strings.addElement(new StringBuffer(string));
        
        
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?