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

92
LINES

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

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

Transpiled version (3056L) is out of date.

1  
// unclear semantics as to whether return null on null
2  
3  
static <A> ArrayList<A> asList(A[] a) {
4  
  return a == null ? new ArrayList<A>() : new ArrayList<A>(Arrays.asList(a));
5  
}
6  
7  
static ArrayList<Integer> asList(int[] a) {
8  
  if (a == null) null;
9  
  ArrayList<Integer> l = emptyList(a.length);
10  
  for (int i : a) l.add(i);
11  
  ret l;
12  
}
13  
14  
static ArrayList<Long> asList(long[] a) {
15  
  if (a == null) null;
16  
  ArrayList<Long> l = emptyList(a.length);
17  
  for (long i : a) l.add(i);
18  
  ret l;
19  
}
20  
21  
static ArrayList<Float> asList(float[] a) {
22  
  if (a == null) null;
23  
  ArrayList<Float> l = emptyList(a.length);
24  
  for (float i : a) l.add(i);
25  
  ret l;
26  
}
27  
28  
static ArrayList<Double> asList(double[] a) {
29  
  if (a == null) null;
30  
  ArrayList<Double> l = emptyList(a.length);
31  
  for (double i : a) l.add(i);
32  
  ret l;
33  
}
34  
35  
static ArrayList<Short> asList(short[] a) {
36  
  if (a == null) null;
37  
  ArrayList<Short> l = emptyList(a.length);
38  
  for (short i : a) l.add(i);
39  
  ret l;
40  
}
41  
42  
static <A> ArrayList<A> asList(Iterator<A> it) {
43  
  new ArrayList l;
44  
  if (it != null)
45  
    while (it.hasNext())
46  
      l.add(it.next());
47  
  ret l;  
48  
}
49  
50  
// disambiguation
51  
static <A> ArrayList<A> asList(ItIt<A> s) {
52  
  ret asList((Iterator) s);
53  
}
54  
55  
static <A> ArrayList<A> asList(Iterable<A> s) {
56  
  if (s instanceof ArrayList) return (ArrayList) s;
57  
  ArrayList l = new ArrayList();
58  
  if (s != null)
59  
    for (A a : s)
60  
      l.add(a);
61  
  return l;
62  
}
63  
64  
ifclass Producer
65  
static <A> ArrayList<A> asList(Producer<A> p) {
66  
  new ArrayList l;
67  
  A a;
68  
  if (p != null) while ((a = p.next()) != null)
69  
    l.add(a);
70  
  ret l;
71  
}
72  
endif
73  
74  
static <A> ArrayList<A> asList(Enumeration<A> e) {
75  
  new ArrayList l;
76  
  if (e != null)
77  
    while (e.hasMoreElements())
78  
      l.add(e.nextElement());
79  
  return l;
80  
}
81  
82  
ifclass ReverseChain
83  
static <A> ArrayList<A> asList(ReverseChain<A> c) {
84  
  ret c == null ? emptyList() : c.toList();
85  
}
86  
endif
87  
88  
ifclass Pair
89  
static <A> L<A> asList(Pair<A> p) {
90  
  ret p == null ?: ll(p.a, p.b);
91  
}
92  
endif

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000999
Snippet name: asList - always returns a new ArrayList (no wrapping)
Eternal ID of this version: #1000999/20
Text MD5: a694e34e777db26c3bf156cb8255f0c3
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-18 00:05:15
Source code size: 2024 bytes / 92 lines
Pitched / IR pitched: No / No
Views / Downloads: 763 / 4755
Version history: 19 change(s)
Referenced in: [show references]