Skip to content

Commit 3ae9699

Browse files
SeunMattzhendrikse
authored andcommitted
example code for BAEL-966 (eugenp#2152)
* added updated example codes * updated example code StringToCharStream * deleted StringToCharStream.java locally * removed redundant file * added code for apache commons collection SetUtils * refactored example code
1 parent 75e0473 commit 3ae9699

3 files changed

Lines changed: 96 additions & 4 deletions

File tree

‎core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import static org.junit.Assert.assertEquals;
1313
import static org.junit.Assert.assertNotNull;
1414
import static org.junit.Assert.assertThat;
15-
import static org.junit.Assert.assertTrue;
1615

1716
public class StringToCharStreamUnitTest {
1817

@@ -44,7 +43,6 @@ public void givenIntStream_whenMapToObj_thenReturnCharacterStream() {
4443
.mapToObj(c -> (char) c);
4544
Stream<Character> characterStream1 = testString.codePoints()
4645
.mapToObj(c -> (char) c);
47-
4846
assertNotNull("IntStream returned by chars() did not map to Stream<Character>", characterStream);
4947
assertNotNull("IntStream returned by codePoints() did not map to Stream<Character>", characterStream1);
5048
}

‎libraries/pom.xml‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@
9090
<artifactId>commons-lang3</artifactId>
9191
<version>${commons-lang.version}</version>
9292
</dependency>
93+
<dependency>
94+
<groupId>org.apache.commons</groupId>
95+
<artifactId>commons-collections4</artifactId>
96+
<version>${commons.collections.version}</version>
97+
</dependency>
9398
<dependency>
9499
<groupId>org.jasypt</groupId>
95100
<artifactId>jasypt</artifactId>
@@ -340,13 +345,17 @@
340345
<artifactId>opennlp-tools</artifactId>
341346
<version>1.8.0</version>
342347
</dependency>
348+
<dependency>
349+
<groupId>junit</groupId>
350+
<artifactId>junit</artifactId>
351+
<version>${junit.version}</version>
352+
<scope>test</scope>
353+
</dependency>
343354
<dependency>
344355
<groupId>info.debatty</groupId>
345356
<artifactId>java-lsh</artifactId>
346357
<version>${java-lsh.version}</version>
347358
</dependency>
348-
349-
350359
</dependencies>
351360
<properties>
352361
<multiverse.version>0.7.0</multiverse.version>
@@ -373,6 +382,8 @@
373382
<serenity.plugin.version>1.4.0</serenity.plugin.version>
374383
<jUnitParams.version>1.1.0</jUnitParams.version>
375384
<netty.version>4.1.10.Final</netty.version>
385+
<commons.collections.version>4.1</commons.collections.version>
386+
<junit.version>4.12</junit.version>
376387
<java-lsh.version>0.10</java-lsh.version>
377388
</properties>
378389

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.commons.collections;
2+
3+
import org.apache.commons.collections4.SetUtils;
4+
import org.apache.commons.collections4.set.TransformedSet;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertTrue;
13+
14+
/**
15+
* Created by smatt on 21/06/2017.
16+
*/
17+
public class SetUtilsUnitTest {
18+
19+
@Test(expected = IllegalArgumentException.class)
20+
public void givenSetAndPredicate_whenPredicatedSet_thenValidateSet_and_throw_IllegalArgumentException() {
21+
Set<String> sourceSet = new HashSet<>();
22+
sourceSet.addAll(Arrays.asList("London", "Lagos", "Err Source1"));
23+
Set<String> validatingSet
24+
= SetUtils.predicatedSet(sourceSet, (s) -> s.startsWith("L"));
25+
validatingSet.add("Err Source2");
26+
}
27+
28+
@Test
29+
public void givenTwoSets_whenDifference_thenSetView() {
30+
Set<Integer> a = new HashSet<>(Arrays.asList(1,2,5));
31+
Set<Integer> b = new HashSet<>(Arrays.asList(1,2));
32+
SetUtils.SetView<Integer> result = SetUtils.difference(a, b);
33+
assertTrue(result.size() == 1 && result.contains(5));
34+
}
35+
36+
@Test
37+
public void givenTwoSets_whenUnion_thenUnionResult() {
38+
Set<Integer> a = new HashSet<>(Arrays.asList(1,2,5));
39+
Set<Integer> b = new HashSet<>(Arrays.asList(1,2));
40+
Set<Integer> expected = new HashSet<>(Arrays.asList(1,2,5));
41+
SetUtils.SetView<Integer> union = SetUtils.union(a, b);
42+
assertTrue(SetUtils.isEqualSet(expected, union));
43+
}
44+
45+
@Test
46+
public void givenTwoSets_whenIntersection_thenIntersectionResult() {
47+
Set<Integer> a = new HashSet<>(Arrays.asList(1,2,5));
48+
Set<Integer> b = new HashSet<>(Arrays.asList(1,2));
49+
Set<Integer> expected = new HashSet<>(Arrays.asList(1,2));
50+
SetUtils.SetView<Integer> intersect = SetUtils.intersection(a, b);
51+
assertTrue(SetUtils.isEqualSet(expected, intersect));
52+
}
53+
54+
@Test
55+
public void givenSet_whenTransformedSet_thenTransformedResult() {
56+
Set<Integer> a = SetUtils.transformedSet(new HashSet<>(), (e) -> e * 2 );
57+
a.add(2);
58+
assertEquals(a.toArray()[0], 4);
59+
60+
Set<Integer> source = new HashSet<>(Arrays.asList(1));
61+
Set<Integer> newSet = TransformedSet.transformedSet(source, (e) -> e * 2);
62+
assertEquals(newSet.toArray()[0], 2);
63+
assertEquals(source.toArray()[0], 2);
64+
}
65+
66+
@Test
67+
public void givenTwoSet_whenDisjunction_thenDisjunctionSet() {
68+
Set<Integer> a = new HashSet<>(Arrays.asList(1,2,5));
69+
Set<Integer> b = new HashSet<>(Arrays.asList(1,2,3));
70+
SetUtils.SetView<Integer> result = SetUtils.disjunction(a, b);
71+
assertTrue(result.toSet().contains(5) && result.toSet().contains(3));
72+
}
73+
74+
@Test
75+
public void givenSet_when_OrderedSet_thenMaintainElementOrder() {
76+
Set<Integer> set = new HashSet<>(Arrays.asList(10,1,5));
77+
System.out.println("unordered set: " + set);
78+
79+
Set<Integer> orderedSet = SetUtils.orderedSet(new HashSet<>());
80+
orderedSet.addAll(Arrays.asList(10,1,5));
81+
System.out.println("ordered set = " + orderedSet);
82+
}
83+
}

0 commit comments

Comments
 (0)