Skip to content

Commit 0a631ac

Browse files
SeunMattzhendrikse
authored andcommitted
Example Code for BAEL-1048 (eugenp#2316)
* 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 * added example code for bytebuddy * added example code for PCollections * update pom
1 parent 7ffd8ec commit 0a631ac

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,11 @@
448448
<artifactId>byte-buddy-agent</artifactId>
449449
<version>${bytebuddy.version}</version>
450450
</dependency>
451+
<dependency>
452+
<groupId>org.pcollections</groupId>
453+
<artifactId>pcollections</artifactId>
454+
<version>${pcollections.version}</version>
455+
</dependency>
451456
</dependencies>
452457
<properties>
453458
<multiverse.version>0.7.0</multiverse.version>
@@ -487,5 +492,6 @@
487492
<org.hamcrest.java-hamcrest.version>2.0.0.0</org.hamcrest.java-hamcrest.version>
488493
<hll.version>1.6.0</hll.version>
489494
<bytebuddy.version>1.7.1</bytebuddy.version>
495+
<pcollections.version>2.1.2</pcollections.version>
490496
</properties>
491497
</project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.pcollections;
2+
3+
import org.junit.Test;
4+
import org.pcollections.*;
5+
6+
import java.util.Arrays;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.assertTrue;
13+
14+
public class PCollectionsUnitTest {
15+
16+
@Test
17+
public void whenEmpty_thenCreateEmptyHashPMap() {
18+
HashPMap<String, String> pmap = HashTreePMap.empty();
19+
assertEquals(pmap.size(), 0);
20+
}
21+
22+
@Test
23+
public void givenKeyValue_whenSingleton_thenCreateNonEmptyHashPMap() {
24+
HashPMap<String, String> pmap1 = HashTreePMap.singleton("key1", "value1");
25+
assertEquals(pmap1.size(), 1);
26+
}
27+
28+
@Test
29+
public void givenExistingHashMap_whenFrom_thenCreateHashPMap() {
30+
Map map = new HashMap();
31+
map.put("mkey1", "mval1");
32+
map.put("mkey2", "mval2");
33+
34+
HashPMap<String, String> pmap2 = HashTreePMap.from(map);
35+
assertEquals(pmap2.size(), 2);
36+
}
37+
38+
@Test
39+
public void whenHashPMapMethods_thenPerformOperations() {
40+
41+
HashPMap<String, String> pmap = HashTreePMap.empty();
42+
pmap = pmap.plus("key1", "value1");
43+
assertEquals(pmap.size(), 1);
44+
45+
Map map = new HashMap();
46+
map.put("key2", "val2");
47+
map.put("key3", "val3");
48+
pmap = pmap.plusAll(map);
49+
50+
assertEquals(pmap.size(), 3);
51+
52+
pmap = pmap.minus("key1");
53+
assertFalse(pmap.containsKey("key1"));
54+
55+
pmap = pmap.minusAll(map.keySet());
56+
assertEquals(pmap.size(), 0);
57+
58+
}
59+
60+
@Test
61+
public void whenTreePVectorMethods_thenPerformOperations() {
62+
TreePVector pVector = TreePVector.empty();
63+
64+
pVector = pVector.plus("e1");
65+
pVector = pVector.plusAll(Arrays.asList("e2", "e3", "e4"));
66+
assertEquals(4, pVector.size());
67+
68+
TreePVector pSub = pVector.subList(0, 2);
69+
assertTrue(pSub.contains("e1") && pSub.contains("e2"));
70+
71+
TreePVector pVW = (TreePVector) pVector.with(0, "e10");
72+
assertEquals(pVW.get(0), "e10");
73+
74+
pVector = pVector.minus("e1");
75+
TreePVector pV1 = pVector.minusAll(Arrays.asList("e2", "e3"));
76+
assertEquals(pV1.size(), 1);
77+
}
78+
79+
@Test
80+
public void whenMapPSetMethods_thenPerformOperations() {
81+
82+
MapPSet pSet = HashTreePSet.empty()
83+
.plusAll(Arrays.asList("e1","e2","e3","e4"));
84+
assertEquals(pSet.size(), 4);
85+
86+
pSet = pSet.minus("e4");
87+
assertFalse(pSet.contains("e4"));
88+
89+
}
90+
91+
}

0 commit comments

Comments
 (0)