Modes¶
Cloner cloner = Cloners.builder()
.mode(CloningMode.SEQUENTIAL)
.traversalAlgorithm(TraversalAlgorithm.BREADTH_FIRST)
.build();
| Mode | How the graph is copied |
|---|---|
SEQUENTIAL (default) |
one thread, without recursion: any depth of the graph, e.g. a linked list of millions of nodes |
RECURSIVE |
one thread, by recursion: errors name the whole path to the object, the stack limits the depth of the graph |
PARALLEL |
the objects are copied by the tasks of an executor |
In the sequential mode the traversal algorithm is DEPTH_FIRST (default) or BREADTH_FIRST; it changes the order
of copying, not the result.
Recursive mode for diagnostics¶
The recursive mode copies an object while the objects which reference it are still being copied, so it knows the whole path from the root. Use it to find where an uncopyable object sits in a large graph: the sequential and the parallel mode name only the object which references it.
sequential: Cannot copy java.lang.Thread in com.acme.Task ...
recursive: Cannot copy java.lang.Thread in com.acme.Task in java.util.ArrayList in com.acme.Job ...
The stack of the recursive mode also follows the graph, which helps in a debugger. Deep graphs, e.g. long linked lists, overflow the stack in this mode.
Parallel mode¶
Without an executor the parallel mode uses ForkJoinPool.commonPool(). Parallel copying pays off for large graphs
with many objects; for small ones the sequential mode is faster.
Shared references and cycles¶
Every object of the graph is copied once: if two objects reference the same object, their clones reference the same
clone, and cycles are reproduced. Objects which create their clones from already copied values (records, Copyable,
single-phase custom copiers) cannot be completed while an object they wait for waits for them; the cloner reports such
cycles with a ClonerException, see Troubleshooting.