Skip to content

Commit

Permalink
Fix bug in Interval.take() when count is 0.
Browse files Browse the repository at this point in the history
Signed-off-by: Nikhil Nanivadekar <nikhil.nanivadekar@gs.com>
  • Loading branch information
nikhilnanivadekar committed Apr 28, 2016
1 parent 5c413ed commit 17e1683
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTE_DRAFT.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Optimizations
Bug fixes
---------

* Fixed Interval.take(int count) when count is 0. Old behavior was to return an Interval of size 1. This is a behavior breaking change.
* Changed AbstractSynchronizedRichIterable.groupByUniqueKey() to return MapIterable instead of MutableMap.
* Changed AbstractMutableMapIterable.groupByUniqueKey() to return MutableMapIterable instead of MutableMap.
* Changed MutableMapIterable.aggregateBy() to return MutableMap instead of MutableMapIterable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ public Interval subList(int fromIndex, int toIndex)
}

@Override
public Interval take(int count)
public LazyIterable<Integer> take(int count)
{
if (count < 0)
{
Expand All @@ -1063,7 +1063,7 @@ public Interval take(int count)
{
return Interval.fromToBy(this.from, this.locationAfterN(count - 1), this.step);
}
return Interval.fromToBy(this.from, this.from, this.step);
return Lists.immutable.<Integer>empty().asLazy();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ public void set()
@Test
public void take()
{
Verify.assertIterableEmpty(Interval.fromTo(1, 3).take(0));
Assert.assertEquals(FastList.newListWith(1, 2), Interval.fromTo(1, 3).take(2));
Assert.assertEquals(FastList.newListWith(1, 2), Interval.fromTo(1, 2).take(3));

Expand All @@ -803,6 +804,7 @@ public void take()
public void drop()
{
Assert.assertEquals(FastList.newListWith(3, 4), Interval.fromTo(1, 4).drop(2));
Assert.assertEquals(FastList.newListWith(1, 2, 3, 4), Interval.fromTo(1, 4).drop(0));
Verify.assertIterableEmpty(Interval.fromTo(1, 2).drop(3));

Verify.assertThrows(IllegalArgumentException.class, () -> Interval.fromTo(1, 3).drop(-1));
Expand Down Expand Up @@ -845,7 +847,8 @@ public void tap()
MutableList<Integer> tapResult = Lists.mutable.of();
Interval interval = Interval.fromTo(10, -10).by(-5);
LazyIterable<Integer> lazyTapIterable = interval.tap(tapResult::add);
lazyTapIterable.each(x -> { }); //force evaluation
lazyTapIterable.each(x -> {
}); //force evaluation
Assert.assertEquals(interval, tapResult);
}
}

0 comments on commit 17e1683

Please sign in to comment.