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

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

Introduction

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

Prototype

public boolean isOverlappedBy(final Range<T> otherRange) 

Source Link

Document

Checks whether this range is overlapped by the specified range.

Two ranges overlap if there is at least one element in common.

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 the leftmost annotation of type <tt>cls</tt> that overlaps <tt>focusAnnotation</tt>. That is, if multiple
 * annotations of type <tt>cls</tt> overlap with <tt>focusAnnotation</tt>, the one with the lowest begin offset will
 * be chosen./*from  w w w .j av a2  s . c  o m*/
 * <p>
 * The two annotations may overlap in any way (partial, nested, inclusion, exact match). 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
 * getIncludingAnnotation().
 * </p>
 * 
 * @param aJCas
 * @param focusAnnotation
 * @param cls
 * @return the leftmost annotation of type <tt>cls</tt> that overlaps <tt>focusAnnotation</tt>.
 */
@SuppressWarnings("unchecked")
public static <T extends Annotation> T getPartiallyOverlappingAnnotation(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.getBegin()
            || !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.isOverlappedBy(focusRange))
        return (T) cursor.get();

    return null;
}

From source file:de.tor.tribes.util.algo.types.TimeFrame.java

/**
 * Check if a movement with the provided runtime is possible for this
 * AttackFitter//w  ww.  j  a v  a2s . c  om
 *
 * @param pRuntime Runtime to check
 * @param pVillage Village for which the runtime is valid
 * @return boolean TRUE=Runtime might be fitted if not all send times are
 * already used
 */
public boolean isMovementPossible(long pRuntime) {
    if (startRanges == null) {
        startRanges = startTimespansToRanges();
    }
    if (arriveRanges == null) {
        arriveRanges = arriveTimespansToRanges();
    }

    for (Range<Long> currentStartRange : startRanges) {
        Range<Long> arriveRangeForStartRange = Range.between(currentStartRange.getMinimum() + pRuntime,
                currentStartRange.getMaximum() + pRuntime);
        for (Range<Long> currentArriveRange : arriveRanges) {
            if (currentArriveRange.isOverlappedBy(arriveRangeForStartRange)) {
                //movement with 'pRuntime' starting in 'currentStartRange' will arrive withing 'currentArriveRange'
                return true;
            }
        }
    }
    //no overlapping range was found
    return false;
}

From source file:de.tor.tribes.util.algo.types.TimeFrame.java

/**
 * Returns an arrive date that fits into this AttackFitter and is based on the
 * provided runtime//from w  ww.j  av  a  2 s .c  o  m
 *
 * @param pRuntime Runtime to fit into
 * @param pVillage Village for which the arrive date should be valid
 * @param pUsedSendTimes Already used send times (possible times are checked
 * in steps of 1ms). This argument may be 'null'. Then send times are
 * not checked.
 * @return Date Fitted arrive time
 */
public Date getFittedArriveTime(long pRuntime, List<Long> pUsedSendTimes) {
    List<Range<Long>> startRanges = startTimespansToRanges();
    List<Range<Long>> arriveRanges = arriveTimespansToRanges();
    List<Range<Long>> possible = new ArrayList();
    for (Range<Long> currentStartRange : startRanges) {
        Range<Long> arriveRangeForStartRange = Range.between(currentStartRange.getMinimum() + pRuntime,
                currentStartRange.getMaximum() + pRuntime);
        for (Range<Long> currentArriveRange : arriveRanges) {
            if (currentArriveRange.isOverlappedBy(arriveRangeForStartRange)) {
                //movement possible for these 'currentStartRange' and 'currentArriveRange' so fit runtime into
                //           |-----------|
                //   |--------------|
                long minArrive = currentArriveRange.getMinimum();
                long minArriveForStartRange = arriveRangeForStartRange.getMinimum();
                long checkStart = 0;
                if (minArrive < minArriveForStartRange) {
                    //|----------- (Arrive)
                    //   |-------------- (ArriveForStart)
                    //check everything beginning with 'minArriveForStartRange'
                    checkStart = minArriveForStartRange;
                } else if (minArriveForStartRange <= minArrive) {
                    //     |----------- (Arrive)
                    //|-------------- (ArriveForStart)
                    //check everything beginning with 'minArrive'
                    checkStart = minArrive;
                }
                long maxArrive = currentArriveRange.getMaximum();
                long maxArriveForStartRange = arriveRangeForStartRange.getMaximum();
                long checkEnd = 0;
                if (maxArrive < maxArriveForStartRange) {
                    //-----------| (Arrive)
                    //---------------| (ArriveForStart)
                    //check everything until 'maxArrive'
                    checkEnd = maxArrive;
                } else if (maxArriveForStartRange <= maxArrive) {
                    //-------------| (Arrive)
                    //---------| (ArriveForStart)
                    //check everything until 'maxArriveForStartRange'
                    checkEnd = maxArriveForStartRange;
                }

                if (checkStart != checkEnd) {
                    //We are handling real Ranges
                    int cnt = 0;
                    while (cnt < 20) {
                        long arriveTime = checkStart + Math.round(Math.random() * (checkEnd - checkStart));
                        if (pUsedSendTimes == null || !pUsedSendTimes.contains(arriveTime - pRuntime)) {
                            if (pUsedSendTimes != null) {
                                pUsedSendTimes.add(arriveTime - pRuntime);
                            }
                            return new Date(arriveTime);
                        }
                        cnt++;
                    }
                    //We found nothing with random, try systematic search
                    for (long i = checkStart; i <= checkEnd; i++) {
                        if (!pUsedSendTimes.contains(i - pRuntime)) {
                            pUsedSendTimes.add(i - pRuntime);
                            return new Date(i);
                        }
                    }
                } else {
                    //We are handling a fixed arrive Time
                    if (pUsedSendTimes == null || !pUsedSendTimes.contains(checkStart - pRuntime)) {
                        if (pUsedSendTimes != null) {
                            pUsedSendTimes.add(checkStart - pRuntime);
                        }
                        return new Date(checkStart);
                    }
                }
                possible.add(Range.between(checkStart, checkEnd));
            }
        }
    }

    if (!possible.isEmpty()) {
        long cnt = 0;
        for (Range<Long> r : possible) {
            cnt += r.getMaximum() - r.getMinimum() + 1;
        }
        cnt = (long) (Math.random() * cnt);
        for (Range<Long> r : possible) {
            Long span = r.getMaximum() - r.getMinimum() + 1;
            if (cnt < span) {
                return new Date(r.getMinimum() + cnt);
            }
            cnt -= span;
        }
    }

    return null;
}

From source file:com.rockhoppertech.music.Note.java

/**
 * Determines if the time range of the given note (start beat to end beat)
 * overlaps the time range of this note.
 * <p>//from w w  w.  j a  v a 2  s.  c  o  m
 * Uses a tiny fudge factor to exclude start times that match end times.
 * 
 * @param note
 *            - the guy to compare to
 * @return
 */
public boolean isOverlapping(final Note note) {
    final double fudge = .0000001;
    Range<Double> range = Range.between(this.getStartBeat(), this.getEndBeat());
    Range<Double> range2 = Range.between(note.getStartBeat() + fudge, note.getEndBeat());
    return range.isOverlappedBy(range2);

    // NumberRange range = new NumberRange(this.getStartBeat(), this
    // .getEndBeat());
    // NumberRange range2 = new NumberRange(note.getStartBeat() +
    // Double.MIN_VALUE, note
    // .getEndBeat());
    // return range.overlapsRange(range2);
}

From source file:de.tor.tribes.types.TimeSpan.java

public boolean intersects(TimeSpan pSpan) {
    if (!this.getDirection().equals(pSpan.getDirection())) {
        //different directions
        return false;
    }/*w  w  w.ja  v a2 s . c  om*/

    //one of the spans uses manual Time (new intersect)
    Range<Long> thisSpan = this.getSpan();
    Range<Long> theOtherSpan = pSpan.getSpan();

    if (this.isValidAtEveryDay() || pSpan.isValidAtEveryDay()) {
        if (this.isValidAtSpecificDay() || pSpan.isValidAtSpecificDay()) {
            //remove day Information
            Long thisStart = DateUtils.getFragmentInMilliseconds(new Date(thisSpan.getMinimum()),
                    Calendar.DATE);
            Long thisEnd = DateUtils.getFragmentInMilliseconds(new Date(thisSpan.getMaximum()), Calendar.DATE);
            thisSpan = Range.between(thisStart, thisEnd);

            Long otherStart = DateUtils.getFragmentInMilliseconds(new Date(theOtherSpan.getMinimum()),
                    Calendar.DATE);
            Long otherEnd = DateUtils.getFragmentInMilliseconds(new Date(theOtherSpan.getMaximum()),
                    Calendar.DATE);

            theOtherSpan = Range.between(otherStart, otherEnd);

            return thisSpan.isOverlappedBy(theOtherSpan);
        } else if (this.isValidAtEveryDay() && pSpan.isValidAtEveryDay()) {
            //both valid at every Day - just compare spans
            return thisSpan.isOverlappedBy(theOtherSpan);
        } else {
            //one span is for everyDay the other is over multiple Days
            //manual intersect
            Range<Long> always;
            Range<Long> manual;
            if (this.isValidAtEveryDay()) {
                always = thisSpan;
                manual = theOtherSpan;
            } else {
                always = theOtherSpan;
                manual = thisSpan;
            }

            long manualDate = DateUtils.truncate(new Date(manual.getMinimum()), Calendar.DATE).getTime();
            long manualStart = manual.getMinimum() - manualDate;
            long manualEnd = manual.getMaximum() - manualDate;

            if (manualEnd - manualStart > DateUtils.MILLIS_PER_DAY) {
                //must intersect somehow because span is longer than 1 Day
                return true;
            }
            //direct intersection
            manual = Range.between(manualStart, manualEnd);
            if (always.isOverlappedBy(manual))
                return true;

            //should not be possible, because it should be handeld by isValidAtSpecificDay
            if (manualEnd <= DateUtils.MILLIS_PER_DAY)
                return false;

            //maybe intersection at next day
            manual = Range.between(new Long(0), manualEnd - DateUtils.MILLIS_PER_DAY);
            return always.isOverlappedBy(manual);
        }
    }

    return thisSpan.isOverlappedBy(theOtherSpan);
}

From source file:org.kalypso.model.wspm.ui.view.chart.layer.wsp.WaterlevelRenderWorker.java

private WaterlevelRenderSegment buildSegment(final LineSegment segment, final Range<Double> restriction)
        throws Exception {
    final Range<Double> segmentRange = Range.between(segment.p0.x, segment.p1.x);
    if (restriction != null && !segmentRange.isOverlappedBy(restriction))
        return null;

    final Polygon area = buildSegmentArea(segment);

    return new WaterlevelRenderSegment(segment, area);
}