added code for apache commons collection SetUtils

This commit is contained in:
Seun Matt 2017-06-25 19:28:02 +01:00
parent 6e3d26062a
commit 5397d2c970
2 changed files with 128 additions and 1 deletions

View File

@ -85,6 +85,11 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons.collections.version}</version>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
@ -345,7 +350,12 @@
<artifactId>opennlp-tools</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<multiverse.version>0.7.0</multiverse.version>
@ -371,6 +381,8 @@
<serenity.plugin.version>1.4.0</serenity.plugin.version>
<jUnitParams.version>1.1.0</jUnitParams.version>
<netty.version>4.1.10.Final</netty.version>
<commons.collections.version>4.1</commons.collections.version>
<junit.version>4.12</junit.version>
</properties>
</project>

View File

@ -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<String> sourceSet = new HashSet<>();
sourceSet.add("London");
sourceSet.add("Lagos");
sourceSet.add("Error Source1");
Set<String> validatingSet
= SetUtils.predicatedSet(sourceSet, (s) -> s.startsWith("L"));
validatingSet.add("Error Source2");
}
@Test
public void givenTwoSets_whenDifference_thenSetView() {
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(5);
Set<Integer> b = new HashSet<>();
b.add(1);
b.add(2);
SetUtils.SetView<Integer> result = SetUtils.difference(a, b);
assertTrue(result.size() == 1 && result.contains(5));
}
@Test
public void givenTwoSets_whenUnion_thenUnionResult() {
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(5);
Set<Integer> b = new HashSet<>();
b.add(1);
b.add(2);
Set<Integer> expected = new HashSet<>();
expected.addAll(a);
expected.addAll(b);
SetUtils.SetView<Integer> union = SetUtils.union(a, b);
assertTrue(SetUtils.isEqualSet(expected, union));
}
@Test
public void givenTwoSets_whenIntersection_thenIntersectionResult() {
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(5);
Set<Integer> b = new HashSet<>();
b.add(1);
b.add(2);
Set<Integer> expected = new HashSet<>();
expected.add(1);
expected.add(2);
SetUtils.SetView<Integer> intersect = SetUtils.intersection(a, b);
assertTrue(SetUtils.isEqualSet(expected, intersect));
}
@Test
public void givenSet_whenTransformedSet_thenTransformedResult() {
Set<Integer> a = SetUtils.transformedSet(new HashSet<>(), (e) -> e * 2 );
a.add(2);
assertEquals(a.toArray()[0], 4);
Set<Integer> source = new HashSet<>();
source.add(1);
Set<Integer> newSet = TransformedSet.transformedSet(source, (e) -> e * 2);
assertEquals(newSet.toArray()[0], 2);
assertEquals(source.toArray()[0], 2);
}
@Test
public void givenTwoSet_whenDisjunction_thenDisjunctionSet() {
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(5);
Set<Integer> b = new HashSet<>();
b.add(1);
b.add(2);
b.add(3);
SetUtils.SetView<Integer> result = SetUtils.disjunction(a, b);
assertTrue(result.toSet().contains(5) && result.toSet().contains(3));
}
@Test
public void givenSet_when_OrderedSet_thenMaintainElementOrder() {
Set<Integer> set = new HashSet<>();
set.add(10);
set.add(1);
set.add(5);
System.out.println("unordered set: " + set);
Set<Integer> orderedSet = SetUtils.orderedSet(new HashSet<>());
orderedSet.add(10);
orderedSet.add(1);
orderedSet.add(5);
System.out.println("ordered set = " + orderedSet);
}
}