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

32
LINES

< > BotCompany Repo | #1022935 // RandomAndVary_Record

JavaX fragment (include)

sclass RandomAndVary_Record<A> implements RandomAndVary<A> {
  F0<A> makeInstance;
  new Map<S, RandomAndVary> handlers; // key = field name

  *(Class<A> theClass, O... params) {
    makeInstance = func -> A { nu(theClass) };
    addAll(handlers, paramsToMap(params));
  }
  
  *(A prototype, O... params) {
    makeInstance = func -> A { shallowClone(prototype) };
    addAll(handlers, paramsToMap(params));
  }
  
  public A randomInstance() {
    A a = makeInstance!;
    for (S field, RandomAndVary handler : handlers) {
      setOpt(a, field, handler.randomInstance());
    }
    ret a;
  }
  
  public A varyInstance(A a) {
    a = shallowClone(a);
    S field = random(keys(handlers));
    RandomAndVary handler = handlers.get(field);
    O o = getOpt(a, field);
    if (o == null) null;
    setOpt(a, field, handler.varyInstance(o));
    ret a;
  }
}

Author comment

Began life as a copy of #1022932

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1022935
Snippet name: RandomAndVary_Record
Eternal ID of this version: #1022935/8
Text MD5: d4f28d63e575dd2507f9867e1283eb18
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-05-05 19:41:45
Source code size: 890 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 208 / 647
Version history: 7 change(s)
Referenced in: [show references]