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

32
LINES

< > BotCompany Repo | #1022935 // RandomAndVary_Record

JavaX fragment (include)

1  
sclass RandomAndVary_Record<A> implements RandomAndVary<A> {
2  
  F0<A> makeInstance;
3  
  new Map<S, RandomAndVary> handlers; // key = field name
4  
5  
  *(Class<A> theClass, O... params) {
6  
    makeInstance = func -> A { nu(theClass) };
7  
    addAll(handlers, paramsToMap(params));
8  
  }
9  
  
10  
  *(A prototype, O... params) {
11  
    makeInstance = func -> A { shallowClone(prototype) };
12  
    addAll(handlers, paramsToMap(params));
13  
  }
14  
  
15  
  public A randomInstance() {
16  
    A a = makeInstance!;
17  
    for (S field, RandomAndVary handler : handlers) {
18  
      setOpt(a, field, handler.randomInstance());
19  
    }
20  
    ret a;
21  
  }
22  
  
23  
  public A varyInstance(A a) {
24  
    a = shallowClone(a);
25  
    S field = random(keys(handlers));
26  
    RandomAndVary handler = handlers.get(field);
27  
    O o = getOpt(a, field);
28  
    if (o == null) null;
29  
    setOpt(a, field, handler.varyInstance(o));
30  
    ret a;
31  
  }
32  
}

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: 217 / 659
Version history: 7 change(s)
Referenced in: [show references]