BigDecimalStream { public static Stream<BigDecimal> range(BigDecimal startInclusive, BigDecimal endExclusive, BigDecimal step, MathContext mathContext) { if (step.signum() == 0) { throw new IllegalArgumentException("invalid step: 0"); } if (endExclusive.subtract(startInclusive).signum() != step.signum()) { return Stream.empty(); } return StreamSupport.stream(new BigDecimalSpliterator(startInclusive, endExclusive, false, step, mathContext), false); }  static Stream<BigDecimal> range(BigDecimal startInclusive, BigDecimal endExclusive, BigDecimal step, MathContext mathContext); static Stream<BigDecimal> range(long startInclusive, long endExclusive, long step, MathContext mathContext); static Stream<BigDecimal> range(double startInclusive, double endExclusive, double step, MathContext mathContext); static Stream<BigDecimal> rangeClosed(BigDecimal startInclusive, BigDecimal endInclusive, BigDecimal step, MathContext mathContext); static Stream<BigDecimal> rangeClosed(long startInclusive, long endInclusive, long step, MathContext mathContext); static Stream<BigDecimal> rangeClosed(double startInclusive, double endInclusive, double step, MathContext mathContext);  }
@Test(expected = IllegalArgumentException.class) public void testRangeStepZero() { BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.ZERO, MathContext.DECIMAL64); }
@Test public void testRangeStep1() { List<BigDecimal> list = BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.ONE, MathContext.DECIMAL64) .collect(Collectors.toList()); assertList(list, 0, 10); }
@Test public void testRangeStep3() { List<BigDecimal> list = BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.valueOf(3), MathContext.DECIMAL64) .collect(Collectors.toList()); assertEquals(4, list.size()); assertEquals(true, list.contains(BigDecimal.valueOf(0))); assertEquals(true, list.contains(BigDecimal.valueOf(3))); assertEquals(true, list.contains(BigDecimal.valueOf(6))); assertEquals(true, list.contains(BigDecimal.valueOf(9))); assertEquals(0, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(-1), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(0, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(0), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(1, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(1), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(1, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(2), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(1, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(3), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(2, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(4), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(2, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(5), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(2, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(6), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(3, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(7), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(3, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(8), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(3, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(9), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(4, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(4, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(11), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(4, BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(12), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); }
@Test public void testRangeLongStep3() { List<BigDecimal> list = BigDecimalStream.range(0, 10, 3, MathContext.DECIMAL64) .collect(Collectors.toList()); assertEquals(4, list.size()); assertEquals(true, list.contains(BigDecimal.valueOf(0))); assertEquals(true, list.contains(BigDecimal.valueOf(3))); assertEquals(true, list.contains(BigDecimal.valueOf(6))); assertEquals(true, list.contains(BigDecimal.valueOf(9))); }
@Test public void testRangeDoubleStep3() { List<BigDecimal> list = BigDecimalStream.range(0.0, 10.0, 3.0, MathContext.DECIMAL64) .collect(Collectors.toList()); assertEquals(4, list.size()); assertEquals(true, list.contains(BigDecimal.valueOf(0.0))); assertEquals(true, list.contains(BigDecimal.valueOf(3.0))); assertEquals(true, list.contains(BigDecimal.valueOf(6.0))); assertEquals(true, list.contains(BigDecimal.valueOf(9.0))); }
@Test public void testRangeStepMinus1() { List<BigDecimal> list = BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.ONE.negate(), MathContext.DECIMAL64) .collect(Collectors.toList()); assertList(list, 0, 0); }
@Test public void testRangeParallel() { List<BigDecimal> list = BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.ONE, MathContext.DECIMAL64) .parallel() .collect(Collectors.toList()); assertList(list, 0, 10); }
@Test public void testRangeDown() { List<BigDecimal> list = BigDecimalStream.range(BigDecimal.valueOf(9), BigDecimal.valueOf(-1), BigDecimal.ONE.negate(), MathContext.DECIMAL64) .collect(Collectors.toList()); assertList(list, 0, 10); }
@Test public void testSingleStep() { Stream<BigDecimal> stream = BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(3), BigDecimal.ONE, MathContext.DECIMAL64); Spliterator<BigDecimal> spliterator = stream.spliterator(); assertEquals(true, spliterator.tryAdvance(value -> assertEquals(BigDecimal.valueOf(0), value))); assertEquals(true, spliterator.tryAdvance(value -> assertEquals(BigDecimal.valueOf(1), value))); assertEquals(true, spliterator.tryAdvance(value -> assertEquals(BigDecimal.valueOf(2), value))); assertEquals(false, spliterator.tryAdvance(value -> fail("Should not be called"))); }