public static List selectPreceding()

in uimafit-core/src/main/java/org/apache/uima/fit/util/CasUtil.java [1220:1274]


  public static List<AnnotationFS> selectPreceding(CAS cas, Type type, AnnotationFS anchor,
          int count) {
    requireAnnotationType(cas, type);

    List<AnnotationFS> precedingAnnotations = new ArrayList<AnnotationFS>();

    // Seek annotation in index
    // withSnapshotIterators() not needed here since we copy the FSes to a list anyway
    FSIterator<AnnotationFS> itr = cas.getAnnotationIndex(type).iterator();
    itr.moveTo(anchor);

    // If the insertion point is beyond the index, move back to the last.
    if (!itr.isValid()) {
      itr.moveToLast();
      if (!itr.isValid()) {
        return precedingAnnotations;
      }
    }

    int anchorBegin = anchor.getBegin();

    // Zero-width annotations are in the index *after* the wider annotations starting at the same
    // location, but we would consider a zero-width annotation at the beginning of a larger
    // reference annotation to be preceding the larger one. So we need to seek right
    // for any relevant zero-with annotations.
    while (itr.isValid() && itr.get().getBegin() == anchorBegin) {
      itr.moveToNext();
      if (!itr.isValid()) {
        itr.moveToLast();
        break;
      }
    }

    // make sure we're past the beginning of the reference annotation
    while (itr.isValid() && itr.get().getEnd() > anchorBegin) {
      itr.moveToPrevious();
    }

    // add annotations from the iterator into the result list
    for (int i = 0; i < count && itr.isValid(); itr.moveToPrevious()) {
      AnnotationFS cur = itr.get();

      int curEnd = cur.getEnd();

      if ((curEnd <= anchorBegin || (cur.getBegin() == curEnd && curEnd == anchorBegin))
              && cur != anchor) {
        precedingAnnotations.add(cur);
        i++;
      }
    }

    // return in correct order
    Collections.reverse(precedingAnnotations);
    return precedingAnnotations;
  }