*/
public class FieldsTables
{
- private static final byte[] BYTES_EMPTY = new byte[0];
-
private static final class GenericPropertyNodeComparator implements
Comparator<GenericPropertyNode>
{
* we have start node. end offset points to next node, separator or
* end
*/
- int nextNodePositionInArray = Arrays.binarySearch(
- nodes,
- next + 1,
- endOffsetExclusive,
- new GenericPropertyNode( startNode.getEnd(), startNode
- .getEnd() + 1, BYTES_EMPTY ), comparator );
+ int nextNodePositionInArray = binarySearch( nodes, next + 1,
+ endOffsetExclusive, startNode.getEnd() );
if ( nextNodePositionInArray < 0 )
{
/*
GenericPropertyNode separatorNode = nextNode;
PlexOfField separatorPlexOfField = nextPlexOfField;
- int endNodePositionInArray = Arrays.binarySearch( nodes,
+ int endNodePositionInArray = binarySearch( nodes,
nextNodePositionInArray, endOffsetExclusive,
- new GenericPropertyNode( separatorNode.getEnd(),
- separatorNode.getEnd() + 1, BYTES_EMPTY ),
- comparator );
+ separatorNode.getEnd() );
if ( endNodePositionInArray < 0 )
{
/*
}
}
+ /**
+ * This is port and adaptation of Arrays.binarySearch from Java 6 (Apache
+ * Harmony).
+ */
+ public static <T> int binarySearch( GenericPropertyNode[] array,
+ int startIndex, int endIndex, int requiredStartOffset )
+ {
+ checkIndexForBinarySearch( array.length, startIndex, endIndex );
+
+ int low = startIndex, mid = -1, high = endIndex - 1, result = 0;
+ while ( low <= high )
+ {
+ mid = ( low + high ) >>> 1;
+ int midStart = array[mid].getStart();
+
+ if ( midStart == requiredStartOffset )
+ {
+ return mid;
+ }
+ else if ( midStart < requiredStartOffset )
+ {
+ low = mid + 1;
+ }
+ else
+ {
+ high = mid - 1;
+ }
+ }
+ if ( mid < 0 )
+ {
+ int insertPoint = endIndex;
+ for ( int index = startIndex; index < endIndex; index++ )
+ {
+ if ( requiredStartOffset < array[index].getStart() )
+ {
+ insertPoint = index;
+ }
+ }
+ return -insertPoint - 1;
+ }
+ return -mid - ( result >= 0 ? 1 : 2 );
+ }
+
+ private static void checkIndexForBinarySearch( int length, int start,
+ int end )
+ {
+ if ( start > end )
+ {
+ throw new IllegalArgumentException();
+ }
+ if ( length < end || 0 > start )
+ {
+ throw new ArrayIndexOutOfBoundsException();
+ }
+ }
+
public Field lookupFieldByStartOffset( int documentPart, int offset )
{
Map<Integer, Field> map = _fieldsByOffset.get( Integer