-
Notifications
You must be signed in to change notification settings - Fork 57
/
Util.java
823 lines (752 loc) · 34.2 KB
/
Util.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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
package com.redhat.ceylon.compiler.java;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import ceylon.language.ArraySequence;
import ceylon.language.Iterable;
import ceylon.language.Iterator;
import ceylon.language.Null;
import ceylon.language.Ranged;
import ceylon.language.Sequential;
import ceylon.language.empty_;
import ceylon.language.finished_;
import com.redhat.ceylon.cmr.api.ArtifactResult;
import com.redhat.ceylon.cmr.api.Logger;
import com.redhat.ceylon.cmr.api.RepositoryManager;
import com.redhat.ceylon.cmr.api.RepositoryManagerBuilder;
import com.redhat.ceylon.compiler.java.metadata.Ceylon;
import com.redhat.ceylon.compiler.java.metadata.Class;
import com.redhat.ceylon.compiler.java.metadata.SatisfiedTypes;
import com.redhat.ceylon.compiler.java.runtime.model.ReifiedType;
import com.redhat.ceylon.compiler.java.runtime.model.RuntimeModuleManager;
import com.redhat.ceylon.compiler.java.runtime.model.TypeDescriptor;
import com.redhat.ceylon.compiler.loader.impl.reflect.mirror.ReflectionClass;
import com.redhat.ceylon.compiler.loader.model.LazyClass;
import com.redhat.ceylon.compiler.loader.model.LazyInterface;
import com.redhat.ceylon.compiler.typechecker.context.Context;
import com.redhat.ceylon.compiler.typechecker.io.VFS;
import com.redhat.ceylon.compiler.typechecker.model.ProducedType;
import com.redhat.ceylon.compiler.typechecker.model.TypeDeclaration;
/**
* Helper class for generated Ceylon code that needs to call implementation logic.
*
* @author Stéphane Épardaud <stef@epardaud.fr>
*/
public class Util {
private static RuntimeModuleManager moduleManager;
static{
resetModuleManager();
}
public static void loadModule(String name, String version, ArtifactResult result, ClassLoader classLoader){
moduleManager.loadModule(name, version, result, classLoader);
}
public static void resetModuleManager() {
RepositoryManagerBuilder builder = new RepositoryManagerBuilder(new Logger(){
@Override
public void error(String str) {
System.err.println("ERROR: "+str);
}
@Override
public void warning(String str) {
System.err.println("WARN: "+str);
}
@Override
public void info(String str) {
System.err.println("INFO: "+str);
}
@Override
public void debug(String str) {
System.err.println("DEBUG: "+str);
}
});
RepositoryManager repoManager = builder.buildRepository();
VFS vfs = new VFS();
Context context = new Context(repoManager, vfs);
moduleManager = new RuntimeModuleManager(context);
moduleManager.initCoreModules();
moduleManager.prepareForTypeChecking();
}
public static String declClassName(String name) {
return name.replace("::", ".");
}
public static TypeDescriptor getTypeDescriptor(Object instance) {
if(instance == null)
return Null.$TypeDescriptor;
else if(instance instanceof ReifiedType)
return((ReifiedType) instance).$getType();
else
return null; // FIXME: interop?
}
public static boolean isReified(java.lang.Object o, TypeDescriptor type){
TypeDescriptor instanceType = getTypeDescriptor(o);
if(instanceType == null)
return false; // FIXME: interop?
return instanceType.toProducedType(moduleManager).isSubtypeOf(type.toProducedType(moduleManager));
}
public static ProducedType getProducedType(Object instance) {
TypeDescriptor instanceType = getTypeDescriptor(instance);
if(instanceType == null)
throw new RuntimeException("Metamodel not yet supported for Java types");
return instanceType.toProducedType(moduleManager);
}
public static ceylon.language.metamodel.ProducedType getMetamodel(TypeDescriptor typeDescriptor) {
if(typeDescriptor == null)
throw new RuntimeException("Metamodel not yet supported for Java types");
ProducedType pt = typeDescriptor.toProducedType(moduleManager);
return getMetamodel(pt);
}
// FIXME: this will need better thinking in terms of memory usage
private static Map<com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface, com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface> typeCheckModelToRuntimeModel
= new HashMap<com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface, com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface>();
public static com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface getOrCreateMetamodel(com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface declaration){
synchronized(typeCheckModelToRuntimeModel){
com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface ret = typeCheckModelToRuntimeModel.get(declaration);
if(ret == null){
if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Class){
ret = new com.redhat.ceylon.compiler.java.runtime.metamodel.Class((com.redhat.ceylon.compiler.typechecker.model.Class)declaration);
}else if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Interface){
ret = new com.redhat.ceylon.compiler.java.runtime.metamodel.Interface((com.redhat.ceylon.compiler.typechecker.model.Interface)declaration);
}
typeCheckModelToRuntimeModel.put(declaration, ret);
}
return ret;
}
}
public static ceylon.language.metamodel.ProducedType getMetamodel(ProducedType pt) {
TypeDeclaration declaration = pt.getDeclaration();
if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Class){
return new com.redhat.ceylon.compiler.java.runtime.metamodel.ClassType(pt);
}
if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Interface){
return new com.redhat.ceylon.compiler.java.runtime.metamodel.InterfaceType(pt);
}
throw new RuntimeException("Declaration type not supported yet: "+declaration);
}
public static TypeDescriptor getTypeDescriptorForDeclaration(com.redhat.ceylon.compiler.typechecker.model.Declaration declaration) {
if(declaration instanceof LazyClass){
ReflectionClass classMirror = (ReflectionClass) ((LazyClass) declaration).classMirror;
return TypeDescriptor.klass(classMirror.klass);
}
if(declaration instanceof LazyInterface){
ReflectionClass classMirror = (ReflectionClass) ((LazyInterface) declaration).classMirror;
return TypeDescriptor.klass(classMirror.klass);
}
throw new RuntimeException("Unsupported declaration type: " + declaration);
}
/**
* Returns true if the given object satisfies ceylon.language.Identifiable
*/
public static boolean isIdentifiable(java.lang.Object o){
return satisfiesInterface(o, "ceylon.language.Identifiable");
}
/**
* Returns true if the given object extends ceylon.language.Basic
*/
public static boolean isBasic(java.lang.Object o){
return extendsClass(o, "ceylon.language.Basic");
}
/**
* Returns true if the given object extends the given class
*/
public static boolean extendsClass(java.lang.Object o, String className) {
if(o == null)
return false;
if(className == null)
throw new IllegalArgumentException("Type name cannot be null");
return classExtendsClass(o.getClass(), className);
}
private static boolean classExtendsClass(java.lang.Class<?> klass, String className) {
if(klass == null)
return false;
if (klass.getName().equals(className))
return true;
if ((className.equals("ceylon.language.Basic"))
&& klass!=java.lang.Object.class
&& !isSubclassOfString(klass)
//&& klass!=java.lang.String.class
&& !klass.isAnnotationPresent(Class.class)
&& (!klass.isInterface() || !klass.isAnnotationPresent(Ceylon.class))) {
//TODO: this is broken for a Java class that
// extends a Ceylon class
return true;
}
Class classAnnotation = klass.getAnnotation(Class.class);
if (classAnnotation != null) {
String superclassName = declClassName(classAnnotation.extendsType());
int i = superclassName.indexOf('<');
if (i>0) {
superclassName = superclassName.substring(0, i);
}
if (superclassName.isEmpty()) {
return false;
}
try {
return classExtendsClass(
java.lang.Class.forName(superclassName, true, klass.getClassLoader()),
className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
return classExtendsClass(klass.getSuperclass(), className);
}
/**
* Returns true if the given object satisfies the given interface
*/
public static boolean satisfiesInterface(java.lang.Object o, String className){
if(o == null)
return false;
if(className == null)
throw new IllegalArgumentException("Type name cannot be null");
// we use a hash set to speed things up for interfaces, to avoid looking at them twice
Set<java.lang.Class<?>> alreadyVisited = new HashSet<java.lang.Class<?>>();
return classSatisfiesInterface(o.getClass(), className, alreadyVisited);
}
private static boolean classSatisfiesInterface(java.lang.Class<?> klass, String className,
Set<java.lang.Class<?>> alreadyVisited) {
if(klass == null
|| klass == ceylon.language.Anything.class)
return false;
if ((className.equals("ceylon.language.Identifiable"))
&& klass!=java.lang.Object.class
//&& klass!=java.lang.String.class
&& !klass.isAnnotationPresent(Ceylon.class)) {
//TODO: this is broken for a Java class that
// extends a Ceylon class
return true;
}
if (className.equals("ceylon.language.Identifiable")
&& isSubclassOfString(klass)) {
return false;
}
// try the interfaces
if(lookForInterface(klass, className, alreadyVisited))
return true;
// try its superclass
Class classAnnotation = klass.getAnnotation(Class.class);
String superclassName;
if (classAnnotation!=null) {
superclassName = declClassName(classAnnotation.extendsType());
int i = superclassName.indexOf('<');
if (i>0) {
superclassName = superclassName.substring(0, i);
}
} else {
// Maybe the class didn't have an extends, so implictly Basic
superclassName = "ceylon.language.Basic";
}
if (!superclassName.isEmpty()) {
try {
return classSatisfiesInterface(
java.lang.Class.forName(superclassName, true, klass.getClassLoader()),
className, alreadyVisited);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
return classSatisfiesInterface(klass.getSuperclass(), className, alreadyVisited);
}
private static boolean isSubclassOfString(java.lang.Class<?> klass) {
return klass.getName().equals("com.redhat.ceylon.compiler.java.language.EmptyString") || klass.getName().equals("com.redhat.ceylon.compiler.java.language.SequenceString");
}
private static boolean lookForInterface(java.lang.Class<?> klass, String className,
Set<java.lang.Class<?>> alreadyVisited){
if (klass.getName().equals(className))
return true;
// did we already visit this type?
if(!alreadyVisited.add(klass))
return false;
// first see if it satisfies it directly
SatisfiedTypes satisfiesAnnotation = klass.getAnnotation(SatisfiedTypes.class);
if (satisfiesAnnotation!=null){
for (String satisfiedType : satisfiesAnnotation.value()){
satisfiedType = declClassName(satisfiedType);
int i = satisfiedType.indexOf('<');
if (i>0) {
satisfiedType = satisfiedType.substring(0, i);
}
try {
if (lookForInterface(
java.lang.Class.forName(satisfiedType, true, klass.getClassLoader()),
className, alreadyVisited)) {
return true;
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
// now look at this class's interfaces
for (java.lang.Class<?> intrface : klass.getInterfaces()){
if (lookForInterface(intrface, className, alreadyVisited))
return true;
}
// no luck
return false;
}
//
// Java variadic conversions
@SuppressWarnings("unchecked")
public static <T> List<T> collectIterable(Iterable<? extends T, ? extends java.lang.Object> sequence) {
List<T> list = new LinkedList<T>();
if (sequence != null) {
Iterator<? extends T> iterator = sequence.iterator();
Object o;
while((o = iterator.next()) != finished_.getFinished$()){
list.add((T)o);
}
}
return list;
}
public static boolean[] toBooleanArray(ceylon.language.Iterable<? extends ceylon.language.Boolean, ? extends java.lang.Object> sequence,
boolean... initialElements){
if(sequence instanceof ceylon.language.List)
return toBooleanArray((ceylon.language.List<? extends ceylon.language.Boolean>)sequence, initialElements);
List<ceylon.language.Boolean> list = collectIterable(sequence);
boolean[] ret = new boolean[list.size() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = initialElements[i];
}
for(ceylon.language.Boolean e : list){
ret[i++] = e.booleanValue();
}
return ret;
}
public static boolean[] toBooleanArray(ceylon.language.List<? extends ceylon.language.Boolean> sequence,
boolean... initialElements){
boolean[] ret = new boolean[(int) sequence.getSize() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = initialElements[i];
}
while(!sequence.getEmpty()){
ret[i++] = sequence.getFirst().booleanValue();
sequence = (ceylon.language.List<? extends ceylon.language.Boolean>)sequence.getRest();
}
return ret;
}
public static byte[] toByteArray(ceylon.language.Iterable<? extends ceylon.language.Integer, ? extends java.lang.Object> sequence,
long... initialElements){
if(sequence instanceof ceylon.language.List)
return toByteArray((ceylon.language.List<? extends ceylon.language.Integer>)sequence, initialElements);
List<ceylon.language.Integer> list = collectIterable(sequence);
byte[] ret = new byte[initialElements.length + list.size()];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = (byte) initialElements[i];
}
for(ceylon.language.Integer e : list){
ret[i++] = (byte)e.longValue();
}
return ret;
}
public static byte[] toByteArray(ceylon.language.List<? extends ceylon.language.Integer> sequence,
long... initialElements){
byte[] ret = new byte[initialElements.length + (int) sequence.getSize()];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = (byte) initialElements[i];
}
while(!sequence.getEmpty()){
ret[i++] = (byte) sequence.getFirst().longValue();
sequence = (ceylon.language.List<? extends ceylon.language.Integer>)sequence.getRest();
}
return ret;
}
public static short[] toShortArray(ceylon.language.Iterable<? extends ceylon.language.Integer, ? extends java.lang.Object> sequence,
long... initialElements){
if(sequence instanceof ceylon.language.List)
return toShortArray((ceylon.language.List<? extends ceylon.language.Integer>)sequence, initialElements);
List<ceylon.language.Integer> list = collectIterable(sequence);
short[] ret = new short[list.size() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = (short) initialElements[i];
}
for(ceylon.language.Integer e : list){
ret[i++] = (short)e.longValue();
}
return ret;
}
public static short[] toShortArray(ceylon.language.List<? extends ceylon.language.Integer> sequence,
long... initialElements){
short[] ret = new short[(int) sequence.getSize() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = (short) initialElements[i];
}
while(!sequence.getEmpty()){
ret[i++] = (short) sequence.getFirst().longValue();
sequence = (ceylon.language.List<? extends ceylon.language.Integer>)sequence.getRest();
}
return ret;
}
public static int[] toIntArray(ceylon.language.Iterable<? extends ceylon.language.Integer, ? extends java.lang.Object> sequence,
long... initialElements){
if(sequence instanceof ceylon.language.List)
return toIntArray((ceylon.language.List<? extends ceylon.language.Integer>)sequence, initialElements);
List<ceylon.language.Integer> list = collectIterable(sequence);
int[] ret = new int[list.size() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = (int) initialElements[i];
}
for(ceylon.language.Integer e : list){
ret[i++] = (int)e.longValue();
}
return ret;
}
public static int[] toIntArray(ceylon.language.List<? extends ceylon.language.Integer> sequence,
long... initialElements){
int[] ret = new int[(int) sequence.getSize() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = (int) initialElements[i];
}
while(!sequence.getEmpty()){
ret[i++] = (int) sequence.getFirst().longValue();
sequence = (ceylon.language.List<? extends ceylon.language.Integer>)sequence.getRest();
}
return ret;
}
public static long[] toLongArray(ceylon.language.Iterable<? extends ceylon.language.Integer, ? extends java.lang.Object> sequence,
long... initialElements){
if(sequence instanceof ceylon.language.List)
return toLongArray((ceylon.language.List<? extends ceylon.language.Integer>)sequence, initialElements);
List<ceylon.language.Integer> list = collectIterable(sequence);
long[] ret = new long[list.size() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = initialElements[i];
}
for(ceylon.language.Integer e : list){
ret[i++] = e.longValue();
}
return ret;
}
public static long[] toLongArray(ceylon.language.List<? extends ceylon.language.Integer> sequence,
long... initialElements){
long[] ret = new long[(int) sequence.getSize() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = initialElements[i];
}
while(!sequence.getEmpty()){
ret[i++] = sequence.getFirst().longValue();
sequence = (ceylon.language.List<? extends ceylon.language.Integer>)sequence.getRest();
}
return ret;
}
public static float[] toFloatArray(ceylon.language.Iterable<? extends ceylon.language.Float, ? extends java.lang.Object> sequence,
double... initialElements){
if(sequence instanceof ceylon.language.List)
return toFloatArray((ceylon.language.List<? extends ceylon.language.Float>)sequence, initialElements);
List<ceylon.language.Float> list = collectIterable(sequence);
float[] ret = new float[initialElements.length + list.size()];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = (float) initialElements[i];
}
for(ceylon.language.Float e : list){
ret[i++] = (float)e.doubleValue();
}
return ret;
}
public static float[] toFloatArray(ceylon.language.List<? extends ceylon.language.Float> sequence,
double... initialElements){
float[] ret = new float[initialElements.length + (int) sequence.getSize()];
int i = 0;
for(;i<initialElements.length;i++){
ret[i] = (float) initialElements[i];
}
while(!sequence.getEmpty()){
ret[i++] = (float) sequence.getFirst().doubleValue();
sequence = (ceylon.language.List<? extends ceylon.language.Float>)sequence.getRest();
}
return ret;
}
public static double[] toDoubleArray(ceylon.language.Iterable<? extends ceylon.language.Float, ? extends java.lang.Object> sequence,
double... initialElements){
if(sequence instanceof ceylon.language.List)
return toDoubleArray((ceylon.language.List<? extends ceylon.language.Float>)sequence, initialElements);
List<ceylon.language.Float> list = collectIterable(sequence);
double[] ret = new double[list.size() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = initialElements[i];
}
for(ceylon.language.Float e : list){
ret[i++] = e.doubleValue();
}
return ret;
}
public static double[] toDoubleArray(ceylon.language.List<? extends ceylon.language.Float> sequence,
double... initialElements){
double[] ret = new double[(int) sequence.getSize() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = initialElements[i];
}
while(!sequence.getEmpty()){
ret[i++] = sequence.getFirst().doubleValue();
sequence = (ceylon.language.List<? extends ceylon.language.Float>)sequence.getRest();
}
return ret;
}
public static char[] toCharArray(ceylon.language.Iterable<? extends ceylon.language.Character, ? extends java.lang.Object> sequence,
int... initialElements){
if(sequence instanceof ceylon.language.List)
return toCharArray((ceylon.language.List<? extends ceylon.language.Character>)sequence, initialElements);
List<ceylon.language.Character> list = collectIterable(sequence);
char[] ret = new char[list.size() + initialElements.length];
int i=0;
// FIXME: this is invalid and should yield a larger array by splitting chars > 16 bits in two
for(;i<initialElements.length;i++){
ret[i] = (char) initialElements[i];
}
for(ceylon.language.Character e : list){
ret[i++] = (char)e.intValue();
}
return ret;
}
public static char[] toCharArray(ceylon.language.List<? extends ceylon.language.Character> sequence,
int... initialElements){
char[] ret = new char[(int) sequence.getSize() + initialElements.length];
int i=0;
// FIXME: this is invalid and should yield a larger array by splitting chars > 16 bits in two
for(;i<initialElements.length;i++){
ret[i] = (char) initialElements[i];
}
while(!sequence.getEmpty()){
ret[i++] = (char) sequence.getFirst().intValue();
sequence = (ceylon.language.List<? extends ceylon.language.Character>)sequence.getRest();
}
return ret;
}
public static int[] toCodepointArray(ceylon.language.Iterable<? extends ceylon.language.Character, ? extends java.lang.Object> sequence,
int... initialElements){
if(sequence instanceof ceylon.language.List)
return toCodepointArray((ceylon.language.List<? extends ceylon.language.Character>)sequence, initialElements);
List<ceylon.language.Character> list = collectIterable(sequence);
int[] ret = new int[list.size() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = initialElements[i];
}
for(ceylon.language.Character e : list){
ret[i++] = e.intValue();
}
return ret;
}
public static int[] toCodepointArray(ceylon.language.List<? extends ceylon.language.Character> sequence,
int... initialElements){
int[] ret = new int[(int) sequence.getSize() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = initialElements[i];
}
while(!sequence.getEmpty()){
ret[i++] = sequence.getFirst().intValue();
sequence = (ceylon.language.List<? extends ceylon.language.Character>)sequence.getRest();
}
return ret;
}
public static java.lang.String[] toJavaStringArray(ceylon.language.Iterable<? extends ceylon.language.String, ? extends java.lang.Object> sequence,
java.lang.String... initialElements){
if(sequence instanceof ceylon.language.List)
return toJavaStringArray((ceylon.language.List<? extends ceylon.language.String>)sequence, initialElements);
List<ceylon.language.String> list = collectIterable(sequence);
java.lang.String[] ret = new java.lang.String[list.size() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = initialElements[i];
}
for(ceylon.language.String e : list){
ret[i++] = e.toString();
}
return ret;
}
public static java.lang.String[] toJavaStringArray(ceylon.language.List<? extends ceylon.language.String> sequence,
java.lang.String... initialElements){
java.lang.String[] ret = new java.lang.String[(int) sequence.getSize() + initialElements.length];
int i=0;
for(;i<initialElements.length;i++){
ret[i] = initialElements[i];
}
while(!sequence.getEmpty()){
ret[i++] = sequence.getFirst().toString();
sequence = (ceylon.language.List<? extends ceylon.language.String>)sequence.getRest();
}
return ret;
}
public static <T> T[] toArray(ceylon.language.List<? extends T> sequence,
T[] ret, T... initialElements){
System.arraycopy(initialElements, 0, ret, 0, initialElements.length);
int i=initialElements.length;
while(!sequence.getEmpty()){
ret[i++] = sequence.getFirst();
sequence = (ceylon.language.List<? extends T>)sequence.getRest();
}
return ret;
}
public static <T> T[] toArray(ceylon.language.Iterable<? extends T, ? extends java.lang.Object> iterable,
java.lang.Class<T> klass, T... initialElements){
List<T> list = collectIterable(iterable);
@SuppressWarnings("unchecked")
T[] ret = (T[]) java.lang.reflect.Array.newInstance(klass, list.size() + initialElements.length);
// fast path
if(initialElements.length == 0){
// fast copy of list
list.toArray(ret);
}else{
// fast copy of initialElements
System.arraycopy(initialElements, 0, ret, 0, initialElements.length);
// slow iteration for list :(
int i = initialElements.length;
for(T o : list)
ret[i++] = o;
}
return ret;
}
public static <T> T checkNull(T t){
if(t == null)
throw new NullPointerException();
return t;
}
/**
* Return {@link empty_#getEmpty$ empty} or an {@link ArraySequence}
* wrapping the given elements, depending on whether the given array is
* empty
* @param elements The elements
* @return A Sequential
*/
public static <T> Sequential<T> sequentialInstance(TypeDescriptor $reifiedT, T[] elements) {
if (elements.length == 0) {
return (Sequential)empty_.getEmpty$();
}
return ArraySequence.<T>instance($reifiedT, elements);
}
/**
* Return {@link empty_#getEmpty$ empty} or an {@link ArraySequence}
* wrapping the given elements, depending on whether the given array and varargs are
* empty
* @param rest The elements at the end of the sequence
* @param elements the elements at the start of the sequence
* @return A Sequential
*/
public static <T> Sequential<? extends T> sequentialInstance(TypeDescriptor $reifiedT, Sequential<? extends T> rest, T... elements) {
if (elements.length == 0){
if(rest.getEmpty()) {
return (Sequential)empty_.getEmpty$();
}
return rest;
}
// elements is not empty
if(rest.getEmpty())
return ArraySequence.<T>instance($reifiedT, elements);
// we have both, let's find the total size
int total = (int) (rest.getSize() + elements.length);
java.lang.Object[] newArray = new java.lang.Object[total];
System.arraycopy(elements, 0, newArray, 0, elements.length);
Iterator<? extends T> iterator = rest.iterator();
int i = elements.length;
for(Object elem; (elem = iterator.next()) != finished_.getFinished$(); i++){
newArray[i] = elem;
}
return ArraySequence.<T>instance($reifiedT, newArray);
}
/**
* Method for instantiating a Range (or Empty) from a Tree.SpreadOp,
* {@code start:length}.
* @param start The start
* @param length The size of the Range to create
* @return A range
*/
public static <T extends ceylon.language.Ordinal<? extends T>> Sequential<T> spreadOp(TypeDescriptor $reifiedT, T start, long length) {
if (length <= 0) {
return (Sequential)empty_.getEmpty$();
}
if (start instanceof ceylon.language.Integer) {
ceylon.language.Integer startInt = (ceylon.language.Integer)start;
return new ceylon.language.Range($reifiedT, startInt,
ceylon.language.Integer.instance(startInt.longValue() + (length - 1)));
} else if (start instanceof ceylon.language.Character) {
ceylon.language.Character startChar = (ceylon.language.Character)start;
return new ceylon.language.Range($reifiedT, startChar,
ceylon.language.Character.instance((int)(startChar.intValue() + length - 1)));
} else {
T end = start;
long ii = 0L;
while (++ii < length) {
end = end.getSuccessor();
}
return new ceylon.language.Range($reifiedT, start, end);
}
}
/**
* Returns a runtime exception. To be used by implementors of mixin methods used to access super-interfaces $impl fields
* for final classes that don't and will never need them
*/
public static RuntimeException makeUnimplementedMixinAccessException() {
return new RuntimeException("Internal error: should never be called");
}
/**
* Specialised version of Tuple.spanFrom for when the typechecker determines that it can do better than the generic one
* that returns a Sequential. Here we return a Tuple, although our type signature hides this.
*/
public static Sequential tuple_spanFrom(Ranged tuple, ceylon.language.Integer index){
Sequential seq = (Sequential)tuple;
long i = index.longValue();
while(i-- > 0){
seq = seq.getRest();
}
return seq;
}
public static boolean[] fillArray(boolean[] array, boolean val){
Arrays.fill(array, val);
return array;
}
public static byte[] fillArray(byte[] array, byte val){
Arrays.fill(array, val);
return array;
}
public static short[] fillArray(short[] array, short val){
Arrays.fill(array, val);
return array;
}
public static int[] fillArray(int[] array, int val){
Arrays.fill(array, val);
return array;
}
public static long[] fillArray(long[] array, long val){
Arrays.fill(array, val);
return array;
}
public static float[] fillArray(float[] array, float val){
Arrays.fill(array, val);
return array;
}
public static double[] fillArray(double[] array, double val){
Arrays.fill(array, val);
return array;
}
public static char[] fillArray(char[] array, char val){
Arrays.fill(array, val);
return array;
}
public static java.lang.Object[] fillArray(java.lang.Object[] array, java.lang.Object val){
Arrays.fill(array, val);
return array;
}
/**
* Returns a runtime exception. To be used by implementors of Java array methods used to make sure they are never called
*/
public static RuntimeException makeJavaArrayWrapperException() {
return new RuntimeException("Internal error: should never be called");
}
}