Skip to content

Commit

Permalink
Optimize and increase test coverage for CaseFunction and CaseProcedure.
Browse files Browse the repository at this point in the history
Signed-off-by: Donald Raab <Donald.Raab@gs.com>
  • Loading branch information
Donald Raab authored and Donald Raab committed Jan 7, 2017
1 parent cadfb06 commit 0752bd8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ public CaseFunction<T, V> setDefault(Function<? super T, ? extends V> function)
@Override
public V valueOf(T argument)
{
for (Pair<Predicate<? super T>, Function<? super T, ? extends V>> pair : this.predicateFunctions)
int localSize = this.predicateFunctions.size();
for (int i = 0; i < localSize; i++)
{
Pair<Predicate<? super T>, Function<? super T, ? extends V>> pair = this.predicateFunctions.get(i);
if (pair.getOne().accept(argument))
{
return pair.getTwo().valueOf(argument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public CaseProcedure<T> setDefault(Procedure<? super T> procedure)
@Override
public void value(T argument)
{
for (Pair<Predicate<? super T>, Procedure<? super T>> pair : this.predicateProcedures)
int localSize = this.predicateProcedures.size();
for (int i = 0; i < localSize; i++)
{
Pair<Predicate<? super T>, Procedure<? super T>> pair = this.predicateProcedures.get(i);
if (pair.getOne().accept(argument))
{
pair.getTwo().value(argument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,79 @@

package org.eclipse.collections.impl.block.procedure;

import java.util.List;

import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.utility.Iterate;
import org.junit.Assert;
import org.junit.Test;

public class CaseProcedureTest
{
@Test
public void procedure()
public void noopCaseAndThenDefault()
{
List<String> result = Lists.mutable.empty();
FastList<String> strings = FastList.newListWith("1", "2");
CaseProcedure<String> procedure = new CaseProcedure<>();
strings.each(procedure);
Verify.assertEmpty(result);
procedure.setDefault(result::add);
strings.each(procedure);
Assert.assertEquals(result, strings);
Verify.assertContains("CaseProcedure", procedure.toString());
}

@Test
public void oneCaseWithDefault()
{
MutableList<String> ifOneList = Lists.mutable.of();
MutableList<String> defaultList = Lists.mutable.of();
MutableList<String> list = FastList.newListWith("1", "2");
Iterate.forEach(list, new CaseProcedure<String>(defaultList::add).addCase("1"::equals, ifOneList::add));
CaseProcedure<String> procedure =
new CaseProcedure<String>(defaultList::add)
.addCase("1"::equals, ifOneList::add);
list.each(procedure);
Assert.assertEquals(FastList.newListWith("1"), ifOneList);
Assert.assertEquals(FastList.newListWith("2"), defaultList);
Verify.assertContains("CaseProcedure", procedure.toString());
}

@Test
public void twoCasesNoDefault()
{
MutableList<String> ifOneList = Lists.mutable.of();
MutableList<String> ifTwoList = Lists.mutable.of();
MutableList<String> list = FastList.newListWith("1", "2", "3");
CaseProcedure<String> procedure =
new CaseProcedure<String>()
.addCase("1"::equals, ifOneList::add)
.addCase("2"::equals, ifTwoList::add);
list.each(procedure);
Assert.assertEquals(FastList.newListWith("1"), ifOneList);
Assert.assertEquals(FastList.newListWith("2"), ifTwoList);
Verify.assertContains("CaseProcedure", procedure.toString());
}

@Test
public void twoCasesWithDefault()
{
MutableList<String> ifOneList = Lists.mutable.of();
MutableList<String> ifTwoList = Lists.mutable.of();
MutableList<String> defaultList = Lists.mutable.of();
MutableList<String> list = FastList.newListWith("1", "2", "3", "4");
CaseProcedure<String> procedure =
new CaseProcedure<String>(defaultList::add)
.addCase("1"::equals, ifOneList::add)
.addCase("2"::equals, ifTwoList::add);
list.each(procedure);
Assert.assertEquals(FastList.newListWith("1"), ifOneList);
Assert.assertEquals(FastList.newListWith("2"), ifTwoList);
Assert.assertEquals(FastList.newListWith("3", "4"), defaultList);
Verify.assertContains("CaseProcedure", procedure.toString());
}
}

0 comments on commit 0752bd8

Please sign in to comment.