// optimistic signature that usually holds static <A> A clone(A o) { new ArrayDeque<Runnable> q; Object x = clone_impl(o, new IdentityHashMap, q); while (!q.isEmpty()) q.poll().run(); return (A) x; } static O clone_impl(O o, final IdentityHashMap seen, final ArrayDeque<Runnable> q) ctex { if (o == null) null; Object y = seen.get(o); if (y != null) return y; if (o instanceof L) { //print("Cloning list: " + o); final new L l; seen.put(o, l); for (final O x : (L) o) q.add(r { l.add(clone_impl(x, seen, q)) }); return l; } if (o instanceof Map) { final Map m = similarEmptyMap((Map) o); seen.put(o, m); for (O entry : ((Map) o).entrySet()) { final Map.Entry e = (Map.Entry) entry; q.add(r { m.put(clone_impl(e.getKey(), seen, q), clone_impl(e.getValue(), seen, q)) }); } ret m; } if (o instanceof S || o instanceof Number || o instanceof Bool) ret o; if (o instanceof O[]) { final O[] l = (O[]) o; final O[] l2 = l.clone(); seen.put(o, l2); for (int i = 0; i < l.length; i++) { final int _i = i; q.add(r { l2[_i] = clone_impl(l[_i], seen, q) }); } ret l2; } // clone an arbitrary custom object //print("Cloning custom: " + o); final O clone = nuObjectWithoutArguments(o.getClass()); seen.put(o, clone); Class c = o.getClass(); while (c != O.class) { Field[] fields = c.getDeclaredFields(); for (final Field field : fields) { if ((field.getModifiers() & Modifier.STATIC) != 0) continue; field.setAccessible(true); final O value = field.get(o); q.add(r { field.set(clone, clone_impl(value, seen, q)) }); } c = c.getSuperclass(); } ret clone; }
Began life as a copy of #1001005
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
ID | Author/Program | Comment | Date |
---|---|---|---|
1167 | stefan | deep clone! | 2015-11-08 15:34:47 |
Snippet ID: | #1001032 |
Snippet name: | clone function, with queue (clone objects from outside by going through structure) |
Eternal ID of this version: | #1001032/8 |
Text MD5: | 513dffb46e1df76e5f281953e09a7b8c |
Author: | stefan |
Category: | |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-05-28 00:13:36 |
Source code size: | 1839 bytes / 68 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 1095 / 1970 |
Version history: | 7 change(s) |
Referenced in: | [show references] |