2017-08-09 06:42:13 +02:00
|
|
|
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;
|
2017-08-09 23:06:04 +02:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import org.junit.Before;
|
2017-08-09 06:42:13 +02:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
public class SelectPatternTest {
|
|
|
|
|
|
2017-08-09 23:06:04 +02:00
|
|
|
MutableList<Integer> list;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void getList() {
|
|
|
|
|
this.list = new FastList<>();
|
2017-08-09 06:42:13 +02:00
|
|
|
list.add(1);
|
|
|
|
|
list.add(8);
|
|
|
|
|
list.add(5);
|
|
|
|
|
list.add(41);
|
|
|
|
|
list.add(31);
|
|
|
|
|
list.add(17);
|
|
|
|
|
list.add(23);
|
|
|
|
|
list.add(38);
|
2017-08-09 23:06:04 +02:00
|
|
|
}
|
2017-08-09 06:42:13 +02:00
|
|
|
|
2017-08-09 23:06:04 +02:00
|
|
|
@Test
|
|
|
|
|
public void givenListwhenSelect_thenCorrect() {
|
2017-08-09 06:42:13 +02:00
|
|
|
MutableList<Integer> greaterThanThirty = list.select(Predicates.greaterThan(30))
|
|
|
|
|
.sortThis();
|
|
|
|
|
|
2017-08-09 23:06:04 +02:00
|
|
|
assertEquals(31, (int) greaterThanThirty.getFirst());
|
|
|
|
|
assertEquals(38, (int) greaterThanThirty.get(1));
|
|
|
|
|
assertEquals(41, (int) greaterThanThirty.getLast());
|
2017-08-09 06:42:13 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-09 23:06:04 +02:00
|
|
|
@SuppressWarnings("rawtypes")
|
2017-08-09 06:42:13 +02:00
|
|
|
public MutableList selectUsingLambda() {
|
|
|
|
|
return list.select(each -> each > 30)
|
|
|
|
|
.sortThis();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 23:06:04 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2017-08-09 06:42:13 +02:00
|
|
|
@Test
|
2017-08-09 23:06:04 +02:00
|
|
|
public void givenListwhenSelectUsingLambda_thenCorrect() {
|
2017-08-09 06:42:13 +02:00
|
|
|
MutableList<Integer> greaterThanThirty = selectUsingLambda();
|
|
|
|
|
|
2017-08-09 23:06:04 +02:00
|
|
|
assertEquals(31, (int) greaterThanThirty.getFirst());
|
|
|
|
|
assertEquals(38, (int) greaterThanThirty.get(1));
|
|
|
|
|
assertEquals(41, (int) greaterThanThirty.getLast());
|
2017-08-09 06:42:13 +02:00
|
|
|
}
|
|
|
|
|
}
|