public static Iterator < List < TermOccurrence > > occurrenceChunkIterator ( Collection < TermOccurrence > occurrences ) { List < TermOccurrence > asList = Lists . newArrayList ( occurrences ) ; Collections . sort ( asList , TermOccurrenceUtils . uimaNaturalOrder ) ; final Iterator < TermOccurrence > it = asList . iterator ( ) ; return new AbstractIterator < List < TermOccurrence > > ( ) { private List < TermOccurrence > currentChunk = Lists . newArrayList ( ) ; @ Override protected List < TermOccurrence > computeNext ( ) { while ( it . hasNext ( ) ) { TermOccurrence next = it . next ( ) ; if ( currentChunk . isEmpty ( ) || hasOverlappingOffsets ( next , currentChunk ) ) currentChunk . add ( next ) ; else { List < TermOccurrence > ret = copyAndReinit ( ) ; currentChunk . add ( next ) ; return ret ; } } if ( ! currentChunk . isEmpty ( ) ) { return copyAndReinit ( ) ; } else return endOfData ( ) ; } private List < TermOccurrence > copyAndReinit ( ) { List < TermOccurrence > copy = Lists . newArrayList ( currentChunk ) ; currentChunk = Lists . newArrayList ( ) ; return copy ; } } ; }
Returns a virtual iterator on chunks of an occurrence collection .