Example usage for org.apache.commons.lang3 Range containsRange

List of usage examples for org.apache.commons.lang3 Range containsRange

Introduction

In this page you can find the example usage for org.apache.commons.lang3 Range containsRange.

Prototype

public boolean containsRange(final Range<T> otherRange) 

Source Link

Document

Checks whether this range contains all the elements of the specified range.

This method may fail if the ranges have two different comparators or element types.

Usage

From source file:de.julielab.jcore.utility.JCoReAnnotationTools.java

/**
 * Returns, in ascending order, all annotations of type <tt>cls</tt> that are completely included - perhaps with
 * having the same begin and/or end as the <tt>focusAnnotation</tt> - in <tt>focusAnnotation</tt>.
 * // ww w . j av  a 2 s  .c o  m
 * @param aJCas
 * @param focusAnnotation
 * @param cls
 * @return
 */
@SuppressWarnings("unchecked")
public static <T extends Annotation> List<T> getIncludedAnnotations(JCas aJCas, Annotation focusAnnotation,
        Class<T> cls) {
    FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator();

    // for debugging: print out absolutely all annotations
    // cursor.moveToFirst();
    // while (cursor.isValid()) {
    // System.out.println(cursor.get());
    // cursor.moveToNext();
    // }

    cursor.moveTo(focusAnnotation);
    if (!cursor.isValid())
        throw new IllegalArgumentException(
                "Given FocusAnnotation was not found in the JCas' annotation index: " + focusAnnotation);

    // The annotations are sorted by begin offset. So go to the first annotation with a lower begin offset compared
    // to the focusAnnotation.
    while (cursor.isValid() && cursor.get().getBegin() >= focusAnnotation.getBegin()) {
        cursor.moveToPrevious();
    }
    if (!cursor.isValid())
        cursor.moveToFirst();
    else
        cursor.moveToNext();

    // Now that we have our starting point, we go to the right as long as there is a possibility to still find
    // annotations included in the focusAnnotation, i.e. as long the current begin offset is still lower (or equal
    // for the weird case of zero-length-annotations) than the
    // end offset of the focusAnnotation
    Annotation currentAnnotation = null;
    List<T> includedAnnotations = new ArrayList<>();
    while (cursor.isValid() && (currentAnnotation = cursor.get()).getBegin() <= focusAnnotation.getEnd()) {
        if (!cls.isInstance(currentAnnotation)) {
            cursor.moveToNext();
            continue;
        }
        Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd());
        Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd());
        if (cursor.isValid() && cls.isInstance(currentAnnotation) && focusRange.containsRange(currentRange))
            includedAnnotations.add((T) currentAnnotation);
        cursor.moveToNext();
    }
    return includedAnnotations;
}

From source file:de.julielab.jcore.utility.JCoReAnnotationTools.java

/**
 * Returns the leftmost annotation of type <tt>cls</tt> that completely includes <tt>focusAnnotation</tt>. That is,
 * if multiple annotations of type <tt>cls</tt> include <tt>focusAnnotation</tt>, the one with the lowest begin
 * offset will be chosen./*w  w w . java  2 s  . c  om*/
 * <p>
 * This algorithm has <tt>O(n)</tt> runtime with <tt>n</tt> being the number of annotations in the annotation index.
 * </p>
 * <p>
 * TODO: A start offset parameter could be introduced from where to start looking. This way, when iterating over a
 * number of different focusAnnotations in ascending order, one would have only to check from focusAnnotation to
 * focusAnnotation and not always from the very beginning of the annotation index. Same thing for
 * getPartiallyOverlappingAnnotation().
 * </p>
 * 
 * @param aJCas
 * @param focusAnnotation
 * @param cls
 * @return the leftmost annotation of type <tt>cls</tt> that completely includes <tt>focusAnnotation</tt>.
 */
@SuppressWarnings("unchecked")
public static <T extends Annotation> T getIncludingAnnotation(JCas aJCas, Annotation focusAnnotation,
        Class<T> cls) {
    FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator();

    // Annotations are sorted by begin offset and may be arbitrarily long. Thus we just have to start from the
    // beginning.
    cursor.moveToFirst();

    // Now go to the right as long as we don't yet overlap with the focus annotation, then stop.
    Annotation currentAnnotation = null;
    while (cursor.isValid() && ((currentAnnotation = cursor.get()).getEnd() < focusAnnotation.getEnd()
            || !cls.isInstance(currentAnnotation))) {
        cursor.moveToNext();
    }

    // Check whether we have found an overlapping annotation.
    Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd());
    Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd());
    if (cursor.isValid() && cls.isInstance(currentAnnotation) && currentRange.containsRange(focusRange))
        return (T) cursor.get();

    return null;
}

From source file:de.julielab.jcore.utility.JCoReAnnotationTools.java

/**
 * Returns the nearest annotation of class <tt>cls</tt> to <tt>focusAnnotation</tt>, i.e. the one (or just one, if
 * multiple exist) with the highest start-offset that completely overlaps <tt>focusAnnotation</tt>.
 * <p>/*w  w w  .j  a  v a 2 s  . c  o  m*/
 * This method has nice performance properties when it is known that the annotation looked for is near, e.g. finding
 * the nearest token or sentence.
 * </p>
 * 
 * @param aJCas
 * @param focusAnnotation
 * @param cls
 * @return the leftmost annotation of type <tt>cls</tt> that completely includes <tt>focusAnnotation</tt>.
 */
@SuppressWarnings("unchecked")
public static <T extends Annotation> T getNearestIncludingAnnotation(JCas aJCas, Annotation focusAnnotation,
        Class<T> cls) {
    FSIterator<Annotation> cursor = aJCas.getAnnotationIndex().iterator();

    if (!cursor.isValid())
        throw new IllegalArgumentException(
                "Given FocusAnnotation was not found in the JCas' annotation index: " + focusAnnotation);

    // The annotations are sorted by begin offset. So go to the first annotation with a larger begin offset compared
    // to the focusAnnotation. Afterwards we we search for an including annotation to the left.
    cursor.moveTo(focusAnnotation);
    while (cursor.isValid() && cursor.get().getBegin() <= focusAnnotation.getBegin()) {
        cursor.moveToNext();
    }
    if (!cursor.isValid())
        cursor.moveTo(focusAnnotation);
    else
        cursor.moveToPrevious();

    // Now that we have our starting point, we go to the left until we find the first annotation of correct type
    // completely overlapping the focus annotation.
    while (cursor.isValid()) {
        Annotation currentAnnotation = cursor.get();
        if (!cls.isInstance(currentAnnotation)) {
            cursor.moveToPrevious();
            continue;
        }
        Range<Integer> currentRange = Range.between(currentAnnotation.getBegin(), currentAnnotation.getEnd());
        Range<Integer> focusRange = Range.between(focusAnnotation.getBegin(), focusAnnotation.getEnd());
        if (cursor.isValid() && cls.isInstance(currentAnnotation) && currentRange.containsRange(focusRange))
            return (T) currentAnnotation;
        cursor.moveToPrevious();
    }
    return null;
}