public V get()

in ch-commons-util/src/main/java/com/cloudhopper/commons/util/DigitLookupMap.java [72:125]


    public V get(String key) throws IllegalArgumentException {
        assertValidGetKey(key);
        
        char c;
        int index;
        int keyLength = key.length();

        // if the "root" value is null, no keys have been put yet, always return null
        if (this.root == null) {
            return null;
        }

        Node<V> currentNode = this.root;

        // default the best matched value to the "root" node
        V bestMatchedValue = currentNode.getPrefixValue();

        // search through the key for the best match
        for (int i = 0; i < keyLength; i++) {
            c = key.charAt(i);
            index = toNodeArrayIndex(c);

            // assertValidPutKey should already check, but to be safe
            // check if it was a valid character
            if (index == -1 || index == 10) {
                throw new IllegalArgumentException("Illegal key [" + key + "]: unsupported char [" + c + "] at index [" + i + "]");
            }

            // if last char then see if there is a "specific" value
            if ((i+1) == keyLength) {
                // grab this specific index's node
                Node<V> specificNode = currentNode.getNode(index);
                // does a specific value exist?
                if (specificNode != null && specificNode.getSpecificValue() != null) {
                    // this is definitely the best value to return
                    return specificNode.getSpecificValue();
                }
            } else {
                // make the "current" node the "next" node
                currentNode = currentNode.getNode(index);
                // if the "next" node is null, then we already have the "best" value
                if (currentNode == null) {
                    return bestMatchedValue;
                } else {
                    // if a "prefix" value exists, this is actually the better value
                    if (currentNode.getPrefixValue() != null) {
                        bestMatchedValue = currentNode.getPrefixValue();
                    }
                }
            }
        }

        return bestMatchedValue;
    }