BAEL-1035 Introduction to Eclipse Collections

This commit is contained in:
Ahmed Tawila 2017-08-09 23:06:04 +02:00
parent ac1e77f5d9
commit c8a6913956
6 changed files with 123 additions and 39 deletions

View File

@ -5,13 +5,16 @@ import static org.junit.Assert.assertTrue;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.junit.Before;
import org.junit.Test;
public class AllSatisfyPatternTest {
@Test
public void whenAnySatisfiesCondition_thenCorrect() {
MutableList<Integer> list = new FastList<>();
MutableList<Integer> list;
@Before
public void getList() {
this.list = new FastList<>();
list.add(1);
list.add(8);
list.add(5);
@ -20,7 +23,10 @@ public class AllSatisfyPatternTest {
list.add(17);
list.add(23);
list.add(38);
}
@Test
public void whenAnySatisfiesCondition_thenCorrect() {
boolean result = list.allSatisfy(Predicates.greaterThan(0));
assertTrue(result);

View File

@ -5,13 +5,16 @@ import static org.junit.Assert.assertTrue;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.junit.Before;
import org.junit.Test;
public class AnySatisfyPatternTest {
@Test
public void whenAnySatisfiesCondition_thenCorrect() {
MutableList<Integer> list = new FastList<>();
MutableList<Integer> list;
@Before
public void getList() {
this.list = new FastList<>();
list.add(1);
list.add(8);
list.add(5);
@ -20,7 +23,10 @@ public class AnySatisfyPatternTest {
list.add(17);
list.add(23);
list.add(38);
}
@Test
public void whenAnySatisfiesCondition_thenCorrect() {
boolean result = list.anySatisfy(Predicates.greaterThan(30));
assertTrue(result);

View File

@ -4,13 +4,17 @@ import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.list.mutable.FastList;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class DetectPatternTest {
@Test
public void whenDetect_thenCorrect() {
MutableList<Integer> list = new FastList<>();
MutableList<Integer> list;
@Before
public void getList() {
this.list = new FastList<>();
list.add(1);
list.add(8);
list.add(5);
@ -19,7 +23,10 @@ public class DetectPatternTest {
list.add(17);
list.add(23);
list.add(38);
}
@Test
public void whenDetect_thenCorrect() {
Integer result = list.detect(Predicates.greaterThan(30));
assertEquals((int) result, 41);

View File

@ -0,0 +1,61 @@
package com.baeldung.eclipsecollections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.partition.list.PartitionMutableList;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.junit.Before;
import org.junit.Test;
public class PartitionPatternTest {
MutableList<Integer> list;
@Before
public void getList() {
this.list = new FastList<>();
list.add(1);
list.add(8);
list.add(5);
list.add(41);
list.add(31);
list.add(17);
list.add(23);
list.add(38);
}
@SuppressWarnings({ "unused" })
@Test
public void whenAnySatisfiesCondition_thenCorrect() {
MutableList<Integer> numbers = list;
PartitionMutableList<Integer> partitionedFolks = numbers.partition(new Predicate<Integer>() {
/**
*
*/
private static final long serialVersionUID = -1551138743683678406L;
public boolean accept(Integer each) {
return each > 30;
}
});
MutableList<Integer> greaterThanThirty = partitionedFolks.getSelected()
.sortThis();
MutableList<Integer> smallerThanThirty = partitionedFolks.getRejected()
.sortThis();
assertEquals(1, (int) smallerThanThirty.getFirst());
assertTrue(smallerThanThirty.anySatisfy(Predicates.equal(5)));
assertTrue(smallerThanThirty.anySatisfy(Predicates.equal(8)));
assertTrue(smallerThanThirty.anySatisfy(Predicates.equal(17)));
assertTrue(smallerThanThirty.anySatisfy(Predicates.equal(23)));
assertTrue(greaterThanThirty.anySatisfy(Predicates.equal(31)));
assertTrue(greaterThanThirty.anySatisfy(Predicates.equal(38)));
assertTrue(greaterThanThirty.anySatisfy(Predicates.equal(41)));
}
}

View File

@ -3,14 +3,17 @@ package com.baeldung.eclipsecollections;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class RejectPatternTest {
@Test
public void whenReject_thenCorrect() {
MutableList<Integer> list = new FastList<>();
MutableList<Integer> list;
@Before
public void getList() {
this.list = new FastList<>();
list.add(1);
list.add(8);
list.add(5);
@ -19,14 +22,17 @@ public class RejectPatternTest {
list.add(17);
list.add(23);
list.add(38);
}
@Test
public void whenReject_thenCorrect() {
MutableList<Integer> notGreaterThanThirty = list.reject(Predicates.greaterThan(30))
.sortThis();
Assert.assertEquals(1, (int) notGreaterThanThirty.getFirst());
Assert.assertEquals(5, (int) notGreaterThanThirty.get(1));
Assert.assertEquals(8, (int) notGreaterThanThirty.get(2));
Assert.assertEquals(17, (int) notGreaterThanThirty.get(3));
Assert.assertEquals(23, (int) notGreaterThanThirty.getLast());
assertEquals(1, (int) notGreaterThanThirty.getFirst());
assertEquals(5, (int) notGreaterThanThirty.get(1));
assertEquals(8, (int) notGreaterThanThirty.get(2));
assertEquals(17, (int) notGreaterThanThirty.get(3));
assertEquals(23, (int) notGreaterThanThirty.getLast());
}
}

View File

@ -3,14 +3,17 @@ package com.baeldung.eclipsecollections;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class SelectPatternTest {
@Test
public void whenSelect_thenCorrect() {
MutableList<Integer> list = new FastList<>();
MutableList<Integer> list;
@Before
public void getList() {
this.list = new FastList<>();
list.add(1);
list.add(8);
list.add(5);
@ -19,36 +22,31 @@ public class SelectPatternTest {
list.add(17);
list.add(23);
list.add(38);
}
@Test
public void givenListwhenSelect_thenCorrect() {
MutableList<Integer> greaterThanThirty = list.select(Predicates.greaterThan(30))
.sortThis();
Assert.assertEquals(31, (int) greaterThanThirty.getFirst());
Assert.assertEquals(38, (int) greaterThanThirty.get(1));
Assert.assertEquals(41, (int) greaterThanThirty.getLast());
assertEquals(31, (int) greaterThanThirty.getFirst());
assertEquals(38, (int) greaterThanThirty.get(1));
assertEquals(41, (int) greaterThanThirty.getLast());
}
@SuppressWarnings("rawtypes")
public MutableList selectUsingLambda() {
MutableList<Integer> list = new FastList<>();
list.add(1);
list.add(8);
list.add(5);
list.add(41);
list.add(31);
list.add(17);
list.add(23);
list.add(38);
return list.select(each -> each > 30)
.sortThis();
}
@SuppressWarnings("unchecked")
@Test
public void whenSelectUsingLambda_thenCorrect() {
public void givenListwhenSelectUsingLambda_thenCorrect() {
MutableList<Integer> greaterThanThirty = selectUsingLambda();
Assert.assertEquals(31, (int) greaterThanThirty.getFirst());
Assert.assertEquals(38, (int) greaterThanThirty.get(1));
Assert.assertEquals(41, (int) greaterThanThirty.getLast());
assertEquals(31, (int) greaterThanThirty.getFirst());
assertEquals(38, (int) greaterThanThirty.get(1));
assertEquals(41, (int) greaterThanThirty.getLast());
}
}