org.crosswire.jsword.passage
Class SynchronizedPassage

java.lang.Object
  extended by org.crosswire.jsword.passage.SynchronizedPassage
All Implemented Interfaces:
Serializable, Cloneable, Comparable, Key, Passage

final class SynchronizedPassage
extends Object
implements Passage

This is a simple proxy to a real Passage object that makes all accesses synchronized. It is final to give the VM as much hope as possible at being able to inline stuff.

Distribution Licence:
JSword is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
The License is available on the internet here, or by writing to: Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
The copyright to this program is held by it's authors.

Version:
$Id: SynchronizedPassage.java,v 1.14 2005/03/06 20:21:47 dmsmith Exp $
Author:
Joe Walker [joe at eireneh dot com]
See Also:
Licence

Field Summary
private  Passage ref
          The object we are proxying to
private static long serialVersionUID
          Serialization ID
 
Constructor Summary
protected SynchronizedPassage(Passage ref)
          Construct a SynchronizedPassage from a real Passage to which we proxy.
 
Method Summary
 void add(VerseBase that)
          Add this Verse/VerseRange to this Passage
 void addAll(Key key)
          Adds the specified element to this set if it is not already present.
 void addPassageListener(PassageListener li)
          Event Listeners - Add Listener
 void blur(int by, RestrictionType restrict)
          Widen the range of the verses/keys in this list.
 int booksInPassage()
          How many books are there in this Passage
 boolean canHaveChildren()
          Returns true if the receiver is a leaf node and can not have children.
 int chaptersInPassage(int book)
          How many chapters are there in a particular book in this Passage
 void clear()
          Removes all of the elements from this set (optional operation).
 Object clone()
          This needs to be declared here so that it is visible as a method on a derived Passage.
 int compareTo(Object o)
           
 boolean contains(Key key)
          Returns true if this set contains the specified element.
 boolean contains(VerseBase that)
          Returns true if this collection contains all the specified Verse
 boolean containsAll(Passage that)
          Returns true if this Passage contains all of the verses in that Passage
 int countRanges(RestrictionType restrict)
          Like countVerses() that counts VerseRanges instead of Verses Returns the number of fragments in this collection.
 int countVerses()
          Returns the number of verses in this collection.
 Key get(int index)
          Gets a key from a specific point in this list.
 int getChildCount()
          Returns the number of elements in this set (its cardinality).
 String getName()
          A Human readable version of the Key.
 String getOSISName()
          The OSIS defined specification for this Passage.
 String getOverview()
          A summary of the verses in this Passage For example "10 verses in 4 books"
 Key getParent()
          All keys have parents unless they are the root of a Key.
 VerseRange getRangeAt(int offset, RestrictionType restrict)
          Get a specific VerseRange from this collection
 Verse getVerseAt(int offset)
          Get a specific Verse from this collection
 int indexOf(Key that)
          Reverse a Key into the position the key holds in the list
 boolean isEmpty()
          Does this Passage have 0 members
 Iterator iterator()
           
 void optimizeReads()
          For preformance reasons we may well want to hint to the Passage that we have done editing it for now and that it is safe to cache certain values to speed up future reads.
 Iterator rangeIterator(RestrictionType restrict)
          Like verseElements() that iterates over VerseRanges instead of Verses.
 void readDescription(Reader in)
          To be compatible with humans we read/write ourselves to a file that a human can read and even edit.
 void remove(VerseBase that)
          Remove this Verse/VerseRange from this Passage
 void removeAll(Key key)
          Removes the specified elements from this set if it is present.
 void removePassageListener(PassageListener li)
          Event Listeners - Remove Listener
 void retainAll(Key key)
          Removes all but the specified element from this set.
 Passage trimRanges(int count, RestrictionType restrict)
          Ensures that there are a maximum of count VerseRanges in this Passage.
 Passage trimVerses(int count)
          Ensures that there are a maximum of count Verses in this Passage.
 int versesInPassage(int book, int chapter)
          How many chapters are there in a particular book in this Passage Note that versesInPassage(ref, 0, 0) == ref.countVerses()
 void writeDescription(Writer out)
          To be compatible with humans we read/write ourselves to a file that a human can read and even edit.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ref

private Passage ref
The object we are proxying to


serialVersionUID

private static final long serialVersionUID
Serialization ID

See Also:
Constant Field Values
Constructor Detail

SynchronizedPassage

protected SynchronizedPassage(Passage ref)
Construct a SynchronizedPassage from a real Passage to which we proxy.

Parameters:
ref - The real Passage
Method Detail

addAll

public void addAll(Key key)
Description copied from interface: Key
Adds the specified element to this set if it is not already present.

Specified by:
addAll in interface Key
Parameters:
key - element to be added to this set.

removeAll

public void removeAll(Key key)
Description copied from interface: Key
Removes the specified elements from this set if it is present.

Specified by:
removeAll in interface Key
Parameters:
key - object to be removed from this set, if present.

retainAll

public void retainAll(Key key)
Description copied from interface: Key
Removes all but the specified element from this set.

Specified by:
retainAll in interface Key
Parameters:
key - object to be left in this set.

contains

public boolean contains(Key key)
Description copied from interface: Key
Returns true if this set contains the specified element.

Specified by:
contains in interface Key
Parameters:
key - element whose presence in this set is to be tested.
Returns:
true if this set contains the specified element.

getChildCount

public int getChildCount()
Description copied from interface: Key
Returns the number of elements in this set (its cardinality). If this set contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
getChildCount in interface Key
Returns:
the number of elements in this set (its cardinality).

canHaveChildren

public boolean canHaveChildren()
Description copied from interface: Key
Returns true if the receiver is a leaf node and can not have children. Any attempt to add()/remove() wlll throw

Specified by:
canHaveChildren in interface Key

iterator

public Iterator iterator()
Specified by:
iterator in interface Key
Returns:
an iterator over the elements in this set.

get

public Key get(int index)
Description copied from interface: Key
Gets a key from a specific point in this list.

Specified by:
get in interface Key
Parameters:
index - The index of the Key to retrieve
Returns:
The specified key

indexOf

public int indexOf(Key that)
Description copied from interface: Key
Reverse a Key into the position the key holds in the list

Specified by:
indexOf in interface Key
Parameters:
that - The Key to find
Returns:
The index of the key or -1 if the key is not in the list

getParent

public Key getParent()
Description copied from interface: Key
All keys have parents unless they are the root of a Key.

Specified by:
getParent in interface Key
Returns:
The parent of this tree, or null if this Key is the root.

getName

public String getName()
Description copied from interface: Key
A Human readable version of the Key. For Biblical passages this uses short books names, and the shortest sensible rendering eg "Mat 3:1-4" and "Mar 1:1, 3, 5" and "3Jo, Jude"

Specified by:
getName in interface Key
Returns:
a String containing a description of the Key

getOSISName

public String getOSISName()
Description copied from interface: Key
The OSIS defined specification for this Passage. Uses short books names, with "." as a verse part separator.

Specified by:
getOSISName in interface Key
Returns:
a String containing the OSIS description of the verses

getOverview

public String getOverview()
Description copied from interface: Passage
A summary of the verses in this Passage For example "10 verses in 4 books"

Specified by:
getOverview in interface Passage
Returns:
a String containing an overview of the verses

isEmpty

public boolean isEmpty()
Description copied from interface: Key
Does this Passage have 0 members

Specified by:
isEmpty in interface Key
Returns:
true if this set contains no elements.

countVerses

public int countVerses()
Description copied from interface: Passage
Returns the number of verses in this collection. Like Collection.size() This does not mean the Passage needs to use Verses, just that it understands the concept.

Specified by:
countVerses in interface Passage
Returns:
the number of Verses in this collection
See Also:
Verse

countRanges

public int countRanges(RestrictionType restrict)
Description copied from interface: Passage
Like countVerses() that counts VerseRanges instead of Verses Returns the number of fragments in this collection. This does not mean the Passage needs to use VerseRanges, just that it understands the concept.

Specified by:
countRanges in interface Passage
Parameters:
restrict - Do we break ranges at chapter/book boundries
Returns:
the number of VerseRanges in this collection
See Also:
VerseRange

trimVerses

public Passage trimVerses(int count)
Description copied from interface: Passage
Ensures that there are a maximum of count Verses in this Passage. If there were more than count Verses then a new Passage is created containing the Verses from count+1 onwards. If there was not greater than count in the Passage, then the passage remains unchanged, and null is returned.

Specified by:
trimVerses in interface Passage
Parameters:
count - The maximum number of Verses to allow in this collection
Returns:
A new Passage conatining the remaining verses or null
See Also:
Verse

trimRanges

public Passage trimRanges(int count,
                          RestrictionType restrict)
Description copied from interface: Passage
Ensures that there are a maximum of count VerseRanges in this Passage. If there were more than count VerseRanges then a new Passage is created containing the VerseRanges from count+1 onwards. If there was not greater than count in the Passage, then the passage remains unchanged, and null is returned.

Specified by:
trimRanges in interface Passage
Parameters:
count - The maximum number of VerseRanges to allow in this collection
restrict - Do we break ranges at chapter/book boundries
Returns:
A new Passage conatining the remaining verses or null
See Also:
VerseRange

booksInPassage

public int booksInPassage()
Description copied from interface: Passage
How many books are there in this Passage

Specified by:
booksInPassage in interface Passage
Returns:
The number of distinct books

chaptersInPassage

public int chaptersInPassage(int book)
                      throws NoSuchVerseException
Description copied from interface: Passage
How many chapters are there in a particular book in this Passage

Specified by:
chaptersInPassage in interface Passage
Parameters:
book - The book to check (0 for distinct chapters in all books)
Returns:
The number of distinct chapters
Throws:
NoSuchVerseException - if the book is invalid

versesInPassage

public int versesInPassage(int book,
                           int chapter)
                    throws NoSuchVerseException
Description copied from interface: Passage
How many chapters are there in a particular book in this Passage Note that versesInPassage(ref, 0, 0) == ref.countVerses()

Specified by:
versesInPassage in interface Passage
Parameters:
book - The book to check (0 for distinct chapters in all books)
chapter - The chapter to check (0 for distinct verses in all chapters)
Returns:
The number of distinct chapters
Throws:
NoSuchVerseException - if the book/chapter is invalid

getVerseAt

public Verse getVerseAt(int offset)
                 throws ArrayIndexOutOfBoundsException
Description copied from interface: Passage
Get a specific Verse from this collection

Specified by:
getVerseAt in interface Passage
Parameters:
offset - The verse offset (legal values are 0 to countVerses()-1)
Returns:
The Verse
Throws:
ArrayIndexOutOfBoundsException - If the offset is out of range

getRangeAt

public VerseRange getRangeAt(int offset,
                             RestrictionType restrict)
                      throws ArrayIndexOutOfBoundsException
Description copied from interface: Passage
Get a specific VerseRange from this collection

Specified by:
getRangeAt in interface Passage
Parameters:
offset - The verse range offset (legal values are 0 to countRanges()-1)
restrict - Do we break ranges at chapter/book boundries
Returns:
The Verse Range
Throws:
ArrayIndexOutOfBoundsException - If the offset is out of range

rangeIterator

public Iterator rangeIterator(RestrictionType restrict)
Description copied from interface: Passage
Like verseElements() that iterates over VerseRanges instead of Verses. Exactly the same data will be traversed, however using rangeIterator() will usually give less iterations (and never more)

Specified by:
rangeIterator in interface Passage
Parameters:
restrict - Do we break ranges over chapters
Returns:
A list enumerator

contains

public boolean contains(VerseBase that)
Description copied from interface: Passage
Returns true if this collection contains all the specified Verse

Specified by:
contains in interface Passage
Parameters:
that - Verse or VerseRange that may exist in this Passage
Returns:
true if this collection contains that

add

public void add(VerseBase that)
Description copied from interface: Passage
Add this Verse/VerseRange to this Passage

Specified by:
add in interface Passage
Parameters:
that - The Verses to be removed from this Passage

remove

public void remove(VerseBase that)
Description copied from interface: Passage
Remove this Verse/VerseRange from this Passage

Specified by:
remove in interface Passage
Parameters:
that - The Verses to be removed from this Passage

containsAll

public boolean containsAll(Passage that)
Description copied from interface: Passage
Returns true if this Passage contains all of the verses in that Passage

Specified by:
containsAll in interface Passage
Parameters:
that - Passage to be checked for containment in this collection.
Returns:
true if this reference contains all of the Verses in that Passage

clear

public void clear()
Description copied from interface: Key
Removes all of the elements from this set (optional operation). This set will be empty after this call returns (unless it throws an exception).

Specified by:
clear in interface Key

blur

public void blur(int by,
                 RestrictionType restrict)
Description copied from interface: Key
Widen the range of the verses/keys in this list. This is primarily for "find x within n verses of y" type applications.

Specified by:
blur in interface Key
Parameters:
by - The number of verses/keys to widen by
restrict - How should we restrict the blurring?
See Also:
Passage

readDescription

public void readDescription(Reader in)
                     throws IOException,
                            NoSuchVerseException
Description copied from interface: Passage
To be compatible with humans we read/write ourselves to a file that a human can read and even edit. OLB verse.lst integration is a good goal here.

Specified by:
readDescription in interface Passage
Parameters:
in - The stream to read from
Throws:
IOException - If the file/network etc breaks
NoSuchVerseException - If the file was invalid

writeDescription

public void writeDescription(Writer out)
                      throws IOException
Description copied from interface: Passage
To be compatible with humans we read/write ourselves to a file that a human can read and even edit. OLB verse.lst integration is a good goal here.

Specified by:
writeDescription in interface Passage
Parameters:
out - The stream to write to
Throws:
IOException - If the file/network etc breaks

optimizeReads

public void optimizeReads()
Description copied from interface: Passage
For preformance reasons we may well want to hint to the Passage that we have done editing it for now and that it is safe to cache certain values to speed up future reads. Any action taken by this method will be undone simply by making a future edit, and the only loss in calling optimizeReads() is a loss of time if you then persist in writing to the Passage.

Specified by:
optimizeReads in interface Passage

addPassageListener

public void addPassageListener(PassageListener li)
Description copied from interface: Passage
Event Listeners - Add Listener

Specified by:
addPassageListener in interface Passage
Parameters:
li - The listener to add

removePassageListener

public void removePassageListener(PassageListener li)
Description copied from interface: Passage
Event Listeners - Remove Listener

Specified by:
removePassageListener in interface Passage
Parameters:
li - The listener to remove

clone

public Object clone()
Description copied from interface: Passage
This needs to be declared here so that it is visible as a method on a derived Passage. Specifically, in SynchronizedPassage, ref.clone() where ref is only known as a Passage has to have clone visible in this interface.

Specified by:
clone in interface Passage
Overrides:
clone in class Object
Returns:
A complete copy of ourselves

compareTo

public int compareTo(Object o)
Specified by:
compareTo in interface Comparable

Copyright ? 2003-2004