BAEL-1035 Introduction to Eclipse Collections (#2425)

* Evaluation article: Different Types of Bean Injection in Spring

* added tests & changed configuration to Java-based config

* removed xml config files

* rename unit tests

* BAEL-972 - Apache Commons Text

* remove code from evaluation article

* remove code from evaluation article

* BAEL-972 - Apache Commons Text - added another example

* BAEL-972 - Apache Commons Text - just indentation

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java - fix problems

* BAEL-1033 Introduction to StreamUtils

* BAEL-1033 Introduction to StreamUtils

* BAEL-1033 Introduction to StreamUtils

* fix formatting

* BAEL-1033 minor refactor

* BAEL-1035 Introduction to Eclipse Collections

* format

* BAEL-1035 Introduction to Eclipse Collections

* BAEL-1035 Introduction to Eclipse Collections

* BAEL-1035 Introduction to Eclipse Collections

* cleanup

* cleanup
This commit is contained in:
Ahmed Tawila 2017-08-12 18:42:04 +02:00 committed by Grzegorz Piwowarek
parent 310e89e4f0
commit 9b2e8ec9f9
13 changed files with 84 additions and 118 deletions

View File

@ -14,15 +14,7 @@ public class AllSatisfyPatternTest {
@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);
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
}
@Test

View File

@ -14,15 +14,7 @@ public class AnySatisfyPatternTest {
@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);
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
}
@Test

View File

@ -2,7 +2,8 @@ package com.baeldung.eclipsecollections;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.list.mutable.FastList;
import static org.junit.Assert.assertEquals;
import org.assertj.core.api.Assertions;
import org.junit.Test;
public class CollectPatternTest {
@ -16,7 +17,7 @@ public class CollectPatternTest {
MutableList<String> lastNames = students.collect(Student::getLastName);
assertEquals("Hopkins", lastNames.get(0));
assertEquals("Adams", lastNames.get(1));
Assertions.assertThat(lastNames)
.containsExactly("Hopkins", "Adams");
}
}

View File

@ -1,9 +1,8 @@
package com.baeldung.eclipsecollections;
import static org.junit.Assert.assertTrue;
import org.assertj.core.api.Assertions;
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.Test;
public class ConvertContainerToAnotherTest {
@ -12,9 +11,8 @@ public class ConvertContainerToAnotherTest {
@Test
public void whenConvertContainerToAnother_thenCorrect() {
MutableList<String> cars = (MutableList) ConvertContainerToAnother.convertToList();
assertTrue(cars.anySatisfy(Predicates.equal("Toyota")));
assertTrue(cars.anySatisfy(Predicates.equal("Mercedes")));
assertTrue(cars.anySatisfy(Predicates.equal("Volkswagen")));
Assertions.assertThat(cars)
.containsExactlyElementsOf(FastList.newListWith("Volkswagen", "Toyota", "Mercedes"));
}
}

View File

@ -1,10 +1,9 @@
package com.baeldung.eclipsecollections;
import org.assertj.core.api.Assertions;
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;
@ -14,21 +13,14 @@ public class DetectPatternTest {
@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);
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
}
@Test
public void whenDetect_thenCorrect() {
Integer result = list.detect(Predicates.greaterThan(30));
assertEquals((int) result, 41);
Assertions.assertThat(result)
.isEqualTo(41);
}
}

View File

@ -1,8 +1,12 @@
package com.baeldung.eclipsecollections;
import org.assertj.core.api.Assertions;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.list.mutable.FastList;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
@ -13,6 +17,7 @@ public class FlatCollectTest {
MutableList<String> addresses3;
MutableList<String> addresses4;
List<String> expectedAddresses;
MutableList<Student> students;
@Before
@ -28,15 +33,18 @@ public class FlatCollectTest {
Student student2 = new Student("George", "Adams", addresses2);
this.addresses2 = FastList.newListWith(address3, address4);
this.students = FastList.newListWith(student1, student2);
this.expectedAddresses = new ArrayList<>();
this.expectedAddresses.add("73 Pacific St., Forest Hills, NY 11375");
this.expectedAddresses.add("93 Bayport Ave., South Richmond Hill, NY 11419");
this.expectedAddresses.add("548 Market St, San Francisco, CA 94104");
this.expectedAddresses.add("8605 Santa Monica Blvd, West Hollywood, CA 90069");
}
@Test
public void whenFlatCollect_thenCorrect() {
MutableList<String> addresses = students.flatCollect(Student::getAddresses);
assertEquals("73 Pacific St., Forest Hills, NY 11375", addresses.get(0));
assertEquals("93 Bayport Ave., South Richmond Hill, NY 11419", addresses.get(1));
assertEquals("548 Market St, San Francisco, CA 94104", addresses.get(2));
assertEquals("8605 Santa Monica Blvd, West Hollywood, CA 90069", addresses.get(3));
Assertions.assertThat(addresses)
.containsExactlyElementsOf(this.expectedAddresses);
}
}

View File

@ -1,13 +1,10 @@
package com.baeldung.eclipsecollections;
import org.eclipse.collections.api.block.procedure.Procedure;
import static org.junit.Assert.assertEquals;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.tuple.Tuples;
import static org.junit.Assert.assertEquals;
import java.util.Map;
import org.junit.Test;
public class ForEachPatternTest {

View File

@ -1,10 +1,9 @@
package com.baeldung.eclipsecollections;
import org.assertj.core.api.Assertions;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.factory.Lists;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class LazyIterationTest {
@ -19,8 +18,7 @@ public class LazyIterationTest {
LazyIterable<Student> lazyStudents = students.asLazy();
LazyIterable<String> lastNames = lazyStudents.collect(Student::getLastName);
assertTrue(lastNames.anySatisfy(Predicates.equal("Hopkins")));
assertTrue(lastNames.anySatisfy(Predicates.equal("Adams")));
assertTrue(lastNames.anySatisfy(Predicates.equal("Rodriguez")));
Assertions.assertThat(lastNames)
.containsAll(Lists.mutable.with("Hopkins", "Adams", "Rodriguez"));
}
}

View File

@ -1,12 +1,9 @@
package com.baeldung.eclipsecollections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.assertj.core.api.Assertions;
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;
@ -17,15 +14,7 @@ public class PartitionPatternTest {
@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);
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
}
@Test
@ -47,14 +36,9 @@ public class PartitionPatternTest {
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)));
Assertions.assertThat(smallerThanThirty)
.containsExactly(1, 5, 8, 17, 23);
Assertions.assertThat(greaterThanThirty)
.containsExactly(31, 38, 41);
}
}

View File

@ -1,27 +1,21 @@
package com.baeldung.eclipsecollections;
import org.assertj.core.api.Assertions;
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 RejectPatternTest {
MutableList<Integer> list;
MutableList<Integer> expectedList;
@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);
public void setup() {
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
this.expectedList = FastList.newListWith(1, 5, 8, 17, 23);
}
@Test
@ -29,10 +23,7 @@ public class RejectPatternTest {
MutableList<Integer> notGreaterThanThirty = list.reject(Predicates.greaterThan(30))
.sortThis();
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());
Assertions.assertThat(notGreaterThanThirty)
.containsExactlyElementsOf(this.expectedList);
}
}

View File

@ -1,9 +1,9 @@
package com.baeldung.eclipsecollections;
import org.assertj.core.api.Assertions;
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;
@ -13,15 +13,7 @@ public class SelectPatternTest {
@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);
this.list = FastList.newListWith(1, 8, 5, 41, 31, 17, 23, 38);
}
@Test
@ -29,9 +21,8 @@ public class SelectPatternTest {
MutableList<Integer> greaterThanThirty = list.select(Predicates.greaterThan(30))
.sortThis();
assertEquals(31, (int) greaterThanThirty.getFirst());
assertEquals(38, (int) greaterThanThirty.get(1));
assertEquals(41, (int) greaterThanThirty.getLast());
Assertions.assertThat(greaterThanThirty)
.containsExactly(31, 38, 41);
}
@SuppressWarnings("rawtypes")
@ -45,8 +36,7 @@ public class SelectPatternTest {
public void givenListwhenSelectUsingLambda_thenCorrect() {
MutableList<Integer> greaterThanThirty = selectUsingLambda();
assertEquals(31, (int) greaterThanThirty.getFirst());
assertEquals(38, (int) greaterThanThirty.get(1));
assertEquals(41, (int) greaterThanThirty.getLast());
Assertions.assertThat(greaterThanThirty)
.containsExactly(31, 38, 41);
}
}

View File

@ -4,19 +4,31 @@ import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.tuple.Tuples;
import static org.junit.Assert.assertEquals;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
public class ZipTest {
MutableList<Pair<String, String>> expectedPairs;
@SuppressWarnings("unchecked")
@Before
public void setup() {
Pair<String, String> pair1 = Tuples.pair("1", "Porsche");
Pair<String, String> pair2 = Tuples.pair("2", "Volvo");
Pair<String, String> pair3 = Tuples.pair("3", "Toyota");
expectedPairs = Lists.mutable.of(pair1, pair2, pair3);
}
@Test
public void whenZip_thenCorrect() {
MutableList<String> numbers = Lists.mutable.with("1", "2", "3", "Ignored");
MutableList<String> cars = Lists.mutable.with("Porsche", "Volvo", "Toyota");
MutableList<Pair<String, String>> pairs = numbers.zip(cars);
assertEquals(Tuples.pair("1", "Porsche"), pairs.get(0));
assertEquals(Tuples.pair("2", "Volvo"), pairs.get(1));
assertEquals(Tuples.pair("3", "Toyota"), pairs.get(2));
Assertions.assertThat(pairs)
.containsExactlyElementsOf(this.expectedPairs);
}
}

View File

@ -1,22 +1,33 @@
package com.baeldung.eclipsecollections;
import static org.junit.Assert.assertEquals;
import org.assertj.core.api.Assertions;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.tuple.Tuples;
import org.junit.Before;
import org.junit.Test;
public class ZipWithIndexTest {
MutableList<Pair<String, Integer>> expectedPairs;
@SuppressWarnings("unchecked")
@Before
public void setup() {
Pair<String, Integer> pair1 = Tuples.pair("Porsche", 0);
Pair<String, Integer> pair2 = Tuples.pair("Volvo", 1);
Pair<String, Integer> pair3 = Tuples.pair("Toyota", 2);
expectedPairs = Lists.mutable.of(pair1, pair2, pair3);
}
@Test
public void whenZip_thenCorrect() {
MutableList<String> cars = FastList.newListWith("Porsche", "Volvo", "Toyota");
MutableList<Pair<String, Integer>> pairs = cars.zipWithIndex();
assertEquals(Tuples.pair("Porsche", 0), pairs.get(0));
assertEquals(Tuples.pair("Volvo", 1), pairs.get(1));
assertEquals(Tuples.pair("Toyota", 2), pairs.get(2));
Assertions.assertThat(pairs)
.containsExactlyElementsOf(this.expectedPairs);
}
}