From 5397d2c97015013bcc29307e88ba6869f953be99 Mon Sep 17 00:00:00 2001 From: Seun Matt Date: Sun, 25 Jun 2017 19:28:02 +0100 Subject: [PATCH] added code for apache commons collection SetUtils --- libraries/pom.xml | 14 ++- .../commons/collections/SetUtilsUnitTest.java | 115 ++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index da040c9731..45b371061e 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -85,6 +85,11 @@ commons-lang3 ${commons-lang.version} + + org.apache.commons + commons-collections4 + ${commons.collections.version} + org.jasypt jasypt @@ -345,7 +350,12 @@ opennlp-tools 1.8.0 - + + junit + junit + ${junit.version} + test + 0.7.0 @@ -371,6 +381,8 @@ 1.4.0 1.1.0 4.1.10.Final + 4.1 + 4.12 \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java new file mode 100644 index 0000000000..3c2949a9b1 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java @@ -0,0 +1,115 @@ +package com.baeldung.commons.collections; + +import org.apache.commons.collections4.SetUtils; +import org.apache.commons.collections4.set.TransformedSet; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by smatt on 21/06/2017. + */ +public class SetUtilsUnitTest { + + @Test(expected = IllegalArgumentException.class) + public void givenSetAndPredicate_whenPredicatedSet_thenValidateSet_and_throw_IllegalArgumentException() { + Set sourceSet = new HashSet<>(); + sourceSet.add("London"); + sourceSet.add("Lagos"); + sourceSet.add("Error Source1"); + Set validatingSet + = SetUtils.predicatedSet(sourceSet, (s) -> s.startsWith("L")); + validatingSet.add("Error Source2"); + } + + @Test + public void givenTwoSets_whenDifference_thenSetView() { + Set a = new HashSet<>(); + a.add(1); + a.add(2); + a.add(5); + Set b = new HashSet<>(); + b.add(1); + b.add(2); + SetUtils.SetView result = SetUtils.difference(a, b); + assertTrue(result.size() == 1 && result.contains(5)); + } + + @Test + public void givenTwoSets_whenUnion_thenUnionResult() { + Set a = new HashSet<>(); + a.add(1); + a.add(2); + a.add(5); + Set b = new HashSet<>(); + b.add(1); + b.add(2); + Set expected = new HashSet<>(); + expected.addAll(a); + expected.addAll(b); + SetUtils.SetView union = SetUtils.union(a, b); + assertTrue(SetUtils.isEqualSet(expected, union)); + } + + @Test + public void givenTwoSets_whenIntersection_thenIntersectionResult() { + Set a = new HashSet<>(); + a.add(1); + a.add(2); + a.add(5); + Set b = new HashSet<>(); + b.add(1); + b.add(2); + Set expected = new HashSet<>(); + expected.add(1); + expected.add(2); + SetUtils.SetView intersect = SetUtils.intersection(a, b); + assertTrue(SetUtils.isEqualSet(expected, intersect)); + } + + @Test + public void givenSet_whenTransformedSet_thenTransformedResult() { + Set a = SetUtils.transformedSet(new HashSet<>(), (e) -> e * 2 ); + a.add(2); + assertEquals(a.toArray()[0], 4); + + Set source = new HashSet<>(); + source.add(1); + Set newSet = TransformedSet.transformedSet(source, (e) -> e * 2); + assertEquals(newSet.toArray()[0], 2); + assertEquals(source.toArray()[0], 2); + } + + @Test + public void givenTwoSet_whenDisjunction_thenDisjunctionSet() { + Set a = new HashSet<>(); + a.add(1); + a.add(2); + a.add(5); + Set b = new HashSet<>(); + b.add(1); + b.add(2); + b.add(3); + SetUtils.SetView result = SetUtils.disjunction(a, b); + assertTrue(result.toSet().contains(5) && result.toSet().contains(3)); + } + + @Test + public void givenSet_when_OrderedSet_thenMaintainElementOrder() { + Set set = new HashSet<>(); + set.add(10); + set.add(1); + set.add(5); + System.out.println("unordered set: " + set); + + Set orderedSet = SetUtils.orderedSet(new HashSet<>()); + orderedSet.add(10); + orderedSet.add(1); + orderedSet.add(5); + System.out.println("ordered set = " + orderedSet); + } +} \ No newline at end of file