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

106
LINES

< > BotCompany Repo | #1000999 // asList - always returns a new ArrayList (no wrapping)

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

Transpiled version (341L) is out of date.

// unclear semantics as to whether return null on null

static <A> ArrayList<A> asList(A[] a) {
  return a == null ? new ArrayList<A>() : new ArrayList<A>(Arrays.asList(a));
}

static ArrayList<Char> asList(char[] a) {
  if (a == null) null;
  ArrayList<Char> l = emptyList(a.length);
  for (i : a) l.add(i);
  ret l;
}

static ArrayList<Byte> asList(byte[] a) {
  if (a == null) null;
  ArrayList<Byte> l = emptyList(a.length);
  for (i : a) l.add(i);
  ret l;
}

static ArrayList<Integer> asList(int[] a) {
  if (a == null) null;
  ArrayList<Integer> l = emptyList(a.length);
  for (int i : a) l.add(i);
  ret l;
}

static ArrayList<Long> asList(long[] a) {
  if (a == null) null;
  ArrayList<Long> l = emptyList(a.length);
  for (long i : a) l.add(i);
  ret l;
}

static ArrayList<Float> asList(float[] a) {
  if (a == null) null;
  ArrayList<Float> l = emptyList(a.length);
  for (float i : a) l.add(i);
  ret l;
}

static ArrayList<Double> asList(double[] a) {
  if (a == null) null;
  ArrayList<Double> l = emptyList(a.length);
  for (double i : a) l.add(i);
  ret l;
}

static ArrayList<Short> asList(short[] a) {
  if (a == null) null;
  ArrayList<Short> l = emptyList(a.length);
  for (short i : a) l.add(i);
  ret l;
}

static <A> ArrayList<A> asList(Iterator<A> it) {
  new ArrayList l;
  if (it != null)
    while (it.hasNext())
      l.add(it.next());
  ret l;  
}

// disambiguation
static <A> ArrayList<A> asList(ItIt<A> s) {
  ret asList((Iterator) s);
}

static <A> ArrayList<A> asList(Iterable<A> s) {
  if (s instanceof ArrayList) return (ArrayList) s;
  ArrayList l = new ArrayList();
  if (s != null)
    for (A a : s)
      l.add(a);
  return l;
}

ifclass Producer
static <A> ArrayList<A> asList(Producer<A> p) {
  new ArrayList l;
  A a;
  if (p != null) while ((a = p.next()) != null)
    l.add(a);
  ret l;
}
endif

static <A> ArrayList<A> asList(Enumeration<A> e) {
  new ArrayList l;
  if (e != null)
    while (e.hasMoreElements())
      l.add(e.nextElement());
  return l;
}

ifclass ReverseChain
static <A> ArrayList<A> asList(ReverseChain<A> c) {
  ret c == null ? emptyList() : c.toList();
}
endif

ifclass Pair
static <A> L<A> asList(Pair<A> p) {
  ret p == null ?: ll(p.a, p.b);
}
endif

download  show line numbers  debug dex  old transpilations   

Travelled to 20 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, djztyncnmsck, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, wnsclhtenguj, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000999
Snippet name: asList - always returns a new ArrayList (no wrapping)
Eternal ID of this version: #1000999/22
Text MD5: f5407d7a512ad588902c62fd7157c2e6
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-08 01:31:54
Source code size: 2326 bytes / 106 lines
Pitched / IR pitched: No / No
Views / Downloads: 1024 / 5134
Version history: 21 change(s)
Referenced in: [show references]