From 1a8ab34fe309d069c04c3bbe084cadca06fc7b3a Mon Sep 17 00:00:00 2001 From: Darin Kelkhoff Date: Mon, 1 May 2023 14:17:25 -0500 Subject: [PATCH] make a new collection if given a null one as input. --- .../core/utils/collections/MutableList.java | 29 +++++++++++++------ .../core/utils/collections/MutableMap.java | 27 ++++++++++++----- .../utils/collections/MutableListTest.java | 20 +++++++++++++ .../utils/collections/MutableMapTest.java | 20 +++++++++++++ 4 files changed, 79 insertions(+), 17 deletions(-) diff --git a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/collections/MutableList.java b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/collections/MutableList.java index b43fd241..78084bbc 100644 --- a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/collections/MutableList.java +++ b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/collections/MutableList.java @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; +import java.util.Objects; import java.util.function.Supplier; import com.kingsrook.qqq.backend.core.utils.lambdas.VoidVoidMethod; @@ -38,8 +39,8 @@ import com.kingsrook.qqq.backend.core.utils.lambdas.VoidVoidMethod; *******************************************************************************/ public class MutableList implements List { - private List sourceList; - private Class> mutableTypeIfNeeded; + private List sourceList; + private Supplier> supplierIfNeeded; @@ -49,7 +50,7 @@ public class MutableList implements List *******************************************************************************/ public MutableList(List sourceList) { - this(sourceList, (Class) ArrayList.class); + this(sourceList, ArrayList::new); } @@ -58,10 +59,20 @@ public class MutableList implements List ** Constructor ** *******************************************************************************/ - public MutableList(List sourceList, Class> mutableTypeIfNeeded) + public MutableList(List sourceList, Supplier> supplierIfNeeded) { - this.sourceList = sourceList; - this.mutableTypeIfNeeded = mutableTypeIfNeeded; + this.sourceList = Objects.requireNonNullElseGet(sourceList, supplierIfNeeded); + this.supplierIfNeeded = supplierIfNeeded; + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + List getUnderlyingList() + { + return (sourceList); } @@ -73,13 +84,13 @@ public class MutableList implements List { try { - List replacementList = mutableTypeIfNeeded.getConstructor().newInstance(); + List replacementList = supplierIfNeeded.get(); replacementList.addAll(sourceList); sourceList = replacementList; } catch(Exception e) { - throw (new IllegalStateException("The mutable type provided for this MutableList [" + mutableTypeIfNeeded.getName() + "] could not be instantiated.")); + throw (new IllegalStateException("Error getting from the supplier provided for this MutableList.", e)); } } @@ -88,7 +99,7 @@ public class MutableList implements List /******************************************************************************* ** *******************************************************************************/ - private T doMutableOperationForValue(Supplier supplier) + private V doMutableOperationForValue(Supplier supplier) { try { diff --git a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/collections/MutableMap.java b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/collections/MutableMap.java index 79102a07..87789756 100644 --- a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/collections/MutableMap.java +++ b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/collections/MutableMap.java @@ -25,6 +25,7 @@ package com.kingsrook.qqq.backend.core.utils.collections; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.function.Supplier; import com.kingsrook.qqq.backend.core.utils.lambdas.VoidVoidMethod; @@ -37,8 +38,8 @@ import com.kingsrook.qqq.backend.core.utils.lambdas.VoidVoidMethod; *******************************************************************************/ public class MutableMap implements Map { - private Map sourceMap; - private Class> mutableTypeIfNeeded; + private Map sourceMap; + private Supplier> supplierIfNeeded; @@ -48,7 +49,7 @@ public class MutableMap implements Map *******************************************************************************/ public MutableMap(Map sourceMap) { - this(sourceMap, (Class) HashMap.class); + this(sourceMap, HashMap::new); } @@ -57,10 +58,20 @@ public class MutableMap implements Map ** Constructor ** *******************************************************************************/ - public MutableMap(Map sourceMap, Class> mutableTypeIfNeeded) + public MutableMap(Map sourceMap, Supplier> supplierIfNeeded) { - this.sourceMap = sourceMap; - this.mutableTypeIfNeeded = mutableTypeIfNeeded; + this.sourceMap = Objects.requireNonNullElseGet(sourceMap, supplierIfNeeded); + this.supplierIfNeeded = supplierIfNeeded; + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + Map getUnderlyingMap() + { + return (sourceMap); } @@ -72,13 +83,13 @@ public class MutableMap implements Map { try { - Map replacementMap = mutableTypeIfNeeded.getConstructor().newInstance(); + Map replacementMap = supplierIfNeeded.get(); replacementMap.putAll(sourceMap); sourceMap = replacementMap; } catch(Exception e) { - throw (new IllegalStateException("The mutable type provided for this MutableMap [" + mutableTypeIfNeeded.getName() + "] could not be instantiated.")); + throw (new IllegalStateException("Error getting from the supplier provided for this MutableMap.", e)); } } diff --git a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/collections/MutableListTest.java b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/collections/MutableListTest.java index 677509fd..6136f814 100644 --- a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/collections/MutableListTest.java +++ b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/collections/MutableListTest.java @@ -22,9 +22,11 @@ package com.kingsrook.qqq.backend.core.utils.collections; +import java.util.LinkedList; import java.util.List; import com.kingsrook.qqq.backend.core.BaseTest; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; /******************************************************************************* @@ -48,4 +50,22 @@ class MutableListTest extends BaseTest list.remove(0); } + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testNullInput() + { + List list = new MutableList<>(null); + list.add(1); + assertEquals(1, list.size()); + + MutableList mutableList = new MutableList<>(null, LinkedList::new); + mutableList.add(1); + assertEquals(1, mutableList.size()); + assertEquals(LinkedList.class, mutableList.getUnderlyingList().getClass()); + } + } \ No newline at end of file diff --git a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/collections/MutableMapTest.java b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/collections/MutableMapTest.java index cc2d160e..44e85544 100644 --- a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/collections/MutableMapTest.java +++ b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/collections/MutableMapTest.java @@ -22,9 +22,11 @@ package com.kingsrook.qqq.backend.core.utils.collections; +import java.util.LinkedHashMap; import java.util.Map; import com.kingsrook.qqq.backend.core.BaseTest; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; /******************************************************************************* @@ -48,4 +50,22 @@ class MutableMapTest extends BaseTest map.putAll(Map.of("c", 3, "d", 4)); } + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testNullInput() + { + Map map = new MutableMap<>(null); + map.put(1, "one"); + assertEquals(1, map.size()); + + MutableMap mutableMap = new MutableMap<>(null, LinkedHashMap::new); + mutableMap.put(1, "uno"); + assertEquals(1, mutableMap.size()); + assertEquals(LinkedHashMap.class, mutableMap.getUnderlyingMap().getClass()); + } + } \ No newline at end of file