mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
Add byte[] and ArrayList to more efficient version of deep copy / copy constructor
This commit is contained in:
@ -158,10 +158,15 @@ public class QRecord implements Serializable
|
|||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// not sure from where/how java.sql.Date objects are getting in here... //
|
// not sure from where/how java.sql.Date objects are getting in here... //
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
if(value == null || value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof Temporal || value instanceof Date)
|
if(value == null || value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof Temporal || value instanceof Date || value instanceof byte[])
|
||||||
{
|
{
|
||||||
clone.put(entry.getKey(), entry.getValue());
|
clone.put(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
|
else if(entry.getValue() instanceof ArrayList<?> arrayList)
|
||||||
|
{
|
||||||
|
ArrayList<?> cloneList = new ArrayList<>(arrayList);
|
||||||
|
clone.put(entry.getKey(), (V) cloneList);
|
||||||
|
}
|
||||||
else if(entry.getValue() instanceof Serializable serializableValue)
|
else if(entry.getValue() instanceof Serializable serializableValue)
|
||||||
{
|
{
|
||||||
LOG.info("Non-primitive serializable value in QRecord - calling SerializationUtils.clone...", logPair("key", entry.getKey()), logPair("type", value.getClass()));
|
LOG.info("Non-primitive serializable value in QRecord - calling SerializationUtils.clone...", logPair("key", entry.getKey()), logPair("type", value.getClass()));
|
||||||
|
@ -23,7 +23,9 @@ package com.kingsrook.qqq.backend.core.model.data;
|
|||||||
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import com.kingsrook.qqq.backend.core.BaseTest;
|
import com.kingsrook.qqq.backend.core.BaseTest;
|
||||||
import com.kingsrook.qqq.backend.core.model.statusmessages.BadInputStatusMessage;
|
import com.kingsrook.qqq.backend.core.model.statusmessages.BadInputStatusMessage;
|
||||||
@ -31,7 +33,9 @@ import com.kingsrook.qqq.backend.core.utils.collections.MapBuilder;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import static com.kingsrook.qqq.backend.core.model.data.QRecord.BACKEND_DETAILS_TYPE_HEAVY_FIELD_LENGTHS;
|
import static com.kingsrook.qqq.backend.core.model.data.QRecord.BACKEND_DETAILS_TYPE_HEAVY_FIELD_LENGTHS;
|
||||||
import static com.kingsrook.qqq.backend.core.model.data.QRecord.BACKEND_DETAILS_TYPE_JSON_SOURCE_OBJECT;
|
import static com.kingsrook.qqq.backend.core.model.data.QRecord.BACKEND_DETAILS_TYPE_JSON_SOURCE_OBJECT;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
@ -140,6 +144,25 @@ class QRecordTest extends BaseTest
|
|||||||
nullWarnings.setWarnings(null);
|
nullWarnings.setWarnings(null);
|
||||||
assertNull(new QRecord(nullWarnings).getWarnings());
|
assertNull(new QRecord(nullWarnings).getWarnings());
|
||||||
|
|
||||||
|
QRecord byteArrayValue = new QRecord().withValue("myBytes", new byte[] { 65, 66, 67, 68 });
|
||||||
|
assertArrayEquals(new byte[] { 65, 66, 67, 68 }, new QRecord(byteArrayValue).getValueByteArray("myBytes"));
|
||||||
|
|
||||||
|
ArrayList<Integer> originalArrayList = new ArrayList<>(List.of(1, 2, 3));
|
||||||
|
QRecord recordWithArrayListValue = new QRecord().withValue("myList", originalArrayList);
|
||||||
|
QRecord cloneWithArrayListValue = new QRecord(recordWithArrayListValue);
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// the clone list and original list should be equals (have contents that are equals), but not be the same (reference) //
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
assertEquals(List.of(1, 2, 3), cloneWithArrayListValue.getValue("myList"));
|
||||||
|
assertNotSame(originalArrayList, cloneWithArrayListValue.getValue("myList"));
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// make sure a change to the original list doesn't change the cloned list (as it was cloned deeply) //
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
originalArrayList.add(4);
|
||||||
|
assertNotEquals(originalArrayList, cloneWithArrayListValue.getValue("myList"));
|
||||||
|
|
||||||
QRecord emptyRecord = new QRecord();
|
QRecord emptyRecord = new QRecord();
|
||||||
QRecord emptyClone = new QRecord(emptyRecord);
|
QRecord emptyClone = new QRecord(emptyRecord);
|
||||||
assertNull(emptyClone.getTableName());
|
assertNull(emptyClone.getTableName());
|
||||||
|
Reference in New Issue
Block a user