-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsList.java
672 lines (642 loc) · 26.2 KB
/
ConsList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
package io.github.nblxa.cons;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.ThreadSafe;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* The ultimate immutable and thread-safe Cons List that implements {@link java.util.Collection}
* and avoids recursive calls.
*
* <p>Usage:
*
* <pre>
* import static io.github.nblxa.cons.ConsList.*;
* ...
* ConsList<String> list = list("Apples", "Oranges", "Bananas");
* list.forEach(fruit -> System.out.println("I like " + fruit);</pre>
*
* @param <E> element type
*/
@Immutable
@ThreadSafe
public interface ConsList<E> extends Collection<E> {
/**
* Returns the empty <tt>ConsList</tt>.
*
* <p>The result is a singleton instance shared by all empty cons lists.
*
* <p><tt>tail</tt> collection, if not of type <tt>ConsList</tt>, will be converted to it.
*
* @param <T> element type
* @return singleton empty list
*/
@NonNull
@SuppressWarnings("unchecked")
static <T> Nil<T> nil() {
return (Nil<T>) Nil.INSTANCE;
}
/**
* Constructs a new <tt>ConsList</tt> with elements <tt>head</tt> and <tt>tail</tt>.
*
* <p>The head, the tail elements and the resulting list have the same compile-time type.
*
* <p>The original list is not modified.
*
* @param head first element of the new list
* @param tail <tt>ConsList</tt> with the second and consecutive elements of the new list
* @param <V> element type
* @return a cons list with the given head and tail elements
*/
@NonNull
static <V> ConsList<V> cons(@Nullable V head, @NonNull ConsList<V> tail) {
return new ConsListImpl<>(head, Objects.requireNonNull(tail, ConsUtil.MSG_TAIL_IS_NULL));
}
/**
* Constructs a new <tt>ConsList</tt> with elements <tt>head</tt> and <tt>tail</tt>.
*
* <p>The head, the tail elements and the resulting list have the same compile-time type.
*
* <p><tt>tail</tt> collection, if not of type <tt>ConsList</tt>, will be converted to it.
*
* <p>The original list is not modified.
*
* @param head first element of the new list
* @param tail collection of second and consecutive elements of the new list;
* @param <V> element type
* @return a cons list with the given head and tail elements
*/
@NonNull
static <V> ConsList<V> cons(@Nullable V head, @NonNull Collection<V> tail) {
return new ConsListImpl<>(head, consList(tail));
}
/**
* Constructs a new <tt>ConsList</tt> of element type <tt>klass</tt> with elements <tt>head</tt>
* and <tt>tail</tt>.
*
* <p>The head, the tail elements and the resulting list have the same compile-time type
* defined by the parameter <tt>klass</tt>. This type must be the common supertype
* of both the new head element and the elements of the tail. The parameter is required
* to overcome the deficiencies of the Java generics and type inference in JDK 8.
*
* <p>This works without the explicit type parameter:
* <pre>ConsList<Number> l = cons(3.14d, cons(10, nil()));</pre>
*
* <p>But this requires it:
* <pre>ConsList<Integer> l = cons(10, nil());
* ConsList<Number> l = cons(3.14d, i, Number.class);</pre>
*
* <p>The original list is not modified.
*
* @param head first element of the new list
* @param tail <tt>ConsList</tt> with the second and consecutive elements of the new list
* @param klass type-evidence parameter, unused at runtime, and only required
* to provide static type binding at compile-time
* @param <V> element type of the resulting list: base class of both <tt>head</tt>
* and elements of <tt>tail</tt>
* @param <U> element type of the new list's head
* @return a cons list with the given head and tail elements and of given element type
*/
@NonNull
@SuppressWarnings( {"rawtypes", "unchecked", "unused"})
static <V, U extends V> ConsList<V> cons(@Nullable U head,
@NonNull ConsList<? extends V> tail,
@NonNull Class<V> klass) {
return (ConsList<V>) new ConsListImpl(head, Objects.requireNonNull(tail, ConsUtil.MSG_TAIL_IS_NULL));
}
/**
* Constructs a new cons list of element type <tt>klass</tt> with elements <tt>head</tt>
* and <tt>tail</tt>.
*
* <p>The head, the tail elements and the resulting list have the same compile-time type
* defined by the parameter <tt>klass</tt>. This type must be the common supertype
* of both the new head element and the elements of the tail. The parameter is required
* to overcome the deficiencies of the Java generics and type inference in JDK 8.
*
* <p>This works without the explicit type parameter:
* <pre>ConsList<Number> l = cons(3.14d, cons(10, nil()));</pre>
*
* <p>But this requires it:
* <pre>ConsList<Integer> l = cons(10, nil());
* ConsList<Number> l = cons(3.14d, i, Number.class);</pre>
*
* <p>The original list is not modified.
*
* @param head first element of the new list
* @param tail collection of second and consecutive elements of the new list;
* if it not of type <tt>ConsList</tt>, it will be converted
* @param klass type-evidence parameter, unused at runtime, and only required
* to provide static type binding at compile-time
* @param <V> element type of the resulting list: base class of both <tt>head</tt>
* and elements of <tt>tail</tt>
* @param <U> element type of the new list's head
* @return a cons list with the given head and tail elements and of given element type
*/
@NonNull
@SuppressWarnings( {"rawtypes", "unchecked", "unused"})
static <V, U extends V> ConsList<V> cons(@Nullable U head,
@NonNull Collection<? extends V> tail,
@NonNull Class<V> klass) {
return (ConsList<V>) new ConsListImpl(head, consList(tail));
}
/**
* Constructs a new cons list containing elements in the given order.
*
* <p>An invocation with an empty parameter list will produce the empty
* list instance <tt>nil()</tt>.
*
* <p>With a non-empty parameter array, its first element is the head of the new cons list,
* the rest are the parameters for constructing its tail.
*
* @param elements any number of elements of the list to be constructed
* @param <V> element type
* @return the cons list with the elements consList the argument array in the same order
*/
@NonNull
@SafeVarargs
static <V> ConsList<V> list(@NonNull V... elements) {
ConsList<V> cons = nil();
for (int i = elements.length - 1; i >= 0; i--) {
cons = new ConsListImpl<>(elements[i], cons);
}
return cons;
}
/**
* Constructs a new <tt>ConsList</tt> from the given {@link Iterable}.
*
* <p>If the {@link Iterable} is itself a ConsList, returns the typecast iterable.
*
* <p>For instances of {@link List}, only one iteration backwards through the list's
* {@link ListIterator} will be done.
*
* <p>For all other {@link Iterable} types, this traverses both the Iterable
* and the intermediate ConsList once.
*
* @param iterable input iterable
* @param <V> element type
* @return the cons list with the elements consList the Iterable in the same order
*/
@NonNull
static <V> ConsList<V> consList(@NonNull Iterable<V> iterable) {
Objects.requireNonNull(iterable, ConsUtil.MSG_ITERABLE_IS_NULL);
if (iterable instanceof ConsList) {
return (ConsList<V>) iterable;
}
ConsList<V> cons = nil();
if (iterable instanceof List) {
List<V> list = (List<V>) iterable;
ListIterator<V> iter = list.listIterator(list.size());
while (iter.hasPrevious()) {
cons = new ConsListImpl<>(iter.previous(), cons);
}
return cons;
} else {
for (V v : iterable) {
cons = new ConsListImpl<>(v, cons);
}
return cons.reverse();
}
}
/**
* Returns a <tt>ConsList</tt> that contains the concatenation of elements of all argument cons lists.
*
* @param first the first argument cons list, not nullable
* @param rest the rest of the argument cons lists, each of them not nullable
* @param <V> element type of all argument lists and the resulting list
* @return the concatentation of all argument lists
*/
@NonNull
@SafeVarargs
static <V> ConsList<V> concat(@NonNull ConsList<V> first, @NonNull ConsList<V>... rest) {
Objects.requireNonNull(first, "Null concat argument at position 0");
Objects.requireNonNull(rest, ConsUtil.MSG_ARG_ARRAY_REST_IS_NULL);
if (rest.length == 0) {
return first;
}
ConsList<V> result = rest[rest.length - 1];
if (result == null) {
throw new NullPointerException(ConsUtil.MSG_NULL_CONCAT_ARG_AT_POS + rest.length);
}
for (int i = rest.length - 2; i >= -1; i--) {
ConsList<V> cons;
if (i == -1) {
cons = first;
} else {
if (rest[i] == null) {
throw new NullPointerException(ConsUtil.MSG_NULL_CONCAT_ARG_AT_POS + (i + 1));
}
cons = rest[i].reverse();
}
while (cons != Nil.INSTANCE) {
result = new ConsListImpl<>(cons.head(), result);
cons = cons.tail();
}
}
return result;
}
/**
* Returns a {@link Collector} collecting a Stream into a cons list.
*
* @param <T> element type
* @return a collector collecting into a cons list
*/
@NonNull
static <T> Collector<T, ?, ConsList<T>> toConsCollector() {
return Collectors.collectingAndThen(Collectors.toList(), ConsList::consList);
}
/**
* Constructs a new <tt>IntConsList</tt> with elements <tt>head</tt> and <tt>tail</tt>.
*
* <p>The head, the tail elements and the resulting list have the same compile-time type.
*
* <p>The original list is not modified.
*
* @param head first element of the new list
* @param tail <tt>ConsList</tt> with the second and consecutive elements of the new list
* @return a cons list with the given head and tail elements
*/
@NonNull
static IntConsList<Integer> intCons(int head, @NonNull IntConsList<Integer> tail) {
return new IntConsListImpl(head, Objects.requireNonNull(tail, ConsUtil.MSG_TAIL_IS_NULL));
}
/**
* Constructs a new <tt>IntConsList</tt> with elements <tt>head</tt> and <tt>tail</tt>.
*
* <p>The head, the tail elements and the resulting list have the same compile-time type.
*
* <p><tt>tail</tt> collection, if not of type <tt>ConsList</tt>, will be converted to it.
*
* <p>The original list is not modified.
*
* @param head first element of the new list
* @param tail collection of second and consecutive elements of the new list;
* @return a cons list with the given head and tail elements
*/
@NonNull
static IntConsList<Integer> intCons(int head, @NonNull Collection<Integer> tail) {
return new IntConsListImpl(head, intConsList(tail));
}
/**
* Constructs a new <tt>IntConsList</tt> containing elements in the given order.
*
* <p>An invocation with an empty parameter list will produce the empty
* list instance <tt>nil()</tt>.
*
* <p>With a non-empty parameter array, its first element is the head of the new cons list,
* the rest are the parameters for constructing its tail.
*
* @param elements any number of elements of the list to be constructed
* @return the cons list with the elements consList the argument array in the same order
*/
@NonNull
static IntConsList<Integer> intList(@NonNull int... elements) {
IntConsList<Integer> cons = nil();
for (int i = elements.length - 1; i >= 0; i--) {
cons = new IntConsListImpl(elements[i], cons);
}
return cons;
}
/**
* Constructs a new <tt>IntConsList</tt> from the given {@link Iterable}.
*
* <p>If the {@link Iterable} is itself a ConsList, returns the typecast iterable.
*
* <p>For instances of {@link List}, only one iteration backwards through the list's
* {@link ListIterator} will be done.
*
* <p>For all other {@link Iterable} types, this traverses both the Iterable
* and the intermediate ConsList once.
*
* @param iterable input iterable
* @return the cons list with the elements consList the Iterable in the same order
*/
@NonNull
static IntConsList<Integer> intConsList(@NonNull Iterable<Integer> iterable) {
Objects.requireNonNull(iterable, ConsUtil.MSG_ITERABLE_IS_NULL);
if (iterable instanceof IntConsList) {
return (IntConsList<Integer>) iterable;
}
IntConsList<Integer> cons = nil();
if (iterable instanceof List) {
List<Integer> list = (List<Integer>) iterable;
ListIterator<Integer> iter = list.listIterator(list.size());
while (iter.hasPrevious()) {
cons = new IntConsListImpl(iter.previous(), cons);
}
return cons;
} else {
for (Integer v : iterable) {
cons = new IntConsListImpl(v, cons);
}
return cons.intReverse();
}
}
/**
* Returns an <tt>IntConsList</tt> that contains the concatenation of elements of all argument IntConsLists.
*
* @param first the first argument cons list, not nullable
* @param rest the rest of the argument cons lists, each of them not nullable
* @return the concatentation of all argument lists
*/
@NonNull
@SafeVarargs
static IntConsList<Integer> concat(@NonNull IntConsList<Integer> first, @NonNull IntConsList<Integer>... rest) {
Objects.requireNonNull(first, ConsUtil.MSG_NULL_CONCAT_ARG_AT_POS_0);
Objects.requireNonNull(rest, ConsUtil.MSG_ARG_ARRAY_REST_IS_NULL);
if (rest.length == 0) {
return first;
}
IntConsList<Integer> result = rest[rest.length - 1];
if (result == null) {
throw new NullPointerException(ConsUtil.MSG_NULL_CONCAT_ARG_AT_POS + rest.length);
}
for (int i = rest.length - 2; i >= -1; i--) {
IntConsList<Integer> cons;
if (i == -1) {
cons = first;
} else {
if (rest[i] == null) {
throw new NullPointerException(ConsUtil.MSG_NULL_CONCAT_ARG_AT_POS + (i + 1));
}
cons = rest[i].intReverse();
}
while (cons != Nil.INSTANCE) {
result = new IntConsListImpl(cons.intHead(), result);
cons = cons.intTail();
}
}
return result;
}
/**
* Constructs a new <tt>LongConsList</tt> with elements <tt>head</tt> and <tt>tail</tt>.
*
* <p>The head, the tail elements and the resulting list have the same compile-time type.
*
* <p>The original list is not modified.
*
* @param head first element of the new list
* @param tail <tt>ConsList</tt> with the second and consecutive elements of the new list
* @return a cons list with the given head and tail elements
*/
@NonNull
static LongConsList<Long> longCons(long head, @NonNull LongConsList<Long> tail) {
return new LongConsListImpl(head, Objects.requireNonNull(tail, ConsUtil.MSG_TAIL_IS_NULL));
}
/**
* Constructs a new LongConsList with elements <tt>head</tt> and <tt>tail</tt>.
*
* <p>The head, the tail elements and the resulting list have the same compile-time type.
*
* <p><tt>tail</tt> collection, if not of type <tt>ConsList</tt>, will be converted to it.
*
* <p>The original list is not modified.
*
* @param head first element of the new list
* @param tail collection of second and consecutive elements of the new list;
* @return a cons list with the given head and tail elements
*/
@NonNull
static LongConsList<Long> longCons(long head, @NonNull Collection<Long> tail) {
return new LongConsListImpl(head, longConsList(tail));
}
/**
* Constructs a new <tt>LongConsList</tt> containing elements in the given order.
*
* <p>An invocation with an empty parameter list will produce the empty
* list instance <tt>nil()</tt>.
*
* <p>With a non-empty parameter array, its first element is the head of the new cons list,
* the rest are the parameters for constructing its tail.
*
* @param elements any number of elements of the list to be constructed
* @return the cons list with the elements consList the argument array in the same order
*/
@NonNull
static LongConsList<Long> longList(@NonNull long... elements) {
LongConsList<Long> cons = nil();
for (int i = elements.length - 1; i >= 0; i--) {
cons = new LongConsListImpl(elements[i], cons);
}
return cons;
}
/**
* Constructs a new <tt>LongConsList</tt> from the given {@link Iterable}.
*
* <p>If the {@link Iterable} is itself a ConsList, returns the typecast iterable.
*
* <p>For instances of {@link List}, only one iteration backwards through the list's
* {@link ListIterator} will be done.
*
* <p>For all other {@link Iterable} types, this traverses both the Iterable
* and the intermediate ConsList once.
*
* @param iterable input iterable
* @return the cons list with the elements consList the Iterable in the same order
*/
@NonNull
static LongConsList<Long> longConsList(@NonNull Iterable<Long> iterable) {
Objects.requireNonNull(iterable, ConsUtil.MSG_ITERABLE_IS_NULL);
if (iterable instanceof LongConsList) {
return (LongConsList<Long>) iterable;
}
LongConsList<Long> cons = nil();
if (iterable instanceof List) {
List<Long> list = (List<Long>) iterable;
ListIterator<Long> iter = list.listIterator(list.size());
while (iter.hasPrevious()) {
cons = new LongConsListImpl(iter.previous(), cons);
}
return cons;
} else {
for (Long v : iterable) {
cons = new LongConsListImpl(v, cons);
}
return cons.longReverse();
}
}
/**
* Returns a <tt>LongConsList</tt> that contains the concatenation of elements of all argument LongConsLists.
*
* @param first the first argument cons list, not nullable
* @param rest the rest of the argument cons lists, each of them not nullable
* @return the concatentation of all argument lists
*/
@NonNull
@SafeVarargs
static LongConsList<Long> concat(@NonNull LongConsList<Long> first, @NonNull LongConsList<Long>... rest) {
Objects.requireNonNull(first, ConsUtil.MSG_NULL_CONCAT_ARG_AT_POS_0);
Objects.requireNonNull(rest, ConsUtil.MSG_ARG_ARRAY_REST_IS_NULL);
if (rest.length == 0) {
return first;
}
LongConsList<Long> result = rest[rest.length - 1];
if (result == null) {
throw new NullPointerException(ConsUtil.MSG_NULL_CONCAT_ARG_AT_POS + rest.length);
}
for (int i = rest.length - 2; i >= -1; i--) {
LongConsList<Long> cons;
if (i == -1) {
cons = first;
} else {
if (rest[i] == null) {
throw new NullPointerException(ConsUtil.MSG_NULL_CONCAT_ARG_AT_POS + (i + 1));
}
cons = rest[i].longReverse();
}
while (cons != Nil.INSTANCE) {
result = new LongConsListImpl(cons.longHead(), result);
cons = cons.longTail();
}
}
return result;
}
/**
* Constructs a new <tt>DoubleConsList</tt> with elements <tt>head</tt> and <tt>tail</tt>.
*
* <p>The head, the tail elements and the resulting list have the same compile-time type.
*
* <p>The original list is not modified.
*
* @param head first element of the new list
* @param tail <tt>ConsList</tt> with the second and consecutive elements of the new list
* @return a cons list with the given head and tail elements
*/
@NonNull
static DoubleConsList<Double> doubleCons(double head, @NonNull DoubleConsList<Double> tail) {
return new DoubleConsListImpl(head, Objects.requireNonNull(tail, ConsUtil.MSG_TAIL_IS_NULL));
}
/**
* Constructs a new <tt>DoubleConsList</tt> with elements <tt>head</tt> and <tt>tail</tt>.
*
* <p>The head, the tail elements and the resulting list have the same compile-time type.
*
* <p><tt>tail</tt> collection, if not of type <tt>ConsList</tt>, will be converted to it.
*
* <p>The original list is not modified.
*
* @param head first element of the new list
* @param tail collection of second and consecutive elements of the new list;
* @return a cons list with the given head and tail elements
*/
@NonNull
static DoubleConsList<Double> doubleCons(double head, @NonNull Collection<Double> tail) {
return new DoubleConsListImpl(head, doubleConsList(tail));
}
/**
* Constructs a new <tt>DoubleConsList</tt> containing elements in the given order.
*
* <p>An invocation with an empty parameter list will produce the empty
* list instance <tt>nil()</tt>.
*
* <p>With a non-empty parameter array, its first element is the head of the new cons list,
* the rest are the parameters for constructing its tail.
*
* @param elements any number of elements of the list to be constructed
* @return the cons list with the elements consList the argument array in the same order
*/
@NonNull
static DoubleConsList<Double> doubleList(@NonNull double... elements) {
DoubleConsList<Double> cons = nil();
for (int i = elements.length - 1; i >= 0; i--) {
cons = new DoubleConsListImpl(elements[i], cons);
}
return cons;
}
/**
* Constructs a new <tt>DoubleConsList</tt> from the given {@link Iterable}.
*
* <p>If the {@link Iterable} is itself a ConsList, returns the typecast iterable.
*
* <p>For instances of {@link List}, only one iteration backwards through the list's
* {@link ListIterator} will be done.
*
* <p>For all other {@link Iterable} types, this traverses both the Iterable
* and the intermediate ConsList once.
*
* @param iterable input iterable
* @return the cons list with the elements consList the Iterable in the same order
*/
@NonNull
static DoubleConsList<Double> doubleConsList(@NonNull Iterable<Double> iterable) {
Objects.requireNonNull(iterable, ConsUtil.MSG_ITERABLE_IS_NULL);
if (iterable instanceof DoubleConsList) {
return (DoubleConsList<Double>) iterable;
}
DoubleConsList<Double> cons = nil();
if (iterable instanceof List) {
List<Double> list = (List<Double>) iterable;
ListIterator<Double> iter = list.listIterator(list.size());
while (iter.hasPrevious()) {
cons = new DoubleConsListImpl(iter.previous(), cons);
}
return cons;
} else {
for (Double v : iterable) {
cons = new DoubleConsListImpl(v, cons);
}
return cons.doubleReverse();
}
}
/**
* Returns a <tt>DoubleConsList</tt> that contains the concatenation of elements of all argument DoubleConsLists.
*
* @param first the first argument cons list, not nullable
* @param rest the rest of the argument cons lists, each of them not nullable
* @return the concatentation of all argument lists
*/
@NonNull
@SafeVarargs
static DoubleConsList<Double> concat(@NonNull DoubleConsList<Double> first, @NonNull DoubleConsList<Double>... rest) {
Objects.requireNonNull(first, "Null concat argument at position 0");
Objects.requireNonNull(rest, ConsUtil.MSG_ARG_ARRAY_REST_IS_NULL);
if (rest.length == 0) {
return first;
}
DoubleConsList<Double> result = rest[rest.length - 1];
if (result == null) {
throw new NullPointerException(ConsUtil.MSG_NULL_CONCAT_ARG_AT_POS + rest.length);
}
for (int i = rest.length - 2; i >= -1; i--) {
DoubleConsList<Double> cons;
if (i == -1) {
cons = first;
} else {
if (rest[i] == null) {
throw new NullPointerException(ConsUtil.MSG_NULL_CONCAT_ARG_AT_POS + (i + 1));
}
cons = rest[i].doubleReverse();
}
while (cons != Nil.INSTANCE) {
result = new DoubleConsListImpl(cons.doubleHead(), result);
cons = cons.doubleTail();
}
}
return result;
}
/**
* Returns the first element of the <tt>ConsList</tt>
*
* <p>Throws {@link NoSuchElementException} if the list is empty.
*
* @return first element of the list
*/
E head();
/**
* Returns another <tt>ConsList</tt> containing the current list's elements after the first one.
*
* <p>Throws {@link NoSuchElementException} if the list is empty.
*
* @return elements of the list after the first one
*/
@NonNull
ConsList<E> tail();
/**
* Constructs a new <tt>ConsList</tt> with the elements of the current one in the reverse order.
*
* @return a new list with elements in reversed order
*/
@NonNull
ConsList<E> reverse();
}