org.apache.commons.lang3.Range.between()方法的使用及代码示例

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

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

Range.between介绍

[英]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). The getMinimum and getMaximum methods will return the correct values.
[中]获取具有指定的最小值和最大值(包括两者)的范围。
范围使用元素的自然顺序来确定值在范围内的位置。
参数可以按(min,max)或(max,min)的顺序传递。getMinimum和getMaximum方法将返回正确的值。

代码示例

代码示例来源: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: 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 natural ordering of the elements 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, not null
 * @return the range object, not null
 * @throws IllegalArgumentException if the element is null
 * @throws ClassCastException if the element is not {@code Comparable}
 */
public static <T extends Comparable<T>> Range<T> is(final T element) {
  return between(element, element, null);
}

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

/**
 * <p>Obtains a range with the specified minimum and maximum values (both inclusive).</p>
 *
 * <p>The range uses the natural ordering of the elements to determine where
 * values lie in the range.</p>
 *
 * <p>The arguments may be passed in the order (min,max) or (max,min).
 * The getMinimum and getMaximum methods will return the correct values.</p>
 *
 * @param <T> the type of the elements in this range
 * @param fromInclusive  the first value that defines the edge of the range, inclusive
 * @param toInclusive  the second value that defines the edge of the range, inclusive
 * @return the range object, not null
 * @throws IllegalArgumentException if either element is null
 * @throws ClassCastException if the elements are not {@code Comparable}
 */
public static <T extends Comparable<T>> Range<T> between(final T fromInclusive, final T toInclusive) {
  return between(fromInclusive, toInclusive, null);
}

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

@SuppressWarnings("cast") // intRange
@Before
public void setUp() {
  byteRange = Range.between((byte) 0, (byte) 5);
  byteRange2 = Range.between((byte) 0, (byte) 5);
  byteRange3 = Range.between((byte) 0, (byte) 10);
  intRange = Range.between(10, 20);
  longRange = Range.between(10L, 20L);
  floatRange = Range.between((float) 10, (float) 20);
  doubleRange = Range.between((double) 10, (double) 20);
}

代码示例来源: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(expected = IllegalArgumentException.class)
public void testIntersectionWithNonOverlapping() {
  intRange.intersectionWith(Range.between(0, 9));
}

代码示例来源: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)));
}

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

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

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

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

代码示例来源: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: 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 testIntersectionWith() {
  assertSame(intRange, intRange.intersectionWith(intRange));
  assertSame(byteRange, byteRange.intersectionWith(byteRange));
  assertSame(longRange, longRange.intersectionWith(longRange));
  assertSame(floatRange, floatRange.intersectionWith(floatRange));
  assertSame(doubleRange, doubleRange.intersectionWith(doubleRange));
  assertEquals(Range.between(10, 15), intRange.intersectionWith(Range.between(5, 15)));
}

代码示例来源: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: org.apache.commons/commons-lang3

@Test
public void testToString() {
  assertNotNull(byteRange.toString());
  final String str = intRange.toString();
  assertEquals("[10..20]", str);
  assertEquals("[-20..-10]", Range.between(-20, -10).toString());
}

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

Range<Integer> rb = Range.between(-10, 20);
assertFalse("should not contain null", rb.contains(null));
assertTrue("should contain 10", rb.contains(10));
assertFalse("should not contain 21", rb.contains(21));
assertFalse("should not contain -11", rb.contains(-11));
rb = Range.between(-10, 20, c);
assertFalse("should not contain null", rb.contains(null));
assertTrue("should contain 10", rb.contains(10));
assertTrue("should contain 21", rb.contains(21));
assertTrue("should contain -11", rb.contains(-11));
Range<String> rbstr = Range.between("house", "i");
assertFalse("should not contain null", rbstr.contains(null));
assertTrue("should contain house", rbstr.contains("house"));
assertFalse("should not contain hose", rbstr.contains("hose"));
assertFalse("should not contain ice", rbstr.contains("ice"));
rbstr = Range.between("house", "i", lengthComp);
assertFalse("should not contain null", rbstr.contains(null));
assertTrue("should contain house", rbstr.contains("house"));

代码示例来源:origin: org.apache.hadoop/hadoop-hdfs

private Range<Long> txnRange(SegmentStateProto seg) {
 Preconditions.checkArgument(seg.hasEndTxId(),
   "invalid segment: %s ; journal id: %s", seg, journalId);
 return Range.between(seg.getStartTxId(), seg.getEndTxId());
}

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

Range<Long> interval = Range.between(left, left + binWidth);
boolean include = includes.iterator().hasNext()?false:true;
for(Predicate<Long> inclusionPredicate : includes) {

代码示例来源:origin: org.apache.hadoop/hadoop-yarn-common

@Override
public synchronized void setStartRange(long begin, long end)
  throws IllegalArgumentException {
 if (begin > end) {
  throw new IllegalArgumentException("begin > end in range (begin, " +
    "end): (" + begin + ", " + end + ")");
 }
 this.start = Range.between(begin, end);
}

代码示例来源:origin: Exslims/MercuryTrade

private boolean valueInSoundRange() {
  float value = getValue() / 1000f;
  float threshold = this.descriptor.getSoundThreshold().floatValue();
  return Range.between(threshold - 0.1, threshold + 0.1).contains((double) value);
}

代码示例来源:origin: org.apache.hadoop/hadoop-yarn-common

@Override
public synchronized Range<Long> getStartRange() {
 if (this.start == null) {
  GetApplicationsRequestProtoOrBuilder p = viaProto ? proto: builder;
  if (p.hasStartBegin() || p.hasStartEnd()) {
   long begin = p.hasStartBegin() ? p.getStartBegin() : 0L;
   long end = p.hasStartEnd() ? p.getStartEnd() : Long.MAX_VALUE;
   this.start = Range.between(begin, end);
  }
 }
 return this.start;
}

相关文章