in ch-commons-util/src/main/java/com/cloudhopper/commons/util/DigitLookupMap.java [142:215]
public V put(String key, V value) throws IllegalArgumentException {
assertValidPutKey(key);
char c;
int index;
int keyLength = key.length();
// if the "root" value is null, no keys have been put yet, and it's time
// that we create the initial root value
if (this.root == null) {
// create a new "root" node by setting the parent to null
this.root = new Node<V>(null);
}
Node<V> currentNode = this.root;
Node<V> nextNode = null;
V previousValue = null;
// search through the address until we reach the end OR best match
for (int i = 0; i < keyLength; i++) {
c = key.charAt(i);
index = toNodeArrayIndex(c);
// assertValidPutKey should already check, but to be safe
if (index == -1) {
throw new IllegalArgumentException("Illegal key [" + key + "]: unsupported char [" + c + "] at index [" + i + "]");
}
// the '*' or index of 10, means we set the prefix value at the current node
if (index == 10) {
// assertValidPutKey should already check, but to be safe
// this *must* be the last char in the key, otherwise, this is an invalid key
if (i+1 != keyLength) {
throw new IllegalArgumentException("Illegal key [" + key + "]: [*] can only be the last char in key");
}
// set the prefix value (and get the previous value)
previousValue = currentNode.setPrefixValue(value);
// done processing, nothing further is required
break;
}
// try to get the "next" node at this index
nextNode = currentNode.getNode(index);
// see if the "next" node exists
if (nextNode == null) {
// does not exist, create a new node and associate with current node as parent
nextNode = new Node<V>(currentNode);
// set this node to the index value
currentNode.setNode(index, nextNode);
}
// "next" node becomes "current" node
currentNode = nextNode;
// is the final character in the key?
if (i+1 == keyLength) {
// set the specific value (and get the previous value)
previousValue = currentNode.setSpecificValue(value);
}
}
if (previousValue == null && value != null) {
// added key-value mapping
this.size++;
} else if (previousValue != null && value == null) {
// removed key-value mapping
this.size--;
} else {
// key-value mapping wasn't removed or added -- do nothing
}
return previousValue;
}