-
Notifications
You must be signed in to change notification settings - Fork 3
/
CollectionServices.java
2404 lines (2212 loc) · 77.1 KB
/
CollectionServices.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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2015, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.services;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import org.eclipse.acceleo.annotations.api.documentation.Documentation;
import org.eclipse.acceleo.annotations.api.documentation.Example;
import org.eclipse.acceleo.annotations.api.documentation.Other;
import org.eclipse.acceleo.annotations.api.documentation.Param;
import org.eclipse.acceleo.annotations.api.documentation.ServiceProvider;
import org.eclipse.acceleo.annotations.api.documentation.Throw;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IService;
import org.eclipse.acceleo.query.runtime.IValidationResult;
import org.eclipse.acceleo.query.runtime.impl.AbstractServiceProvider;
import org.eclipse.acceleo.query.runtime.impl.JavaMethodService;
import org.eclipse.acceleo.query.runtime.impl.LambdaValue;
import org.eclipse.acceleo.query.runtime.impl.Nothing;
import org.eclipse.acceleo.query.runtime.impl.ValidationServices;
import org.eclipse.acceleo.query.services.collection.AbstractCollectionService;
import org.eclipse.acceleo.query.services.collection.AnyService;
import org.eclipse.acceleo.query.services.collection.BooleanLambdaService;
import org.eclipse.acceleo.query.services.collection.ClosureService;
import org.eclipse.acceleo.query.services.collection.CollectService;
import org.eclipse.acceleo.query.services.collection.FirstArgumentRawCollectionType;
import org.eclipse.acceleo.query.services.collection.FirstCollectionTypeService;
import org.eclipse.acceleo.query.services.collection.IncludingService;
import org.eclipse.acceleo.query.services.collection.InsertAtService;
import org.eclipse.acceleo.query.services.collection.IntersectionService;
import org.eclipse.acceleo.query.services.collection.NumberService;
import org.eclipse.acceleo.query.services.collection.RejectService;
import org.eclipse.acceleo.query.services.collection.ReturnCollectionTypeWithFirstAndSecondArgumentRawCollectionType;
import org.eclipse.acceleo.query.services.collection.ReturnCollectionTypeWithFirstArgumentRawCollectionType;
import org.eclipse.acceleo.query.services.collection.SecondArgumentTypeInFirstArgumentCollectionType;
import org.eclipse.acceleo.query.services.collection.SelectService;
import org.eclipse.acceleo.query.validation.type.ICollectionType;
import org.eclipse.acceleo.query.validation.type.IType;
import org.eclipse.acceleo.query.validation.type.SequenceType;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EClassifier;
//@formatter:off
@ServiceProvider(
value = "Services available for Collections"
)
//@formatter:on
@SuppressWarnings({"checkstyle:javadocmethod", "checkstyle:javadoctype" })
public class CollectionServices extends AbstractServiceProvider {
@Override
protected IService<Method> getService(Method publicMethod, boolean forWorkspace) {
final IService<Method> result;
if ("filter".equals(publicMethod.getName())) {
result = new SecondArgumentTypeInFirstArgumentCollectionType(publicMethod, this, forWorkspace);
} else if ("add".equals(publicMethod.getName()) || "concat".equals(publicMethod.getName()) || "union"
.equals(publicMethod.getName())) {
result = new ReturnCollectionTypeWithFirstAndSecondArgumentRawCollectionType(publicMethod, this,
forWorkspace);
} else if ("asSequence".equals(publicMethod.getName()) || "asSet".equals(publicMethod.getName())
|| "asOrderedSet".equals(publicMethod.getName())) {
result = new ReturnCollectionTypeWithFirstArgumentRawCollectionType(publicMethod, this,
forWorkspace);
} else if ("subOrderedSet".equals(publicMethod.getName()) || "subSequence".equals(publicMethod
.getName())) {
result = new FirstCollectionTypeService(publicMethod, this, forWorkspace);
} else if ("drop".equals(publicMethod.getName()) || "dropRight".equals(publicMethod.getName())) {
result = new FirstCollectionTypeService(publicMethod, this, forWorkspace);
} else if ("first".equals(publicMethod.getName()) || "at".equals(publicMethod.getName()) || "last"
.equals(publicMethod.getName())) {
result = new FirstArgumentRawCollectionType(publicMethod, this, forWorkspace);
} else if ("excluding".equals(publicMethod.getName()) || "sub".equals(publicMethod.getName())
|| "reverse".equals(publicMethod.getName())) {
result = new FirstCollectionTypeService(publicMethod, this, forWorkspace);
} else if ("sortedBy".equals(publicMethod.getName())) {
result = new FirstCollectionTypeService(publicMethod, this, forWorkspace);
} else if ("reject".equals(publicMethod.getName())) {
result = new RejectService(publicMethod, this, forWorkspace);
} else if ("select".equals(publicMethod.getName())) {
result = new SelectService(publicMethod, this, forWorkspace);
} else if ("collect".equals(publicMethod.getName())) {
result = new CollectService(publicMethod, this, forWorkspace);
} else if ("closure".equals(publicMethod.getName())) {
result = new ClosureService(publicMethod, this, forWorkspace);
} else if ("including".equals(publicMethod.getName()) || "prepend".equals(publicMethod.getName())
|| "append".equals(publicMethod.getName())) {
result = new IncludingService(publicMethod, this, forWorkspace);
} else if ("sep".equals(publicMethod.getName())) {
if (publicMethod.getParameterTypes().length == 2) {
result = new JavaMethodService(publicMethod, this, forWorkspace) {
@Override
public Set<IType> getType(Call call, ValidationServices services,
IValidationResult validationResult, IReadOnlyQueryEnvironment queryEnvironment,
List<IType> argTypes) {
final Set<IType> result = new LinkedHashSet<IType>();
result.add(new SequenceType(queryEnvironment, ((ICollectionType)argTypes.get(0))
.getCollectionType()));
result.add(new SequenceType(queryEnvironment, argTypes.get(1)));
return result;
}
};
} else if (publicMethod.getParameterTypes().length == 4) {
result = new JavaMethodService(publicMethod, this, forWorkspace) {
@Override
public Set<IType> getType(Call call, ValidationServices services,
IValidationResult validationResult, IReadOnlyQueryEnvironment queryEnvironment,
List<IType> argTypes) {
final Set<IType> result = new LinkedHashSet<IType>();
result.add(new SequenceType(queryEnvironment, ((ICollectionType)argTypes.get(0))
.getCollectionType()));
result.add(new SequenceType(queryEnvironment, argTypes.get(1)));
result.add(new SequenceType(queryEnvironment, argTypes.get(2)));
result.add(new SequenceType(queryEnvironment, argTypes.get(3)));
return result;
}
};
} else {
result = new JavaMethodService(publicMethod, this, forWorkspace);
}
} else if ("any".equals(publicMethod.getName())) {
result = new AnyService(publicMethod, this, forWorkspace);
} else if ("exists".equals(publicMethod.getName()) || "forAll".equals(publicMethod.getName()) || "one"
.equals(publicMethod.getName())) {
result = new BooleanLambdaService(publicMethod, this, forWorkspace);
} else if ("insertAt".equals(publicMethod.getName())) {
result = new InsertAtService(publicMethod, this, forWorkspace);
} else if ("intersection".equals(publicMethod.getName())) {
result = new IntersectionService(publicMethod, this, forWorkspace);
} else if ("sum".equals(publicMethod.getName()) || "min".equals(publicMethod.getName()) || "max"
.equals(publicMethod.getName())) {
result = new NumberService(publicMethod, this, forWorkspace);
} else {
result = new JavaMethodService(publicMethod, this, forWorkspace);
}
return result;
}
// @formatter:off
@SuppressWarnings("unchecked")
@Documentation(
value = "Returns the concatenation of the current sequence with the given collection.",
params = {
@Param(name = "sequence", value = "The first operand"),
@Param(name = "collection", value = "The second operand")
},
result = "The concatenation of the two specified collections.",
examples = {
@Example(
expression = "Sequence{'a', 'b', 'c'}.concat(Sequence{'d', 'e'})", result = "Sequence{'a', 'b', 'c', 'd', 'e'}",
others = {
@Other(
language = Other.ACCELEO_3,
expression = "Sequence{'a', 'b', 'c'}.addAll(Sequence{'d', 'e'})",
result = "Sequence{'a', 'b', 'c', 'd', 'e'}"
)
}
)
}
)
// @formatter:on
public <T> List<T> concat(List<? extends T> sequence, Collection<? extends T> collection) {
if (sequence == null) {
throw new NullPointerException();
}
final List<T> result;
if (collection.isEmpty()) {
result = (List<T>)sequence;
} else {
result = new ArrayList<T>(sequence);
result.addAll(collection);
}
return result;
}
// @formatter:off
@SuppressWarnings("unchecked")
@Documentation(
value = "Returns the concatenation of the current set with the given collection.",
params = {
@Param(name = "set", value = "The first operand"),
@Param(name = "collection", value = "The second operand")
},
result = "The concatenation of the two specified collections.",
examples = {
@Example(
expression = "OrderedSet{'a', 'b', 'c'}.concat(Sequence{'d', 'e'})", result = "OrderedSet{'a', 'b', 'c', 'd', 'e'}",
others = {
@Other(
language = Other.ACCELEO_3,
expression = "OrderedSet{'a', 'b', 'c'}.addAll(Sequence{'d', 'e'})",
result = "OrderedSet{'a', 'b', 'c', 'd', 'e'}"
)
}
)
}
)
// @formatter:on
public <T> Set<T> concat(Set<? extends T> set, Collection<? extends T> collection) {
if (set == null) {
throw new NullPointerException();
}
final Set<T> result;
if (collection.isEmpty()) {
result = (Set<T>)set;
} else {
result = new LinkedHashSet<T>(set);
result.addAll(collection);
}
return result;
}
// @formatter:off
@Documentation(
value = "Returns the concatenation of the given collection into the given sequence.",
params = {
@Param(name = "sequence", value = "The first operand"),
@Param(name = "collection", value = "The second operand")
},
result = "The current sequence including the elements of the given collection.",
examples = {
@Example(
expression = "Sequence{'a', 'b', 'c'}.add(Sequence{'d', 'e'})", result = "Sequence{'a', 'b', 'c', 'd', 'e'}",
others = {
@Other(
language = Other.ACCELEO_3,
expression = "Sequence{'a', 'b', 'c'} + Sequence{'d', 'e'}",
result = "Sequence{'a', 'b', 'c', 'd', 'e'}"
)
}
),
@Example(
expression = "Sequence{'a', 'b', 'c'} + OrderedSet{'c', 'e'}", result = "Sequence{'a', 'b', 'c', 'c', 'e'}",
others = {
@Other(
language = Other.ACCELEO_3,
expression = "Sequence{'a', 'b', 'c'}.addAll(OrderedSet{'c', 'e'})",
result = "Sequence{'a', 'b', 'c', 'c', 'e'}"
)
}
)
},
comment = "The service addAll has been replaced by \"add\" in order to have access to the operator \"+\" between to sequences"
)
// @formatter:on
public <T> List<T> add(List<? extends T> sequence, Collection<? extends T> collection) {
return concat(sequence, collection);
}
// @formatter:off
@Documentation(
value = "Returns the concatenation of the given collection into the current set.",
params = {
@Param(name = "set", value = "The first operand"),
@Param(name = "collection", value = "The second operand")
},
result = "The current set including the elements of the given collection.",
examples = {
@Example(
expression = "OrderedSet{'a', 'b', 'c'} + OrderedSet{'c', 'b', 'f'}", result = "OrderedSet{'a', 'b', 'c', 'f'}",
others = {
@Other(
language = Other.ACCELEO_3,
expression = "OrderedSet{'a', 'b', 'c'}.addAll(OrderedSet{'c', 'b', 'f'})",
result = "OrderedSet{'a', 'b', 'c', 'f'}"
)
}
)
},
comment = "The service addAll has been replaced by \"add\" in order to have access to the operator \"+\" between to sets"
)
// @formatter:on
public <T> Set<T> add(Set<? extends T> set, Collection<? extends T> collection) {
return concat(set, collection);
}
// @formatter:off
@Documentation(
value = "Returns the difference of the current sequence and the given collection.",
params = {
@Param(name = "sequence", value = "The first operand"),
@Param(name = "collection", value = "The second operand")
},
result = "The sequence that contains elements from sequence1 that are not in collection2.",
examples = {
@Example(
expression = "Sequence{'a', 'b', 'c'} - Sequence{'c', 'b', 'f'}", result = "Sequence{'a'}",
others = {
@Other(
language = Other.ACCELEO_3,
expression = "Sequence{'a', 'b', 'c'}.removeAll(Sequence{'c', 'b', 'f'})",
result = "Sequence{'a'}"
)
}
),
@Example(
expression = "Sequence{'a', 'b', 'c'} - OrderedSet{'c', 'b', 'f'}", result = "Sequence{'a'}",
others = {
@Other(
language = Other.ACCELEO_3,
expression = "Sequence{'a', 'b', 'c'}.removeAll(OrderedSet{'c', 'b', 'f'})",
result = "Sequence{'a'}"
)
}
)
},
comment = "The service removeAll has been replaced by \"sub\" in order to have access to the operator \"-\" between to sequences"
)
// @formatter:on
public <T> List<T> sub(List<T> sequence, Collection<?> collection) {
if (sequence == null) {
throw new NullPointerException();
}
if (collection.isEmpty()) {
return sequence;
} else {
List<T> result = new ArrayList<T>(sequence);
result.removeAll(collection);
return result;
}
}
// @formatter:off
@Documentation(
value = "Returns the difference of the current set and the given collection.",
params = {
@Param(name = "set", value = "The first operand"),
@Param(name = "collection", value = "The second operand")
},
result = "The set that contains elements from set1 that are not in collection2.",
examples = {
@Example(
expression = "OrderedSet{'a', 'b', 'c'} - OrderedSet{'c', 'b', 'f'}", result = "OrderedSet{'a'}",
others = {
@Other(
language = Other.ACCELEO_3,
expression = "OrderedSet{'a', 'b', 'c'}.removeAll(OrderedSet{'c', 'b', 'f'})",
result = "OrderedSet{'a'}"
)
}
)
},
comment = "The service removeAll has been replaced by \"sub\" in order to have access to the operator \"-\" between to sets"
)
// @formatter:on
public <T> Set<T> sub(Set<T> set, Collection<?> collection) {
if (set == null) {
throw new NullPointerException();
}
if (collection.isEmpty()) {
return set;
} else {
Set<T> result = new LinkedHashSet<T>(set);
result.removeAll(collection);
return result;
}
}
// @formatter:off
@Documentation(
value = "Select returns a filtered version of the specified sequence. Only elements for which the given \"lambda\" evaluates " +
"to true will be present in the returned sequence.",
params = {
@Param(name = "sequence", value = "The original sequence"),
@Param(name = "lambda", value = "The filtering expression")
},
result = "A filtered version of the specified sequence",
examples = {
@Example(expression = "Sequence{'a', 'b', 'c'}->select(str | str.equals('a'))", result = "Sequence{'a'}")
}
)
// @formatter:on
public <T> List<T> select(List<T> sequence, LambdaValue lambda) {
final List<T> newList;
if (lambda == null) {
newList = new ArrayList<T>();
} else {
newList = new ArrayList<T>();
for (T elt : sequence) {
try {
Object value = lambda.eval(new Object[] {elt });
if (Boolean.TRUE.equals(value)) {
newList.add(elt);
}
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
lambda.logException(Diagnostic.WARNING, e);
}
}
}
return newList;
}
// @formatter:off
@Documentation(
value = "Select returns a filtered version of the specified set. Only elements for which the given \"lambda\" evaluates " +
"to true will be present in the returned set.",
params = {
@Param(name = "set", value = "The original set"),
@Param(name = "lambda", value = "The filtering expression")
},
result = "A filtered version of the specified set",
examples = {
@Example(expression = "OrderedSet{'a', 'b', 'c'}->select(str | str.equals('a'))", result = "OrderedSet{'a'}")
}
)
// @formatter:on
public <T> Set<T> select(Set<T> set, LambdaValue lambda) {
final Set<T> newSet;
if (lambda == null) {
newSet = new LinkedHashSet<T>();
} else {
newSet = new LinkedHashSet<T>();
for (T elt : set) {
try {
Object value = lambda.eval(new Object[] {elt });
if (Boolean.TRUE.equals(value)) {
newSet.add(elt);
}
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
lambda.logException(Diagnostic.WARNING, e);
}
}
}
return newSet;
}
// @formatter:off
@Documentation(
value = "Reject returns a filtered version of the specified set. Only elements for which the given \"lambda\" evaluates " +
"to false will be present in the returned set",
params = {
@Param(name = "set", value = "The original set"),
@Param(name = "lambda", value = "The filtering expression")
},
result = "A filtered version of the specified set",
examples = {
@Example(expression = "OrderedSet{'a', 'b', 'c'}->reject(str | str.equals('a'))", result = "OrderedSet{'b', 'c'}")
}
)
// @formatter:on
public <T> Set<T> reject(Set<T> set, LambdaValue lambda) {
final Set<T> newSet;
if (lambda == null) {
newSet = new LinkedHashSet<T>();
} else {
newSet = new LinkedHashSet<T>();
for (T elt : set) {
try {
Object value = lambda.eval(new Object[] {elt });
if (Boolean.FALSE.equals(value)) {
newSet.add(elt);
}
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
lambda.logException(Diagnostic.WARNING, e);
}
}
}
return newSet;
}
// @formatter:off
@Documentation(
value = "Reject returns a filtered version of the specified sequence. Only elements for which the given \"lambda\" evaluates " +
"to false will be present in the returned sequence",
params = {
@Param(name = "sequence", value = "The original sequence"),
@Param(name = "lambda", value = "The filtering expression")
},
result = "A filtered version of the specified sequence",
examples = {
@Example(expression = "Sequence{'a', 'b', 'c'}->reject(str | str.equals('a'))", result = "Sequence{'b', 'c'}")
}
)
// @formatter:on
public <T> List<T> reject(List<T> sequence, LambdaValue lambda) {
final List<T> newList;
if (lambda == null) {
newList = new ArrayList<T>();
} else {
newList = new ArrayList<T>();
for (T elt : sequence) {
try {
Object value = lambda.eval(new Object[] {elt });
if (Boolean.FALSE.equals(value)) {
newList.add(elt);
}
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
lambda.logException(Diagnostic.WARNING, e);
}
}
}
return newList;
}
// @formatter:off
@Documentation(
value = "Returns a set containing the result of applying \"lambda\" on all elements contained in the current set, " +
"maintaining order.",
params = {
@Param(name = "set", value = "The original set"),
@Param(name = "lambda", value = "The lambda expression")
},
result = "A transformed version of the specified set using the given lamba",
examples = {
@Example(expression = "OrderedSet{'a', 'b', 'c'}->collect(str | str.toUpper())", result = "OrderedSet{'A', 'B', 'C'}")
}
)
// @formatter:on
public Set<Object> collect(Set<?> set, LambdaValue lambda) {
final Set<Object> result;
if (lambda == null) {
result = Collections.emptySet();
} else {
result = new LinkedHashSet<Object>();
for (Object elt : set) {
try {
Object lambdaResult = lambda.eval(new Object[] {elt });
if (!(lambdaResult instanceof Nothing)) {
if (lambdaResult instanceof Collection) {
result.addAll((Collection<?>)lambdaResult);
} else if (lambdaResult != null) {
result.add(lambdaResult);
}
}
// CHECKSTYLE:OFF
} catch (Exception e) {
lambda.logException(Diagnostic.WARNING, e);
}
// CHECKSTYLE:ON
}
}
return result;
}
// @formatter:off
@Documentation(
value = "Returns a sequence containing the result of applying \"lambda\" on all elements contained in the current sequence, " +
"maintaining order.",
params = {
@Param(name = "sequence", value = "The original sequence"),
@Param(name = "lambda", value = "The lambda expression")
},
result = "A transformed version of the specified sequence using the given lamba",
examples = {
@Example(expression = "Sequence{'a', 'b', 'c'}->collect(str | str.toUpper())", result = "Sequence{'A', 'B', 'C'}")
}
)
// @formatter:on
public List<Object> collect(List<?> sequence, LambdaValue lambda) {
final List<Object> result;
if (lambda == null) {
result = Collections.emptyList();
} else {
result = new ArrayList<Object>();
for (Object elt : sequence) {
try {
Object lambdaResult = lambda.eval(new Object[] {elt });
if (!(lambdaResult instanceof Nothing)) {
if (lambdaResult instanceof Collection) {
result.addAll((Collection<?>)lambdaResult);
} else if (lambdaResult != null) {
result.add(lambdaResult);
}
}
// CHECKSTYLE:OFF
} catch (Exception e) {
lambda.logException(Diagnostic.WARNING, e);
}
// CHECKSTYLE:ON
}
}
return result;
}
// @formatter:off
@Documentation(
value = "Returns a set containing the result of applying \"lambda\" recursivly.",
params = {
@Param(name = "collection", value = "The original collection"),
@Param(name = "lambda", value = "The lambda expression")
},
result = "a set containing the result of applying \"lambda\" recursivly",
examples = {
@Example(expression = "Sequence{eCls}->closure(e | e.eContainer())", result = "Sequence{subEPkg, ePkg, rootEPkg}")
}
)
// @formatter:on
public Set<Object> closure(Collection<?> collection, LambdaValue lambda) {
final Set<Object> result;
if (lambda == null) {
result = Collections.emptySet();
} else {
result = new LinkedHashSet<Object>();
Set<Object> currentSet = new LinkedHashSet<Object>(collection);
Set<Object> added;
do {
added = new LinkedHashSet<Object>();
for (Object current : currentSet) {
try {
final Object lambdaResult = lambda.eval(new Object[] {current });
if (lambdaResult instanceof Collection) {
for (Object child : (Collection<?>)lambdaResult) {
if (result.add(child)) {
added.add(child);
}
}
} else if (lambdaResult != null && !(lambdaResult instanceof Nothing)) {
if (result.add(lambdaResult)) {
added.add(lambdaResult);
}
}
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
lambda.logException(Diagnostic.WARNING, e);
}
}
currentSet = added;
} while (!added.isEmpty());
}
return result;
}
// @formatter:off
@Documentation(
value = "Returns a sequence containing the elements of the original sequence ordered by the result of the given lamba",
params = {
@Param(name = "sequence", value = "The original sequence"),
@Param(name = "lambda", value = "The lambda expression")
},
result = "An ordered version of the given sequence",
examples = {
@Example(expression = "Sequence{'aa', 'bbb', 'c'}->sortedBy(str | str.size())", result = "Sequence{'c', 'aa', 'bbb'}")
}
)
// @formatter:on
public <T> List<T> sortedBy(List<T> sequence, final LambdaValue lambda) {
final List<T> result;
if (sequence == null) {
result = null;
} else if (lambda == null) {
return sequence;
} else {
result = new ArrayList<T>(sequence);
final Map<T, Object> values = new HashMap<T, Object>();
for (T object : result) {
try {
values.put(object, lambda.eval(new Object[] {object }));
// CHECKSTYLE:OFF
} catch (Exception e) {
lambda.logException(Diagnostic.WARNING, e);
}
// CHECKSTYLE:ON
}
Collections.sort(result, new LambdaComparator<T>(values));
}
return result;
}
// @formatter:off
@Documentation(
value = "Returns a set containing the elements of the original set ordered by the result of the given lamba",
params = {
@Param(name = "set", value = "The original set"),
@Param(name = "lambda", value = "The lambda expression")
},
result = "An ordered version of the given set",
examples = {
@Example(expression = "OrderedSet{'aa', 'bbb', 'c'}->sortedBy(str | str.size())", result = "OrderedSet{'c', 'aa', 'bbb'}")
}
)
// @formatter:on
public <T> Set<T> sortedBy(Set<T> set, final LambdaValue lambda) {
final Set<T> result;
if (set == null) {
result = null;
} else if (lambda == null) {
return set;
} else {
List<T> sorted = new ArrayList<T>(set);
final Map<T, Object> values = new HashMap<T, Object>();
for (T object : sorted) {
try {
values.put(object, lambda.eval(new Object[] {object }));
// CHECKSTYLE:OFF
} catch (Exception e) {
lambda.logException(Diagnostic.WARNING, e);
}
// CHECKSTYLE:ON
}
Collections.sort(sorted, new LambdaComparator<T>(values));
result = new LinkedHashSet<T>(sorted);
}
return result;
}
// @formatter:off
@Documentation(
value = "Returns the size of the specified collection",
params = {
@Param(name = "collection", value = "The input collection")
},
result = "The size of the specified collection",
examples = {
@Example(expression = "Sequence{'a', 'b', 'c'}->size()", result = "3"),
@Example(expression = "OrderedSet{'a', 'b', 'c', 'd'}->size()", result = "4")
}
)
// @formatter:on
public Integer size(Collection<?> collection) {
return collection.size();
}
// @formatter:off
@Documentation(
value = "Adds the given object to the current set.",
params = {
@Param(name = "set", value = "The source set"),
@Param(name = "object", value = "The object to add")
},
result = "A set containing all elements of source set plus the given object",
examples = {
@Example(expression = "OrderedSet{'a', 'b', 'c'}->including('d')", result = "OrderedSet{'a', 'b', 'c', 'd'}")
}
)
// @formatter:on
public <T> Set<T> including(Set<T> set, T object) {
if (set.contains(object)) {
return set;
} else {
Set<T> result = new LinkedHashSet<T>(set);
result.add(object);
return result;
}
}
// @formatter:off
@Documentation(
value = "Removes the given object from the current set.",
params = {
@Param(name = "set", value = "The source set"),
@Param(name = "object", value = "The object to remove")
},
result = "A set containing all elements of source set minus the given object",
examples = {
@Example(expression = "OrderedSet{'a', 'b', 'c'}->excluding('c')", result = "OrderedSet{'a', 'b'}")
}
)
// @formatter:on
public <T> Set<T> excluding(Set<T> set, Object object) {
if (!set.contains(object)) {
return set;
} else {
Set<T> result = new LinkedHashSet<T>(set);
result.remove(object);
return result;
}
}
// @formatter:off
@Documentation(
value = "Adds the given object to the current sequence.",
params = {
@Param(name = "sequence", value = "The source sequence"),
@Param(name = "object", value = "The object to add")
},
result = "A sequence containing all elements of the source sequence plus the given object",
examples = {
@Example(expression = "Sequence{'a', 'b', 'c'}->including('d')", result = "Sequence{'a', 'b', 'c', 'd'}")
}
)
// @formatter:on
public <T> List<T> including(List<T> sequence, T object) {
List<T> result = new ArrayList<T>(sequence);
result.add(object);
return result;
}
// @formatter:off
@Documentation(
value = "Removes the given object from the current sequence.",
params = {
@Param(name = "sequence", value = "The source sequence"),
@Param(name = "object", value = "The object to remove")
},
result = "A sequence containing all elements of source sequence minus the given object",
examples = {
@Example(expression = "Sequence{'a', 'b', 'c'}->excluding('c')", result = "Sequence{'a', 'b'}")
}
)
// @formatter:on
public <T> List<T> excluding(List<T> sequence, Object object) {
List<T> result = new ArrayList<T>(sequence);
result.removeAll(Collections.singleton(object));
return result;
}
// @formatter:off
@Documentation(
value = "Returns a sequence representation of the specified collection. Returns the same object if " +
"it is already a sequence.",
params = {
@Param(name = "collection", value = "The input collection")
},
result = "A sequence with all the elements of the input collection",
examples = {
@Example(expression = "OrderedSet{'a', 'b', 'c'}->asSequence()", result = "Sequence{'a', 'b', 'c'}"),
@Example(expression = "Sequence{'a', 'b', 'c'}->asSequence()", result = "Sequence{'a', 'b', 'c'}")
}
)
// @formatter:on
public <T> List<T> asSequence(Collection<T> collection) {
if (collection instanceof List) {
return (List<T>)collection;
} else {
return new ArrayList<T>(collection);
}
}
// @formatter:off
@Documentation(
value = "Returns a set representation of the specified collection. Returns the same object if " +
"it is already a set.",
params = {
@Param(name = "collection", value = "The input collection")
},
result = "A set with all the elements of the input collection",
examples = {
@Example(expression = "OrderedSet{'a', 'b', 'c'}->asSet()", result = "OrderedSet{'a', 'b', 'c'}"),
@Example(expression = "Sequence{'a', 'b', 'c', 'c', 'a'}->asSet()", result = "OrderedSet{'a', 'b', 'c'}")
}
)
// @formatter:on
public <T> Set<T> asSet(Collection<T> collection) {
if (collection instanceof Set) {
return (Set<T>)collection;
} else {
return new LinkedHashSet<T>(collection);
}
}
// @formatter:off
@Documentation(
value = "Returns a set representation of the specified collection. Returns the same object if " +
"it is a set already. This operation has the same behavior as \"asSet()\"",
params = {
@Param(name = "collection", value = "The input collection")
},
result = "A set with all the elements of the input collection",
examples = {
@Example(expression = "OrderedSet{'a', 'b', 'c'}->asOrderedSet()", result = "OrderedSet{'a', 'b', 'c'}"),
@Example(expression = "Sequence{'a', 'b', 'c'}->asOrderedSet()", result = "OrderedSet{'a', 'b', 'c'}")
}
)
// @formatter:on
public <T> Set<T> asOrderedSet(Collection<T> collection) {
return asSet(collection);
}
// @formatter:off
@Documentation(
value = "Returns the first element of the specified Collection.",
params = {
@Param(name = "collection", value = "The input collection")
},
result = "The first element of the collection",
examples = {
@Example(expression = "Sequence{'a', 'b', 'c'}->first()", result = "'a'")
}
)
// @formatter:on
public <T> T first(Collection<T> collection) {
final T res;
final Iterator<T> it = collection.iterator();
if (it.hasNext()) {
res = it.next();
} else {
res = null;
}
return res;
}
// @formatter:off
@Documentation(
value = "Returns the given sequence in reversed order.",
params = {
@Param(name = "sequence", value = "The input sequence")
},
result = "The sequence in reserved order",
examples = {
@Example(expression = "Sequence{'a', 'b', 'c'}->reverse()", result = "Sequence{'c', 'b', 'a'}")
}
)
// @formatter:on
public <T> List<T> reverse(List<T> sequence) {
final ArrayList<T> res = new ArrayList<T>(sequence);
Collections.reverse(res);
return res;
}
// @formatter:off
@Documentation(
value = "Returns the given set in reversed order.",
params = {
@Param(name = "set", value = "The input set")
},
result = "The set in reserved order",
examples = {
@Example(expression = "OrderedSet{'a', 'b', 'c'}->reverse()", result = "OrderedSet{'c', 'b', 'a'}")
}
)
// @formatter:on
public <T> Set<T> reverse(Set<T> set) {
final ArrayList<T> res = new ArrayList<T>(set);
Collections.reverse(res);
return new LinkedHashSet<T>(res);
}
// @formatter:off
@Documentation(
value = "Returns \"true\" when the input collection is empty.",
params = {
@Param(name = "collection", value = "The input collection")
},
result = "\"true\" when the input collection is empty.",
examples = {
@Example(expression = "OrderedSet{'a', 'b', 'c'}->isEmpty()", result = "false"),
@Example(expression = "Sequence{}->isEmpty()", result = "true"),
}
)
// @formatter:on
public Boolean isEmpty(Collection<?> collection) {
return collection.isEmpty();