copying: deep vs shallow copy module (clone() traps, copy constructors, serialization copy, records, defensive copies, collection copies), 17 contract tests

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b4X2xx7RrveVQU9R1N
This commit is contained in:
asmhatre
2026-11-12 20:18:06 +05:30
parent 682b7b067c
commit 38fbd513c9
29 changed files with 1201 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
== 1. new ArrayList<>(list) and List.copyOf share the elements ==
boxes = [Box(100), Box(2)]
viaCtor = [Box(100), Box(2)]
viaCopyOf = [Box(100), Box(2)]
after boxes.add(Box(3)): viaCtor.size() = 2
== 2. The deep version needs the element copy, so a stream or a loop ==
boxes = [Box(-1), Box(2), Box(3)]
deep = [Box(100), Box(2), Box(3)]
== 3. HashMap copy constructor: new map, same values ==
mCopy.get("k") = Box(55)
== 4. 1-D primitive arrays: clone / copyOf are true copies ==
a = [99, 2, 3], clone = [1, 2, 3], copyOf = [1, 2, 3]
== 5. 2-D arrays: the rows are shared ==
grid = [[99, 2], [3, 4]]
shallow = [[99, 2], [3, 4]]
deepGrid = [[1, 2], [3, 4]]
== 6. Object arrays: copyOf copies references, not objects ==
arrCopy[0] = Box(77), same object: true