@Test public void testGetOutgoingEdge() throws Exception { Node node = new ByteArrayNodeNonLeafVoidValue( "FOO", Arrays.asList( (Node) new ByteArrayNodeDefault("BAR1", 1, Collections.<Node>emptyList()))); Assert.assertNotNull(node.getOutgoingEdge('B')); Assert.assertEquals(1, node.getOutgoingEdge('B').getValue()); }
/** * Traverses the tree based on characters in the given input, and for each node traversed which * encodes a key in the tree, invokes the given {@link KeyValueHandler} supplying it the key * which matched that node and the value from the node. * * @param input A sequence of characters which controls traversal of the tree * @param keyValueHandler An object which will be notified of every key and value encountered in * the input */ protected void scanForKeysAtStartOfInput(CharSequence input, KeyValueHandler keyValueHandler) { Node currentNode = super.root; int charsMatched = 0; final int documentLength = input.length(); outer_loop: while (charsMatched < documentLength) { Node nextNode = currentNode.getOutgoingEdge(input.charAt(charsMatched)); if (nextNode == null) { // Next node is a dead end... //noinspection UnnecessaryLabelOnBreakStatement break outer_loop; } currentNode = nextNode; CharSequence currentNodeEdgeCharacters = currentNode.getIncomingEdge(); int charsMatchedThisEdge = 0; for (int i = 0, j = Math.min(currentNodeEdgeCharacters.length(), documentLength - charsMatched); i < j; i++) { if (currentNodeEdgeCharacters.charAt(i) != input.charAt(charsMatched + i)) { // Found a difference in chars between character in key and a character in current node. // Current node is the deepest match (inexact match).... break outer_loop; } charsMatchedThisEdge++; } if (charsMatchedThisEdge == currentNodeEdgeCharacters.length()) { // All characters in the current edge matched, add this number to total chars matched... charsMatched += charsMatchedThisEdge; } if (currentNode.getValue() != null) { keyValueHandler.handle(input.subSequence(0, charsMatched), currentNode.getValue()); } } }