make a new collection if given a null one as input.

This commit is contained in:
2023-05-01 14:17:25 -05:00
parent 5070502bde
commit 1a8ab34fe3
4 changed files with 79 additions and 17 deletions

View File

@ -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;
@ -39,7 +40,7 @@ import com.kingsrook.qqq.backend.core.utils.lambdas.VoidVoidMethod;
public class MutableList<T> implements List<T>
{
private List<T> sourceList;
private Class<? extends List<T>> mutableTypeIfNeeded;
private Supplier<List<T>> supplierIfNeeded;
@ -49,7 +50,7 @@ public class MutableList<T> implements List<T>
*******************************************************************************/
public MutableList(List<T> sourceList)
{
this(sourceList, (Class) ArrayList.class);
this(sourceList, ArrayList::new);
}
@ -58,10 +59,20 @@ public class MutableList<T> implements List<T>
** Constructor
**
*******************************************************************************/
public MutableList(List<T> sourceList, Class<? extends List<T>> mutableTypeIfNeeded)
public MutableList(List<T> sourceList, Supplier<List<T>> supplierIfNeeded)
{
this.sourceList = sourceList;
this.mutableTypeIfNeeded = mutableTypeIfNeeded;
this.sourceList = Objects.requireNonNullElseGet(sourceList, supplierIfNeeded);
this.supplierIfNeeded = supplierIfNeeded;
}
/*******************************************************************************
**
*******************************************************************************/
List<T> getUnderlyingList()
{
return (sourceList);
}
@ -73,13 +84,13 @@ public class MutableList<T> implements List<T>
{
try
{
List<T> replacementList = mutableTypeIfNeeded.getConstructor().newInstance();
List<T> 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<T> implements List<T>
/*******************************************************************************
**
*******************************************************************************/
private <T> T doMutableOperationForValue(Supplier<T> supplier)
private <V> V doMutableOperationForValue(Supplier<V> supplier)
{
try
{

View File

@ -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;
@ -38,7 +39,7 @@ import com.kingsrook.qqq.backend.core.utils.lambdas.VoidVoidMethod;
public class MutableMap<K, V> implements Map<K, V>
{
private Map<K, V> sourceMap;
private Class<? extends Map<K, V>> mutableTypeIfNeeded;
private Supplier<Map<K, V>> supplierIfNeeded;
@ -48,7 +49,7 @@ public class MutableMap<K, V> implements Map<K, V>
*******************************************************************************/
public MutableMap(Map<K, V> sourceMap)
{
this(sourceMap, (Class) HashMap.class);
this(sourceMap, HashMap::new);
}
@ -57,10 +58,20 @@ public class MutableMap<K, V> implements Map<K, V>
** Constructor
**
*******************************************************************************/
public MutableMap(Map<K, V> sourceMap, Class<? extends Map<K, V>> mutableTypeIfNeeded)
public MutableMap(Map<K, V> sourceMap, Supplier<Map<K, V>> supplierIfNeeded)
{
this.sourceMap = sourceMap;
this.mutableTypeIfNeeded = mutableTypeIfNeeded;
this.sourceMap = Objects.requireNonNullElseGet(sourceMap, supplierIfNeeded);
this.supplierIfNeeded = supplierIfNeeded;
}
/*******************************************************************************
**
*******************************************************************************/
Map<K, V> getUnderlyingMap()
{
return (sourceMap);
}
@ -72,13 +83,13 @@ public class MutableMap<K, V> implements Map<K, V>
{
try
{
Map<K, V> replacementMap = mutableTypeIfNeeded.getConstructor().newInstance();
Map<K, V> 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));
}
}

View File

@ -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<Integer> list = new MutableList<>(null);
list.add(1);
assertEquals(1, list.size());
MutableList<Integer> mutableList = new MutableList<>(null, LinkedList::new);
mutableList.add(1);
assertEquals(1, mutableList.size());
assertEquals(LinkedList.class, mutableList.getUnderlyingList().getClass());
}
}

View File

@ -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<Integer, String> map = new MutableMap<>(null);
map.put(1, "one");
assertEquals(1, map.size());
MutableMap<Integer, String> mutableMap = new MutableMap<>(null, LinkedHashMap::new);
mutableMap.put(1, "uno");
assertEquals(1, mutableMap.size());
assertEquals(LinkedHashMap.class, mutableMap.getUnderlyingMap().getClass());
}
}