Skip to content

Commit

Permalink
Change collect and collectWith with target collections on InternalArr…
Browse files Browse the repository at this point in the history
…ayIterate to use ensureCapacity for FastLists and ArrayLists.

Signed-off-by: Donald Raab <Donald.Raab@gs.com>
  • Loading branch information
Donald Raab authored and Donald Raab committed Nov 14, 2016
1 parent b40e3bb commit c6a814a
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,28 +307,22 @@ else if (source instanceof List && source instanceof RandomAccess)

private void addAllFastList(FastList<T> source)
{
int sourceSize = source.size();
int newSize = this.size + sourceSize;
this.ensureCapacity(newSize);
System.arraycopy(source.items, 0, this.items, this.size, sourceSize);
int newSize = this.ensureCapacityForAddAll(source);
System.arraycopy(source.items, 0, this.items, this.size, source.size());
this.size = newSize;
}

private void addAllArrayList(ArrayList<T> source)
{
int sourceSize = source.size();
int newSize = this.size + sourceSize;
this.ensureCapacity(newSize);
ArrayListIterate.toArray(source, this.items, this.size, sourceSize);
int newSize = this.ensureCapacityForAddAll(source);
ArrayListIterate.toArray(source, this.items, this.size, source.size());
this.size = newSize;
}

private void addAllRandomAccessList(List<T> source)
{
int sourceSize = source.size();
int newSize = this.size + sourceSize;
this.ensureCapacity(newSize);
RandomAccessListIterate.toArray(source, this.items, this.size, sourceSize);
int newSize = this.ensureCapacityForAddAll(source);
RandomAccessListIterate.toArray(source, this.items, this.size, source.size());
this.size = newSize;
}

Expand Down Expand Up @@ -425,6 +419,26 @@ public boolean trimToSizeIfGreaterThanPercent(double loadFactor)
return false;
}

private void ensureCapacityForAdd()
{
if (this.items == DEFAULT_SIZED_EMPTY_ARRAY)
{
this.items = (T[]) new Object[10];
}
else
{
this.transferItemsToNewArrayWithCapacity(this.sizePlusFiftyPercent(this.size));
}
}

private int ensureCapacityForAddAll(Collection<T> source)
{
int sourceSize = source.size();
int newSize = this.size + sourceSize;
this.ensureCapacity(newSize);
return newSize;
}

public void ensureCapacity(int minCapacity)
{
int oldCapacity = this.items.length;
Expand Down Expand Up @@ -996,18 +1010,6 @@ public boolean add(T newItem)
return true;
}

private void ensureCapacityForAdd()
{
if (this.items == DEFAULT_SIZED_EMPTY_ARRAY)
{
this.items = (T[]) new Object[10];
}
else
{
this.transferItemsToNewArrayWithCapacity(this.sizePlusFiftyPercent(this.size));
}
}

@Override
public void add(int index, T element)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.collections.impl.utility.internal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
Expand Down Expand Up @@ -450,6 +451,14 @@ public static <T, V, R extends Collection<V>> R collect(
Function<? super T, ? extends V> function,
R target)
{
if (target instanceof FastList)
{
((FastList) target).ensureCapacity(target.size() + size);
}
else if (target instanceof ArrayList)
{
((ArrayList) target).ensureCapacity(target.size() + size);
}
for (int i = 0; i < size; i++)
{
target.add(function.valueOf(array[i]));
Expand All @@ -475,13 +484,21 @@ public static <T, P, V, R extends Collection<V>> R collectWith(
int size,
Function2<? super T, ? super P, ? extends V> function,
P parameter,
R targetCollection)
R target)
{
if (target instanceof FastList)
{
((FastList) target).ensureCapacity(target.size() + size);
}
else if (target instanceof ArrayList)
{
((ArrayList) target).ensureCapacity(target.size() + size);
}
for (int i = 0; i < size; i++)
{
targetCollection.add(function.value(array[i], parameter));
target.add(function.value(array[i], parameter));
}
return targetCollection;
return target;
}

public static <T, V, R extends Collection<V>> R collectIf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@

package org.eclipse.collections.impl;

import java.net.PortUnreachableException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.DoubleSummaryStatistics;
import java.util.HashSet;
import java.util.IntSummaryStatistics;
import java.util.Iterator;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.collections.api.BooleanIterable;
Expand Down Expand Up @@ -99,6 +108,8 @@
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertFalse;

public abstract class AbstractRichIterableTestCase
{
protected abstract <T> RichIterable<T> newWith(T... littleElements);
Expand Down Expand Up @@ -271,6 +282,32 @@ public void collect()
Verify.assertContainsAll(this.newWith(1, 2, 3, 4).collect(String::valueOf, UnifiedSet.newSet()), "1", "2", "3", "4");
}

@Test
public void collectTarget()
{
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collect(each -> each + 1, Lists.mutable.empty()).toBag());
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
Bags.mutable.withAll(this.newWith(1, 2, 3).collect(each -> each + 1, new ArrayList<>())));
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
Bags.mutable.withAll(this.newWith(1, 2, 3).collect(each -> each + 1, new CopyOnWriteArrayList<>())));
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collect(each -> each + 1, Bags.mutable.empty()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collect(each -> each + 1, Sets.mutable.empty()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collect(each -> each + 1, new HashSet<>()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collect(each -> each + 1, new CopyOnWriteArraySet<>()));
}

@Test
public void collectBoolean()
{
Expand Down Expand Up @@ -718,12 +755,24 @@ public void collectWith_target()
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, Lists.mutable.empty()).toBag());
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
Bags.mutable.withAll(this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, new ArrayList<>())));
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
Bags.mutable.withAll(this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, new CopyOnWriteArrayList<>())));
Assert.assertEquals(
Bags.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, Bags.mutable.empty()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, Sets.mutable.empty()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, new HashSet<>()));
Assert.assertEquals(
Sets.mutable.of(2, 3, 4),
this.newWith(1, 2, 3).collectWith(AddFunction.INTEGER, 1, new CopyOnWriteArraySet<>()));
}

@Test
Expand Down Expand Up @@ -854,6 +903,15 @@ public void sumFloat()
Assert.assertEquals(expected, actual, 0.001);
}

@Test
public void summarizeFloat()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
DoubleSummaryStatistics expected = objects.summarizeFloat(Integer::floatValue);
Assert.assertEquals(6.0d, expected.getSum(), 0.0);
Assert.assertEquals(3, expected.getCount());
}

@Test
public void sumFloatConsistentRounding1()
{
Expand Down Expand Up @@ -889,6 +947,15 @@ public void sumDouble()
Assert.assertEquals(expected, actual, 0.001);
}

@Test
public void summarizeDouble()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
DoubleSummaryStatistics expected = objects.summarizeDouble(Integer::doubleValue);
Assert.assertEquals(6.0d, expected.getSum(), 0.0);
Assert.assertEquals(3, expected.getCount());
}

@Test
public void sumDoubleConsistentRounding1()
{
Expand Down Expand Up @@ -920,6 +987,15 @@ public void sumInteger()
Assert.assertEquals(expected, actual);
}

@Test
public void summarizeInt()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
IntSummaryStatistics expected = objects.summarizeInt(Integer::intValue);
Assert.assertEquals(6, expected.getSum());
Assert.assertEquals(3, expected.getCount());
}

@Test
public void sumLong()
{
Expand All @@ -929,6 +1005,15 @@ public void sumLong()
Assert.assertEquals(expected, actual);
}

@Test
public void summarizeLong()
{
RichIterable<Integer> objects = this.newWith(1, 2, 3);
LongSummaryStatistics expected = objects.summarizeLong(Integer::longValue);
Assert.assertEquals(6, expected.getSum());
Assert.assertEquals(3, expected.getCount());
}

@Test
public void sumByInt()
{
Expand Down Expand Up @@ -1463,4 +1548,31 @@ public void aggregateByNonMutating()
Assert.assertEquals(3, aggregation.get("3").intValue());
}
}

@Test
public void reduceOptional()
{
RichIterable<Integer> littleIterable = this.newWith(1, 2, 3);
Optional<Integer> result =
littleIterable.reduce(Integer::sum);
Assert.assertEquals(6, result.get().intValue());

RichIterable<Integer> bigIterable = this.newWith(Interval.oneTo(20).toArray());
Optional<Integer> bigResult =
bigIterable.reduce(Integer::max);
Assert.assertEquals(20, bigResult.get().intValue());

Optional<Integer> max =
littleIterable.reduce(Integer::max);
Assert.assertEquals(3, max.get().intValue());

Optional<Integer> min =
littleIterable.reduce(Integer::min);
Assert.assertEquals(1, min.get().intValue());

RichIterable<Integer> iterableEmpty = this.newWith();
Optional<Integer> resultEmpty =
iterableEmpty.reduce(Integer::sum);
assertFalse(resultEmpty.isPresent());
}
}

0 comments on commit c6a814a

Please sign in to comment.