[jsword-devel] [JIRA] Commented: (JS-175) indexOf a valid Key in Josephus returns -1

DM Smith (JIRA) jira at crosswire.org
Sat Mar 12 06:12:25 MST 2011


    [ http://www.crosswire.org/bugs/browse/JS-175?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14110#comment-14110 ] 

DM Smith commented on JS-175:
-----------------------------

bq. What I really would like to do is find the next/prev key e.g. if on section1/chapter1 then pressing Next takes you to section1/chapter2. Is there anything in JSword that will help me do that for a TreeKey or do I need to manually navigate the tree?

One of the things about JSword is that it does not have a proper notion of previous/next. It has all kinds of iterators, but those have no notion of previous. key.get(n+1) and key.get(n-1) work for KeyLists but not TreeKeys.

It's not clear what next really means? I.e. is it depth-first or breadth first. It sounds that if you are on a second level node of 8 levels that next should stay on the same level but return the next sibling. If on the last sibling, then next() should be done.

If this is the case, you are iterating over the children of the parent. parent.get(n) will get the n-th child. You can write next sibling as:
{code}
Key nextSibling(Key key) throws IndexOutOfBoundsException {
   Key parent = key.getParent();
   int currentIndex = parent.indexOf(key);
   return parent.get(currentIndex + 1);
}

{code}
JSword's KeyIterator does a depth-first iterating of TreeKey. But it doesn't have previous.

If you mean that next means get next leaf, then I'd dump the leaves into a List. I'd try something like:
final DefaultKeyList leafList = new DefaultKeyList();
KeyUtil.visit(key, new DefaultKeyVisitor() {
 @Override
 public void visitLeaf(Key k) {
    leafList.add(k);
 }
});

Note, such a call gets all the keys below the TreeKey. If this is useful, we can add this to JSword.




> indexOf a valid Key in Josephus returns -1
> ------------------------------------------
>
>                 Key: JS-175
>                 URL: http://www.crosswire.org/bugs/browse/JS-175
>             Project: JSword
>          Issue Type: Bug
>          Components: o.c.jsword.passage
>    Affects Versions: 1.6.1
>         Environment: All
>            Reporter: Martin Denham
>            Assignee: DM Smith
>
> Here is my test:
> 	Book book = getBook("Josephus");
> 	assertNotNull("Josephus not available", book);
> 	
> 	// find a key and print out it's name - this works
> 	final String SECTION_2 = "Section 2";
> 	Key key = book.getKey(SECTION_2);
> 	assertEquals(SECTION_2, key.getName());
> 	
> 	// but we can't get it's index - this returns -1
> 	int keyPos = book.getGlobalKeyList().indexOf(key);
> 	assertFalse("Could not get index of a valid key", -1==keyPos);
> When dealing with a book having TreeKeys the top level Key returned by book.getGlobalKeyList is not a TreeKey but a KeyList which therefore does not automatically search it's children.  So I wonder if SwordGenBook.global could be a TreeKey rather than a ReadOnlyKeyList.
> Maybe the way it works is the way it should work but I am not sure.  Now I realise what is happening I could just call indexOf on each child of the globalKeyList but I think it would be more elegant if globalKeyList.indexOf returned a key from lower down in the tree.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira



More information about the jsword-devel mailing list