From 471c1e45bb9a889fcf063732ad1d42b0655cb8f1 Mon Sep 17 00:00:00 2001 From: Seun Matt Date: Thu, 27 Jul 2017 10:42:11 +0100 Subject: [PATCH] added example code for PCollections --- libraries/pom.xml | 6 ++ .../pcollections/PCollectionsUnitTest.java | 91 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index 6ab312a535..d78d1b28ec 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -458,6 +458,11 @@ byte-buddy-agent ${bytebuddy.version} + + org.pcollections + pcollections + ${pcollections.version} + 0.7.0 @@ -497,5 +502,6 @@ 2.0.0.0 1.6.0 1.7.1 + 2.1.2 diff --git a/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java b/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java new file mode 100644 index 0000000000..b2f815074a --- /dev/null +++ b/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java @@ -0,0 +1,91 @@ +package com.baeldung.pcollections; + +import org.junit.Test; +import org.pcollections.*; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class PCollectionsUnitTest { + + @Test + public void whenEmpty_thenCreateEmptyHashPMap() { + HashPMap pmap = HashTreePMap.empty(); + assertEquals(pmap.size(), 0); + } + + @Test + public void givenKeyValue_whenSingleton_thenCreateNonEmptyHashPMap() { + HashPMap pmap1 = HashTreePMap.singleton("key1", "value1"); + assertEquals(pmap1.size(), 1); + } + + @Test + public void givenExistingHashMap_whenFrom_thenCreateHashPMap() { + Map map = new HashMap(); + map.put("mkey1", "mval1"); + map.put("mkey2", "mval2"); + + HashPMap pmap2 = HashTreePMap.from(map); + assertEquals(pmap2.size(), 2); + } + + @Test + public void whenHashPMapMethods_thenPerformOperations() { + + HashPMap pmap = HashTreePMap.empty(); + pmap = pmap.plus("key1", "value1"); + assertEquals(pmap.size(), 1); + + Map map = new HashMap(); + map.put("key2", "val2"); + map.put("key3", "val3"); + pmap = pmap.plusAll(map); + + assertEquals(pmap.size(), 3); + + pmap = pmap.minus("key1"); + assertFalse(pmap.containsKey("key1")); + + pmap = pmap.minusAll(map.keySet()); + assertEquals(pmap.size(), 0); + + } + + @Test + public void whenTreePVectorMethods_thenPerformOperations() { + TreePVector pVector = TreePVector.empty(); + + pVector = pVector.plus("e1"); + pVector = pVector.plusAll(Arrays.asList("e2", "e3", "e4")); + assertEquals(4, pVector.size()); + + TreePVector pSub = pVector.subList(0, 2); + assertTrue(pSub.contains("e1") && pSub.contains("e2")); + + TreePVector pVW = (TreePVector) pVector.with(0, "e10"); + assertEquals(pVW.get(0), "e10"); + + pVector = pVector.minus("e1"); + TreePVector pV1 = pVector.minusAll(Arrays.asList("e2", "e3")); + assertEquals(pV1.size(), 1); + } + + @Test + public void whenMapPSetMethods_thenPerformOperations() { + + MapPSet pSet = HashTreePSet.empty() + .plusAll(Arrays.asList("e1","e2","e3","e4")); + assertEquals(pSet.size(), 4); + + pSet = pSet.minus("e4"); + assertFalse(pSet.contains("e4")); + + } + +}