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

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

Introduction

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

Prototype

public static <T extends Comparable<T>> Range<T> between(final T fromInclusive, final T toInclusive) 

Source Link

Document

Obtains a range with the specified minimum and maximum values (both inclusive).

The range uses the natural ordering of the elements to determine where values lie in the range.

The arguments may be passed in the order (min,max) or (max,min).

Usage

From source file:libepg.epg.section.TABLE_ID.java

private TABLE_ID(String tableName, MAX_SECTION_LENGTH maxSectionLength, Class<? extends SectionBody> dataType,
        Integer tableID, Integer... tableIDs) {

    this.tableName = tableName;
    if ((this.tableName == null) || ("".equals(this.tableName))) {
        throw new IllegalArgumentException("???????????");
    }/*from w  w w.  ja  v  a2 s .c  om*/

    List<Integer> t = new ArrayList<>();
    if (tableID != null) {
        t.add(tableID);
    } else {
        throw new NullPointerException("ID??????");
    }

    if (tableIDs != null) {
        t.addAll(Arrays.asList(tableIDs));
    }
    Range<Integer> r = Range.between(0x0, 0xFF);
    for (Integer i : t) {
        if (!r.contains(i)) {
            MessageFormat msg = new MessageFormat(
                    "ID????ID={0}");
            Object[] parameters = { Integer.toHexString(i) };
            throw new IllegalArgumentException(msg.format(parameters));
        }
    }

    Set<Integer> temp = Collections.synchronizedSet(new HashSet<Integer>());
    temp.addAll(t);
    this.tableIDs = Collections.unmodifiableSet(temp);
    this.dataType = dataType;
    this.maxSectionLength = maxSectionLength;
}

From source file:com.rockhoppertech.music.chord.ChordProgression.java

/**
 * Return the Chord that matches if the beat is between the Chord's start
 * and end./*from w w w  .j a  va  2  s  .com*/
 * 
 * @param beat
 *            a beat
 * @return a Chord or null if not chords at beat
 */
public Chord getChordAtBeat(final double beat) {
    logger.debug("looking for  sb {}", beat);
    for (final Chord n : this) {
        final double s = n.getStartBeat();
        final double e = n.getEndBeat();
        logger.debug("checking sb {} of chord {}", s, n);
        Range<Double> range = Range.between(s, e - .0001);
        logger.debug("range {}", range);
        if (range.contains(beat)) {
            logger.debug("returning chord {}", n);
            return n;
        }
    }
    return null;
}

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

public static TimeSpan fromPropertyString(String pString) {
    String[] split = pString.split(",");
    try {/* ww w .ja  v a 2  s.c o  m*/
        long start = Long.parseLong(split[0]);
        long end = Long.parseLong(split[1]);
        boolean daily = Boolean.parseBoolean(split[2]);
        int dir = Integer.parseInt(split[3]);
        TimeSpan t = new TimeSpan(Range.between(start, end), daily);
        switch (dir) {
        case 0:
            t.setDirection(DIRECTION.SEND);
            break;
        case 1:
            t.setDirection(DIRECTION.ARRIVE);
            break;
        }
        return t;
    } catch (Exception ignored) {
    }
    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>//w  w w .j a v a 2s.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.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./*w w w.j  a  va2  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.ui.algo.AttackTimePanel.java

/**
 * Get the currently set up send span/*from  w  w  w .j  ava2 s. c o  m*/
 *
 * @return TimeSpan The send span
 */
private TimeSpan getSendSpan() {
    TimeSpan start = null;
    Range<Long> range = Range.between(
            Math.round(jSendTimeFrame.getMinimumColoredValue()) * DateUtils.MILLIS_PER_HOUR,
            Math.round(jSendTimeFrame.getMaximumColoredValue()) * DateUtils.MILLIS_PER_HOUR);
    if (Objects.equals(range.getMinimum(), range.getMaximum()) && !jExactTimeButton.isSelected()) {
        return null;
    }

    if (jAlwaysButton.isSelected()) {
        start = new TimeSpan(range, true);
    } else if (jDayButton.isSelected()) {
        range = Range.between(dateTimeField.getSelectedDate().getTime() + range.getMinimum(),
                dateTimeField.getSelectedDate().getTime() + range.getMaximum());
        start = new TimeSpan(range, false);
    } else if (jExactTimeButton.isSelected()) {
        start = new TimeSpan(dateTimeField.getSelectedDate());
    }
    if (start != null) {
        start.setDirection(TimeSpan.DIRECTION.SEND);
    }

    return start;
}

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

public List<Range<Long>> startTimespansToRanges() {
    List<Range<Long>> ranges = new LinkedList<>();
    Date startDate = DateUtils.truncate(new Date(startNotBefore), Calendar.DATE);

    for (TimeSpan span : sendTimeSpans) {
        if (!span.isValidAtEveryDay()) {
            Range<Long> range;
            //just copy range
            if (span.isValidAtExactTime()) {
                range = Range.between(span.getSpan().getMinimum(),
                        span.getSpan().getMaximum() + fixedStartTimeRangeSize);
            } else {
                range = Range.between(span.getSpan().getMinimum(), span.getSpan().getMaximum());
            }//from   w ww .jav  a2  s .  c o m

            if (range.getMaximum() > System.currentTimeMillis()) {
                if (range.getMinimum() <= System.currentTimeMillis()) {
                    //rebuild Range
                    range = Range.between(System.currentTimeMillis(), range.getMaximum());
                }
                //add range only if it is in future
                ranges.add(range);
            }
        } else {
            //span is valid for every day
            Date thisDate = new Date(startDate.getTime());
            //go through all days from start to end
            while (thisDate.getTime() < startNotAfter) {
                long spanStart = thisDate.getTime() + span.getSpan().getMinimum();
                long spanEnd = thisDate.getTime() + span.getSpan().getMaximum();
                Range<Long> newRange = null;
                //check span location relative to start frame
                if (spanStart >= startNotBefore && spanEnd > startNotBefore && spanStart < startNotAfter
                        && spanEnd <= startNotAfter) {
                    //|----------| (startNotBefore - startNotAfter)
                    //  |----| (SpanStart - SpanEnd)
                    newRange = Range.between(spanStart, spanEnd);
                } else if (spanStart < startNotBefore && spanEnd > startNotBefore && spanStart < startNotAfter
                        && spanEnd <= startNotAfter) {
                    //  |----------| (startNotBefore - startNotAfter)
                    //|----| (SpanStart - SpanEnd)
                    //set span start to 'startNotBefore'
                    newRange = Range.between(startNotBefore, spanEnd);
                } else if (spanStart <= startNotBefore && spanEnd > startNotBefore && spanStart > startNotAfter
                        && spanEnd >= startNotAfter) {
                    //  |----------| (startNotBefore - startNotAfter)
                    //|--------------| (SpanStart - SpanEnd)
                    //set span start to 'startNotBefore'
                    newRange = Range.between(startNotBefore, startNotAfter);
                } else if (spanStart >= startNotBefore && spanEnd > startNotBefore && spanStart < startNotAfter
                        && spanEnd >= startNotAfter) {
                    //|----------| (startNotBefore - startNotAfter)
                    //    |---------| (SpanStart - SpanEnd)
                    //set span start to 'startNotBefore'
                    newRange = Range.between(spanStart, startNotAfter);
                }

                if (newRange != null) {
                    if (newRange.getMinimum() < System.currentTimeMillis()) {
                        //check minimum as current minimum is in past
                        if (newRange.getMaximum() > System.currentTimeMillis()) {
                            newRange = Range.between(System.currentTimeMillis(), newRange.getMaximum());
                            ranges.add(newRange);
                        } //ignore as entire range is in past
                    } else {
                        //add range as it is in future
                        ranges.add(newRange);
                    }
                }
                //increment current date by one day
                thisDate = DateUtils.addDays(thisDate, 1);
            }
        }
    }
    Collections.sort(ranges, new Comparator<Range<Long>>() {
        @Override
        public int compare(Range<Long> o1, Range<Long> o2) {
            return o1.getMinimum().compareTo(o2.getMinimum());
        }
    });
    return ranges;
}

From source file:de.tor.tribes.ui.algo.AttackTimePanel.java

/**
 * Get the currently set up arrive span//  w  ww .ja va2s .com
 *
 * @return TimeSpan The arrive span
 */
private TimeSpan getArriveSpan() {
    TimeSpan arrive = null;
    Range<Long> range = Range.between(
            Math.round(jSendTimeFrame.getMinimumColoredValue()) * DateUtils.MILLIS_PER_HOUR,
            Math.round(jSendTimeFrame.getMaximumColoredValue()) * DateUtils.MILLIS_PER_HOUR);
    if (Objects.equals(range.getMinimum(), range.getMaximum()) && !jExactTimeButton.isSelected()) {
        return null;
    }

    if (jAlwaysButton.isSelected()) {
        arrive = new TimeSpan(range, true);
    } else if (jDayButton.isSelected()) {
        range = Range.between(dateTimeField.getSelectedDate().getTime() + range.getMinimum(),
                dateTimeField.getSelectedDate().getTime() + range.getMaximum());
        arrive = new TimeSpan(range, false);
    } else if (jExactTimeButton.isSelected()) {
        arrive = new TimeSpan(dateTimeField.getSelectedDate());
    }
    if (arrive != null) {
        arrive.setDirection(TimeSpan.DIRECTION.ARRIVE);
    }

    return arrive;
}

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 a  v 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.tor.tribes.ui.views.DSWorkbenchFarmManager.java

public Range<Integer> getFarmRange(FARM_CONFIGURATION pConfig) {
    if (pConfig == null) {
        pConfig = FARM_CONFIGURATION.C;/*from  w w  w  .  j a va  2s.co m*/
    }
    switch (pConfig) {
    case A:
        return Range.between(UIHelper.parseIntFromField(jMinFarmRuntimeA, 0),
                UIHelper.parseIntFromField(jMaxFarmRuntimeA, 60));
    case B:
        return Range.between(UIHelper.parseIntFromField(jMinFarmRuntimeB, 0),
                UIHelper.parseIntFromField(jMaxFarmRuntimeB, 60));
    case K:
        return Range.between(UIHelper.parseIntFromField(jMinFarmRuntimeK, 0),
                UIHelper.parseIntFromField(jMaxFarmRuntimeK, 60));
    default:
        return Range.between(UIHelper.parseIntFromField(jMinFarmRuntimeC, 0),
                UIHelper.parseIntFromField(jMaxFarmRuntimeC, 60));
    }
}