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.

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<Char> asList(char[] a) {
8  
  if (a == null) null;
9  
  ArrayList<Char> l = emptyList(a.length);
10  
  for (i : a) l.add(i);
11  
  ret l;
12  
}
13  
14  
static ArrayList<Byte> asList(byte[] a) {
15  
  if (a == null) null;
16  
  ArrayList<Byte> l = emptyList(a.length);
17  
  for (i : a) l.add(i);
18  
  ret l;
19  
}
20  
21  
static ArrayList<Integer> asList(int[] a) {
22  
  if (a == null) null;
23  
  ArrayList<Integer> l = emptyList(a.length);
24  
  for (int i : a) l.add(i);
25  
  ret l;
26  
}
27  
28  
static ArrayList<Long> asList(long[] a) {
29  
  if (a == null) null;
30  
  ArrayList<Long> l = emptyList(a.length);
31  
  for (long i : a) l.add(i);
32  
  ret l;
33  
}
34  
35  
static ArrayList<Float> asList(float[] a) {
36  
  if (a == null) null;
37  
  ArrayList<Float> l = emptyList(a.length);
38  
  for (float i : a) l.add(i);
39  
  ret l;
40  
}
41  
42  
static ArrayList<Double> asList(double[] a) {
43  
  if (a == null) null;
44  
  ArrayList<Double> l = emptyList(a.length);
45  
  for (double i : a) l.add(i);
46  
  ret l;
47  
}
48  
49  
static ArrayList<Short> asList(short[] a) {
50  
  if (a == null) null;
51  
  ArrayList<Short> l = emptyList(a.length);
52  
  for (short i : a) l.add(i);
53  
  ret l;
54  
}
55  
56  
static <A> ArrayList<A> asList(Iterator<A> it) {
57  
  new ArrayList l;
58  
  if (it != null)
59  
    while (it.hasNext())
60  
      l.add(it.next());
61  
  ret l;  
62  
}
63  
64  
// disambiguation
65  
static <A> ArrayList<A> asList(ItIt<A> s) {
66  
  ret asList((Iterator) s);
67  
}
68  
69  
static <A> ArrayList<A> asList(Iterable<A> s) {
70  
  if (s instanceof ArrayList) return (ArrayList) s;
71  
  ArrayList l = new ArrayList();
72  
  if (s != null)
73  
    for (A a : s)
74  
      l.add(a);
75  
  return l;
76  
}
77  
78  
ifclass Producer
79  
static <A> ArrayList<A> asList(Producer<A> p) {
80  
  new ArrayList l;
81  
  A a;
82  
  if (p != null) while ((a = p.next()) != null)
83  
    l.add(a);
84  
  ret l;
85  
}
86  
endif
87  
88  
static <A> ArrayList<A> asList(Enumeration<A> e) {
89  
  new ArrayList l;
90  
  if (e != null)
91  
    while (e.hasMoreElements())
92  
      l.add(e.nextElement());
93  
  return l;
94  
}
95  
96  
ifclass ReverseChain
97  
static <A> ArrayList<A> asList(ReverseChain<A> c) {
98  
  ret c == null ? emptyList() : c.toList();
99  
}
100  
endif
101  
102  
ifclass Pair
103  
static <A> L<A> asList(Pair<A> p) {
104  
  ret p == null ?: ll(p.a, p.b);
105  
}
106  
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: 1025 / 5135
Version history: 21 change(s)
Referenced in: [show references]