1 package org.andromda.translation.ocl.validation;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Random;
11 import java.util.Set;
12 import java.util.TreeSet;
13 import org.apache.commons.collections.Bag;
14 import org.apache.commons.collections.CollectionUtils;
15 import org.apache.commons.collections.Predicate;
16 import org.apache.commons.collections.SetUtils;
17 import org.apache.commons.collections.Transformer;
18 import org.apache.commons.collections.bag.HashBag;
19 import org.apache.commons.lang.StringUtils;
20
21
22
23
24 public final class OCLCollections
25 {
26
27
28
29
30
31
32 public static int count(
33 final Collection collection,
34 Object item)
35 {
36 return collection == null ? 0 : CollectionUtils.cardinality(item, collection);
37 }
38
39
40
41
42
43
44
45 public static boolean excludes(
46 final Collection collection,
47 final Object item)
48 {
49 return collection == null || !collection.contains(item);
50 }
51
52
53
54
55
56
57
58
59 public static boolean excludesAll(
60 final Collection collection,
61 final Collection items)
62 {
63 boolean excludesAll = true;
64 for (final Iterator iterator = items.iterator(); iterator.hasNext();)
65 {
66 final Object object = iterator.next();
67 if (!excludes(collection, object))
68 {
69 excludesAll = false;
70 break;
71 }
72 }
73 return excludesAll;
74 }
75
76
77
78
79
80
81
82 public static boolean includes(
83 final Collection collection,
84 final Object item)
85 {
86 return collection != null && collection.contains(item);
87 }
88
89
90
91
92
93
94
95 public static boolean includesAll(
96 final Collection collection,
97 final Collection items)
98 {
99 return collection != null && collection.containsAll(items);
100 }
101
102
103
104
105
106
107 public static boolean isEmpty(final Collection collection)
108 {
109 return (collection == null) || (collection.isEmpty());
110 }
111
112
113
114
115
116
117 public static boolean isEmpty(final Object object)
118 {
119 boolean isEmpty = object == null;
120 if (!isEmpty)
121 {
122 if (object instanceof Collection)
123 {
124 isEmpty = ((Collection) object).isEmpty();
125 }
126 else if (object instanceof String)
127 {
128 isEmpty = isEmpty((String) object);
129 }
130 else if (object != null && object.getClass().isArray())
131 {
132 isEmpty = ((Object[]) object).length == 0;
133 }
134 }
135 return isEmpty;
136 }
137
138
139
140
141
142
143
144 public static boolean isEmpty(final String string)
145 {
146 return StringUtils.isBlank(string);
147 }
148
149
150
151
152
153
154 public static boolean notEmpty(final Collection collection)
155 {
156 return (collection != null) && !isEmpty(collection);
157 }
158
159
160
161
162
163
164 public static boolean notEmpty(final Object object)
165 {
166 boolean notEmpty = object != null;
167 if (notEmpty)
168 {
169 if (object instanceof Collection)
170 {
171 notEmpty = !((Collection) object).isEmpty();
172 }
173 else if (object instanceof String)
174 {
175 notEmpty = notEmpty((String) object);
176 }
177 else if (object != null && object.getClass().isArray())
178 {
179 notEmpty = ((Object[]) object).length > 0;
180 }
181 }
182 return notEmpty;
183 }
184
185
186
187
188
189
190
191 public static boolean notEmpty(final String string)
192 {
193 return StringUtils.isNotBlank(string);
194 }
195
196
197
198
199
200
201
202
203 public static int size(final Object object)
204 {
205 int size = 0;
206 if (object != null)
207 {
208 if (object instanceof Collection)
209 {
210 size = size((Collection) object);
211 } else if (object.getClass().isArray())
212 {
213 size = ((Object[]) object).length;
214 }
215 }
216 return size;
217 }
218
219
220
221
222
223
224 public static int size(final Collection collection)
225 {
226 int size = 0;
227 if (collection != null)
228 {
229 size = collection.size();
230 }
231 return size;
232 }
233
234
235
236
237
238
239
240
241 public static double sum(final Object collection)
242 {
243 double sum = 0;
244 if (collection != null)
245 {
246 if (collection instanceof Collection)
247 {
248
249 sum = sum(collection);
250 }
251 else if (collection.getClass().isArray())
252 {
253 sum = sum(Arrays.asList((Object[]) collection));
254 }
255 }
256 return sum;
257 }
258
259
260
261
262
263
264
265
266 public static double sum(final Collection collection)
267 {
268 double sum = 0;
269 if (collection != null && !collection.isEmpty())
270 {
271 for (final Iterator iterator = collection.iterator(); iterator.hasNext();)
272 {
273 Object object = iterator.next();
274 if (object instanceof Number)
275 {
276 sum += ((Number) object).doubleValue();
277 } else
278 {
279 throw new UnsupportedOperationException(
280 "In order to calculate the sum of a collection\'s elements " +
281 "all of them must extend Number, found: " + object.getClass().getName());
282 }
283 }
284 }
285 return sum;
286 }
287
288
289
290
291
292
293
294 public static boolean append(
295 final List list,
296 final Object item)
297 {
298 return list == null ? false : list.add(item);
299 }
300
301
302
303
304
305
306
307 public static Object prepend(
308 final List list,
309 final Object item)
310 {
311 return list.set(0, item);
312 }
313
314
315
316
317
318
319
320 public static boolean append(
321 final Bag collection,
322 final Object item)
323 {
324 return collection == null ? false : collection.add(item);
325 }
326
327
328
329
330
331
332 public static Bag asBag(final Collection collection)
333 {
334 return collection == null ? new HashBag() : new HashBag(collection);
335 }
336
337
338
339
340
341
342 public static Set asOrderedSet(final Collection collection)
343 {
344 return collection == null ? Collections.emptySet() : SetUtils.orderedSet(new TreeSet(collection));
345 }
346
347
348
349
350
351
352 public static List asSequence(final Collection collection)
353 {
354 return collection == null ? Collections.emptyList() : new ArrayList(collection);
355 }
356
357
358
359
360
361
362 public static Set asSet(final Collection collection)
363 {
364 return collection == null ? Collections.emptySet() : new HashSet(collection);
365 }
366
367
368
369
370
371
372
373 public static Object at(
374 final List list,
375 final int index)
376 {
377 return list == null ? null : list.get(index);
378 }
379
380
381
382
383
384
385
386 public static boolean excluding(
387 final Collection collection,
388 final Object item)
389 {
390 return collection == null ? false : collection.remove(item);
391 }
392
393
394
395
396
397
398
399 public static boolean including(
400 final Collection collection,
401 final Object item)
402 {
403 return collection == null ? false : collection.add(item);
404 }
405
406
407
408
409
410
411
412 public static Collection flatten(final Collection collection)
413 {
414 final Collection flattenedCollection = new ArrayList();
415 for (final Iterator iterator = collection.iterator(); iterator.hasNext();)
416 {
417 final Object object = iterator.next();
418 if (object instanceof Collection)
419 {
420 flattenedCollection.addAll(flatten((Collection) object));
421 }
422 else
423 {
424 flattenedCollection.add(object);
425 }
426 }
427
428 return flattenedCollection;
429 }
430
431
432
433
434
435
436
437
438
439 public static int indexOf(
440 final List collection,
441 final Object item)
442 {
443 return collection == null ? -1 : collection.indexOf(item);
444 }
445
446
447
448
449
450
451
452 public static void insertAt(
453 final List collection,
454 int index,
455 Object item)
456 {
457 collection.add(index, item);
458 }
459
460
461
462
463
464
465
466 public static Collection intersection(
467 final Collection first,
468 final Collection second)
469 {
470 return CollectionUtils.intersection(first, second);
471 }
472
473
474
475
476
477
478
479 public static Collection union(
480 final Collection first,
481 final Collection second)
482 {
483 return CollectionUtils.union(first, second);
484 }
485
486
487
488
489
490
491
492
493 public static Object last(final Object object)
494 {
495 Object last = null;
496 final List list = objectToList(object);
497 if (!list.isEmpty())
498 {
499 last = list.get(list.size() - 1);
500 }
501 return last;
502 }
503
504
505
506
507
508
509
510
511 public static Object first(final Object object)
512 {
513 Object first = null;
514 final List list = objectToList(object);
515 if (!list.isEmpty())
516 {
517 first = list.get(0);
518 }
519 return first;
520 }
521
522
523
524
525
526
527
528 public static Collection symmetricDifference(
529 final Collection first,
530 final Collection second)
531 {
532 return CollectionUtils.disjunction(first, second);
533 }
534
535
536
537
538
539
540 public static Set subOrderedSet(final Set collection)
541 {
542 throw new UnsupportedOperationException(OCLCollections.class.getName() + ".subOrderedSet");
543 }
544
545
546
547
548
549
550 public static List subSequence(final List collection)
551 {
552 throw new UnsupportedOperationException(OCLCollections.class.getName() + ".subSequence");
553 }
554
555
556
557
558
559
560
561 public static Object any(
562 final Collection collection,
563 final Predicate predicate)
564 {
565 final List selectedElements = new ArrayList(select(collection, predicate));
566 final Random random = new Random(System.currentTimeMillis());
567 return selectedElements.isEmpty() ? null : selectedElements.get(random.nextInt(selectedElements.size()));
568 }
569
570
571
572
573
574
575
576
577 public static Collection collect(
578 final Collection collection,
579 final Transformer transformer)
580 {
581 return CollectionUtils.collect(collection, transformer);
582 }
583
584
585
586
587
588
589 public static Collection collectNested(final Collection collection)
590 {
591 throw new UnsupportedOperationException(OCLCollections.class.getName() + ".collectNested");
592 }
593
594
595
596
597
598
599
600
601 public static boolean exists(
602 final Collection collection,
603 final Predicate predicate)
604 {
605 return CollectionUtils.exists(collection, predicate);
606 }
607
608
609
610
611
612
613
614
615 public static boolean exists(
616 final Object collection,
617 final Predicate predicate)
618 {
619 return collection instanceof Collection ? exists((Collection) collection, predicate) : false;
620 }
621
622
623
624
625
626
627
628
629
630
631 public static boolean forAll(
632 final Collection collection,
633 final Predicate predicate)
634 {
635 boolean valid = collection != null;
636 if (valid && collection != null)
637 {
638 for (final Iterator iterator = collection.iterator(); iterator.hasNext();)
639 {
640 final Object object = iterator.next();
641 valid = predicate.evaluate(object);
642 if (!valid)
643 {
644 break;
645 }
646 }
647 }
648 return valid;
649 }
650
651
652
653
654
655
656
657
658
659
660
661 public static boolean forAll(
662 final Object collection,
663 final Predicate predicate)
664 {
665 boolean valid = false;
666 if (collection instanceof Collection)
667 {
668 valid = forAll((Collection) collection, predicate);
669 }
670 return valid;
671 }
672
673
674
675
676
677
678
679
680 public static boolean isUnique(
681 final Collection collection,
682 final Transformer transformer)
683 {
684 boolean unique = true;
685 final Set collected = new HashSet();
686 for (final Iterator iterator = collection.iterator(); iterator.hasNext() && unique;)
687 {
688 final Object result = transformer.transform(iterator.next());
689 if (collected.contains(result))
690 {
691 unique = false;
692 }
693 else
694 {
695 collected.add(result);
696 }
697 }
698 return unique;
699 }
700
701
702
703
704
705
706
707
708 public static boolean isUnique(
709 final Object collection,
710 final Transformer transformer)
711 {
712 boolean unique = collection != null;
713 if (unique && collection != null && Collection.class.isAssignableFrom(collection.getClass()))
714 {
715 unique = isUnique((Collection) collection, transformer);
716 }
717 return unique;
718 }
719
720
721
722
723
724
725 public static Collection iterate(final Collection collection)
726 {
727 throw new UnsupportedOperationException(OCLCollections.class.getName() + ".iterate");
728 }
729
730
731
732
733
734
735
736
737 public static boolean one(
738 final Collection collection,
739 final Predicate predicate)
740 {
741 boolean found = false;
742
743 if (collection != null)
744 {
745 for (final Iterator iterator = collection.iterator(); iterator.hasNext();)
746 {
747 if (predicate.evaluate(iterator.next()))
748 {
749 if (found)
750 {
751 found = false;
752 break;
753 }
754 found = true;
755 }
756 }
757 }
758 return found;
759 }
760
761
762
763
764
765
766
767
768
769
770 public static boolean one(
771 final Object collection,
772 final Predicate predicate)
773 {
774 return collection != null && Collection.class.isAssignableFrom(collection.getClass()) &&
775 one((Collection) collection, predicate);
776 }
777
778
779
780
781
782
783
784
785 public static Collection reject(
786 final Collection collection,
787 final Predicate predicate)
788 {
789 return CollectionUtils.selectRejected(collection, predicate);
790 }
791
792
793
794
795
796
797
798
799 public static Collection select(
800 final Collection collection,
801 final Predicate predicate)
802 {
803 return CollectionUtils.select(collection, predicate);
804 }
805
806
807
808
809
810
811
812
813 public static Collection select(
814 final Object collection,
815 final Predicate predicate)
816 {
817 return CollectionUtils.select((Collection) collection, predicate);
818 }
819
820
821
822
823
824
825 public static Collection sortedBy(final Collection collection)
826 {
827 throw new UnsupportedOperationException(OCLCollections.class.getName() + ".sortedBy");
828 }
829
830
831
832
833
834
835
836
837 private static List objectToList(Object object)
838 {
839 List list = null;
840 if (object instanceof Collection)
841 {
842 final Collection collection = (Collection) object;
843 if (!(object instanceof List))
844 {
845 object = new ArrayList(collection);
846 }
847 list = (List) object;
848 } else
849 {
850 list = new ArrayList();
851 if (object != null)
852 {
853 list.add(object);
854 }
855 }
856 return list;
857 }
858 }