[jsword-devel] ContainsAny key

Chris Burrell chris at burrell.me.uk
Fri Apr 25 05:16:07 MST 2014


Hello

Further to the pullrequest, which is now in test with STEP, I've wanted to
add a containsAny(key) method.

Here's what I've got so far, harnessing the new Scope and BookList


   private boolean containsAny(Book master, Key k) {
        if(!(master instanceof AbstractPassageBook)) {
            return master.contains(k);
        }

        final Set<BibleBook> books = ((AbstractPassageBook)
master).getBibleBooks();
        final Verse firstVerse = KeyUtil.getVerse(k);
        if(!books.contains(firstVerse.getBook())) {
            //the books of the module do not contain the book referred to
by the verse
            return false;
        }

        //we're still here, so the books do exist
        //so let's now examine the keys one by one
        Iterator<Key> keys = k.iterator();
        while(keys.hasNext()) {
            if(master.contains(keys.next())) {
                return true;
            }
        }

        return false;
    }

This may be worth integrating at the AbstractPassageBook level.

For the ESV and KJV the hit is 1 disk seek as they contain verse 0.
For the ASV, it is 2 disk seeks as the first master.contains(keys.next())
returns false.
For WHNU, Genesis would not exist and so it would be purely down to
getBibleBooks().contains(firstVerse.getBooK())

My only concern is that for a key that doesn't exist, then this would be
quite expensive for larger keys.

The only odd thing in the API is that getKey (and getValidKey) would end up
giving you a key that doesn't exist in the module.

In my use case, I'm suggesting keys to the user based on their input. So
this would happen very rarely (i.e. if they type in Ge, then I suggest
Gen.). So I do a

k = book.getKey()
containsAny(book, k)
if(true) return the key.

Perhaps we should modify getKey()'s behaviour...

Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.crosswire.org/pipermail/jsword-devel/attachments/20140425/6934c44d/attachment.html>


More information about the jsword-devel mailing list