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

33
LINES

< > BotCompany Repo | #1007397 - optParam - synonym of optPar - get optional parameter from ThreadLocal

JavaX fragment (include)

static <A> A optParam(ThreadLocal<A> tl, A defaultValue) {
  ret optPar(tl, defaultValue);
}

static <A> A optParam(ThreadLocal<A> tl) {
  ret optPar(tl);
}

static O mapMethodLike optParam(S name, Map params) {
  ret mapGet(params, name);
}

// now also takes a map as single array entry
static <A> A optParam(O[] opt, S name, A defaultValue) {
  int n = l(opt);
  if (n == 1 && opt[0] instanceof Map) {
    Map map = cast opt[0];
    ret map.containsKey(name) ? (A) map.get(name) : defaultValue;
  }
  if (!even(l(opt))) fail("Odd parameter length");
  for (int i = 0; i < l(opt); i += 2)
    if (eq(opt[i], name))
      ret (A) opt[i+1];
  ret defaultValue;
}

sO optParam(O[] opt, S name) {
  ret optParam(opt, name, null);
}

sO mapMethodLike optParam(S name, O[] params) {
  ret optParam(params, name);
}

Author comment

Began life as a copy of #1006679

download  show line numbers  debug dex   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1007397
Snippet name: optParam - synonym of optPar - get optional parameter from ThreadLocal
Eternal ID of this version: #1007397/12
Text MD5: 0a7e54de17f1d576e3ec13303b1eee2d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-14 19:36:13
Source code size: 842 bytes / 33 lines
Pitched / IR pitched: No / No
Views / Downloads: 330 / 332
Version history: 11 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1007397 & http://1007397.tinybrain.de