org.apache.commons.lang3.Range类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(113)

本文整理了Java中org.apache.commons.lang3.Range类的一些代码示例,展示了Range类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Range类的具体详情如下:
包路径:org.apache.commons.lang3.Range
类名称:Range

Range介绍

[英]An immutable range of objects from a minimum to maximum point inclusive.

The objects need to either be implementations of Comparableor you need to supply a Comparator.

#ThreadSafe# if the objects and comparator are thread-safe
[中]从最小点到最大点(包括最大点)的不变对象范围。
对象需要是Comparable的实现,或者您需要提供一个Comparator。
#线程安全#如果对象和比较器是线程安全的

代码示例

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * Calculate the intersection of {@code this} and an overlapping Range.
 * @param other overlapping Range
 * @return range representing the intersection of {@code this} and {@code other} ({@code this} if equal)
 * @throws IllegalArgumentException if {@code other} does not overlap {@code this}
 * @since 3.0.1
 */
public Range<T> intersectionWith(final Range<T> other) {
  if (!this.isOverlappedBy(other)) {
    throw new IllegalArgumentException(String.format(
      "Cannot calculate intersection with non-overlapping range %s", other));
  }
  if (this.equals(other)) {
    return this;
  }
  final T min = getComparator().compare(minimum, other.minimum) < 0 ? other.minimum : minimum;
  final T max = getComparator().compare(maximum, other.maximum) < 0 ? maximum : other.maximum;
  return between(min, max, getComparator());
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testRangeOfChars() {
  final Range<Character> chars = Range.between('a', 'z');
  assertTrue(chars.contains('b'));
  assertFalse(chars.contains('B'));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testGetMaximum() {
  assertEquals(20, (int) intRange.getMaximum());
  assertEquals(20L, (long) longRange.getMaximum());
  assertEquals(20f, floatRange.getMaximum(), 0.00001f);
  assertEquals(20d, doubleRange.getMaximum(), 0.00001d);
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <p>Checks where the specified element occurs relative to this range.</p>
 *
 * <p>The API is reminiscent of the Comparable interface returning {@code -1} if
 * the element is before the range, {@code 0} if contained within the range and
 * {@code 1} if the element is after the range. </p>
 *
 * @param element  the element to check for, not null
 * @return -1, 0 or +1 depending on the element's location relative to the range
 */
public int elementCompareTo(final T element) {
  // Comparable API says throw NPE on null
  Validate.notNull(element, "Element is null");
  if (isAfter(element)) {
    return -1;
  } else if (isBefore(element)) {
    return 1;
  } else {
    return 0;
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test(expected = IllegalArgumentException.class)
public void testIntersectionWithNonOverlapping() {
  intRange.intersectionWith(Range.between(0, 9));
}

代码示例来源:origin: offbynull/portmapper

Range<Long> leaseDurationRange = getLeaseDurationRange();
long leaseDuration;
if (leaseDurationRange.isBefore(lifetime)) {
  leaseDuration = leaseDurationRange.getMaximum();
} else if (leaseDurationRange.isAfter(lifetime)) {
  leaseDuration = leaseDurationRange.getMinimum();
} else {
  leaseDuration = lifetime;
Validate.validState(externalPortRange.contains((long) externalPort),
    "Router reports external port mappings as %s", externalPortRange);
    externalPortRange.getMinimum().intValue(), // should never be < 1
    externalPortRange.getMaximum().intValue() + 1); // should never be > 65535

代码示例来源:origin: org.umlg/sqlg-core

/**
 * range condition
 *
 * @param r range
 * @return
 */
default String getRangeClause(Range<Long> r) {
  return "LIMIT " + (r.getMaximum() - r.getMinimum()) + " OFFSET " + r.getMinimum();
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <p>Obtains a range using the specified element as both the minimum
 * and maximum in this range.</p>
 *
 * <p>The range uses the specified {@code Comparator} to determine where
 * values lie in the range.</p>
 *
 * @param <T> the type of the elements in this range
 * @param element  the value to use for this range, must not be {@code null}
 * @param comparator  the comparator to be used, null for natural ordering
 * @return the range object, not null
 * @throws IllegalArgumentException if the element is null
 * @throws ClassCastException if using natural ordering and the elements are not {@code Comparable}
 */
public static <T> Range<T> is(final T element, final Comparator<T> comparator) {
  return between(element, element, comparator);
}

代码示例来源:origin: offbynull/portmapper

long min;
if (minStr == null) {
  min = maxRange.getMinimum();
} else {
  try {
    min = Long.parseLong(minStr.trim());
  } catch (NumberFormatException nfe) {
    min = maxRange.getMinimum();
long max;
if (maxStr == null) {
  max = maxRange.getMaximum();
} else {
  try {
    max = Long.parseLong(maxStr.trim());
  } catch (NumberFormatException nfe) {
    max = maxRange.getMaximum();
if (min > max || min < maxRange.getMinimum() || max > maxRange.getMaximum()) {
  return maxRange;
return Range.between(min, max);

代码示例来源:origin: com.opencsv/opencsv

range = Range.between(min, max);
  range = Range.is(Integer.valueOf(r));
while(it.hasNext() && ! completelyContained) {
  final Range<Integer> next = it.next();
  if(next.containsRange(range)) {
    completelyContained = true;
    if(next.isOverlappedBy(range)) {
      range = Range.between(
          Math.min(next.getMinimum(), range.getMinimum()),
          Math.max(next.getMaximum(), range.getMaximum()));
      it.remove();
    else if(next.getMaximum()+1 == range.getMinimum()) {
      range = Range.between(next.getMinimum(), range.getMaximum());
    else if(range.getMaximum()+1 == next.getMinimum()) {
      range = Range.between(range.getMinimum(), next.getMaximum());

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <p>Checks whether this range contains all the elements of the specified range.</p>
 *
 * <p>This method may fail if the ranges have two different comparators or element types.</p>
 *
 * @param otherRange  the range to check, null returns false
 * @return true if this range contains the specified range
 * @throws RuntimeException if ranges cannot be compared
 */
public boolean containsRange(final Range<T> otherRange) {
  if (otherRange == null) {
    return false;
  }
  return contains(otherRange.minimum)
    && contains(otherRange.maximum);
}

代码示例来源:origin: com.opencsv/opencsv

/**
 * If there are ranges in the list of ranges encompassed by this mapping
 * that stretch beyond the maximum index given, they are shortened to be
 * no longer than the maximum index.
 * Ranges that lie completely beyond the maximum index are shortened to a
 * one-element range consisting of the range's lower boundary. No ranges are
 * under any circumstances removed, as this might compromise checks for
 * required fields.
 * 
 * @param maxIndex The new maximum for ranges
 */
public void attenuateRanges(int maxIndex) {
  ListIterator<Range<Integer>> rangeIterator = ranges.listIterator();
  while(rangeIterator.hasNext()) {
    Range<Integer> r = rangeIterator.next();
    if(r.getMaximum() > maxIndex) {
      if(r.getMinimum() > maxIndex) {
        rangeIterator.set(Range.is(r.getMinimum()));
      }
      else {
        rangeIterator.set(Range.between(r.getMinimum(), maxIndex));
      }
    }
  }
}

代码示例来源:origin: de.julielab/julielab-entity-evaluator

if (!offsetRange.isOverlappedBy(other.offsetRange))
  return false;
if (comparisonType == ComparisonType.EXACT) {
  if (!offsetRange.equals(other.offsetRange))
    return false;
  Range<Integer> intersection = offsetRange.intersectionWith(other.offsetRange);
  int intersectionLength = intersection.getMaximum() - intersection.getMinimum();
  if (overlapType == OverlapType.CHARS && intersectionLength < overlapSize)
    return false;
  if (overlapType == OverlapType.PERCENT) {
    int thisMentionLength = offsetRange.getMaximum() - offsetRange.getMinimum();
    int otherMentionLength = other.offsetRange.getMaximum() - other.offsetRange.getMinimum();
    int thisOverlapPercent = (int) Math.ceil((double) intersectionLength / thisMentionLength * 100d);
    int otherOverlapPercent = (int) Math.ceil((double) intersectionLength / otherMentionLength * 100d);

代码示例来源:origin: Wikia/selenium-tests

public void verifyNumberOfTop1kWikisInRange(Range expectedRange) {
 wait.forElementVisible(headerWhereIsMyExtensionPage);
 Log.log("verifyNumberOfTop1kWikisInRange", "Verification of top 1k wikis", true, driver);
 Pattern p = Pattern.compile("\\d+");
 Matcher m = p.matcher(headerWhereIsMyExtensionPage.getText());
 m.find();
 Assertion.assertTrue(
     expectedRange.contains(Integer.parseInt(m.group())),
     String.format("Number of Top 1k Wikis between %s and %s", expectedRange.getMinimum(), expectedRange.getMaximum())
 );
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testIsOverlappedBy() {
  // null handling
  assertFalse(intRange.isOverlappedBy(null));
  // easy inside range
  assertTrue(intRange.isOverlappedBy(Range.between(12, 18)));
  // outside range on each side
  assertFalse(intRange.isOverlappedBy(Range.between(32, 45)));
  assertFalse(intRange.isOverlappedBy(Range.between(2, 8)));
  // equals range
  assertTrue(intRange.isOverlappedBy(Range.between(10, 20)));
  // overlaps
  assertTrue(intRange.isOverlappedBy(Range.between(9, 14)));
  assertTrue(intRange.isOverlappedBy(Range.between(16, 21)));
  // touches lower boundary
  assertTrue(intRange.isOverlappedBy(Range.between(10, 19)));
  assertTrue(intRange.isOverlappedBy(Range.between(10, 21)));
  // touches upper boundary
  assertTrue(intRange.isOverlappedBy(Range.between(11, 20)));
  assertTrue(intRange.isOverlappedBy(Range.between(9, 20)));
  // negative
  assertFalse(intRange.isOverlappedBy(Range.between(-11, -18)));
}

代码示例来源:origin: de.julielab/julielab-entity-evaluator

public void setBegin(int begin) {
  Integer end = offsetRange != null ? offsetRange.getMaximum() : begin;
  if (end < begin)
    end = begin;
  offsetRange = Range.between(begin, end);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testComparableConstructors() {
  final Comparable c = new Comparable() {
    @Override
    public int compareTo(final Object other) {
      return 1;
    }
  };
  final Range r1 = Range.is(c);
  final Range r2 = Range.between(c, c);
  assertTrue(r1.isNaturalOrdering());
  assertTrue(r2.isNaturalOrdering());
}

代码示例来源:origin: de.julielab/julielab-entity-evaluator

public void setEnd(int end) {
  Integer begin = offsetRange != null ? offsetRange.getMinimum() : end;
  if (begin > end)
    begin = end;
  offsetRange = Range.between(begin, end);
}

代码示例来源:origin: apache/metron

private boolean containsInclusive(Range<Long> interval, long ts) {
 return interval.contains(ts) || interval.getMaximum() == ts;
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testIsAfterRange() {
  assertFalse(intRange.isAfterRange(null));
  assertTrue(intRange.isAfterRange(Range.between(5, 9)));
  assertFalse(intRange.isAfterRange(Range.between(5, 10)));
  assertFalse(intRange.isAfterRange(Range.between(5, 20)));
  assertFalse(intRange.isAfterRange(Range.between(5, 25)));
  assertFalse(intRange.isAfterRange(Range.between(15, 25)));
  assertFalse(intRange.isAfterRange(Range.between(21, 25)));
  assertFalse(intRange.isAfterRange(Range.between(10, 20)));
}

相关文章