Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

37
LINES

< > BotCompany Repo | #1001715 // shallowClone

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9276L/51K).

static <A> A shallowClone(A o) {
  ret (A) shallowClone_impl(o);
}

static <A> A shallowClone(A o, A emptyClone) {
  ret copyFields(o, emptyClone);
}

static O shallowClone_impl(O o) {
  if (o == null)
    return o;
    
  if (o instanceof List)
    ret cloneList((List) o);

  if (o cast Map) ret cloneMap(o);

  if (o instanceof String || o instanceof Number || o instanceof Boolean)
    return o;
    
  if (o instanceof Object[]) {
    Object[] l = (Object[]) o;
    ret l.clone();
  }
  
  // clone an arbitrary custom object
  
  O clone;
  if (o cast IMakeEmptyClone)
    clone = o.makeEmptyClone();
  else
    clone = nuEmptyObject(o.getClass());

  //print("Cloning custom: " + o);
  copyFields(o, clone);
  ret clone;
}

Author comment

Began life as a copy of #1001032

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1001715
Snippet name: shallowClone
Eternal ID of this version: #1001715/9
Text MD5: 3af8208ba9e7acbd681332a6009343f6
Transpilation MD5: 37b252e7be3803c1fdea1ab79147fce3
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-05 17:34:01
Source code size: 767 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 810 / 1101
Version history: 8 change(s)
Referenced in: #1002427 - Accellerating 629 (SPIKE)
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1015334 - cloneWithoutField (shallow clone)
#1030653 - shallowCloneElements - shallow-clone each element of a list
#3000382 - Answer for ferdie (>> t = 1, f = 0)