BigDecimalStream { public static Stream<BigDecimal> rangeClosed(BigDecimal startInclusive, BigDecimal endInclusive, BigDecimal step, MathContext mathContext) { if (step.signum() == 0) { throw new IllegalArgumentException("invalid step: 0"); } if (endInclusive.subtract(startInclusive).signum() == -step.signum()) { return Stream.empty(); } return StreamSupport.stream(new BigDecimalSpliterator(startInclusive, endInclusive, true, 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 testRangeClosedStepZero() { BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.ZERO, MathContext.DECIMAL64); }
@Test public void testRangeClosedStep3() { List<BigDecimal> list = BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(12), BigDecimal.valueOf(3), MathContext.DECIMAL64) .collect(Collectors.toList()); assertEquals(5, 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(true, list.contains(BigDecimal.valueOf(12))); assertEquals(0, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(-1), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(1, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(0), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(1, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(1), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(1, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(2), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(2, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(3), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(2, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(4), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(2, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(5), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(3, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(6), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(3, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(7), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(3, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(8), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(4, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(9), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(4, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(4, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(11), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); assertEquals(5, BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(12), BigDecimal.valueOf(3), MathContext.DECIMAL64).collect(Collectors.toList()).size()); }
@Test public void testRangeClosedLongStep3() { List<BigDecimal> list = BigDecimalStream.rangeClosed(0, 12, 3, MathContext.DECIMAL64) .collect(Collectors.toList()); assertEquals(5, 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(true, list.contains(BigDecimal.valueOf(12))); }
@Test public void testRangeClosedDoubleStep3() { List<BigDecimal> list = BigDecimalStream.rangeClosed(0.0, 12.0, 3.0, MathContext.DECIMAL64) .collect(Collectors.toList()); assertEquals(5, 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))); assertEquals(true, list.contains(BigDecimal.valueOf(12.0))); }
@Test public void testRangeClosedStep1() { List<BigDecimal> list = BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.ONE, MathContext.DECIMAL64) .collect(Collectors.toList()); assertListClosed(list, 0, 10); }
@Test public void testRangeClosedStepMinus1() { List<BigDecimal> list = BigDecimalStream.rangeClosed(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.ONE.negate(), MathContext.DECIMAL64) .collect(Collectors.toList()); assertList(list, 0, 0); }
@Test public void testRangeClosedDown() { List<BigDecimal> list = BigDecimalStream.rangeClosed(BigDecimal.valueOf(10), BigDecimal.valueOf(0), BigDecimal.ONE.negate(), MathContext.DECIMAL64) .collect(Collectors.toList()); assertListClosed(list, 0, 10); }