Add byte[] and ArrayList to more efficient version of deep copy / copy constructor

This commit is contained in:
2023-11-30 20:18:30 -06:00
parent 19d7559dbf
commit c3d35bf110
2 changed files with 29 additions and 1 deletions

View File

@ -158,10 +158,15 @@ public class QRecord implements Serializable
//////////////////////////////////////////////////////////////////////////
// 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());
}
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)
{
LOG.info("Non-primitive serializable value in QRecord - calling SerializationUtils.clone...", logPair("key", entry.getKey()), logPair("type", value.getClass()));

View File

@ -23,7 +23,9 @@ package com.kingsrook.qqq.backend.core.model.data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.kingsrook.qqq.backend.core.BaseTest;
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 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 org.junit.jupiter.api.Assertions.assertArrayEquals;
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.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
@ -140,6 +144,25 @@ class QRecordTest extends BaseTest
nullWarnings.setWarnings(null);
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 emptyClone = new QRecord(emptyRecord);
assertNull(emptyClone.getTableName());