1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.truth0.subjects; |
19 | |
|
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Arrays; |
22 | |
import java.util.Collection; |
23 | |
import java.util.Collections; |
24 | |
import java.util.HashSet; |
25 | |
import java.util.List; |
26 | |
import java.util.Set; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | final class SubjectUtils { |
35 | |
|
36 | |
static <T> List<T> accumulate(T only) { |
37 | 0 | return new ArrayList<T>(Collections.singleton(only)); |
38 | |
} |
39 | |
static <T> List<T> accumulate(T first, T second, T ... rest) { |
40 | |
|
41 | |
|
42 | |
|
43 | 41 | List<T> items = new ArrayList<T>(2 + ((rest == null) ? 1 : rest.length)); |
44 | 41 | items.add(first); |
45 | 41 | items.add(second); |
46 | 41 | if (rest == null) { |
47 | 3 | items.add(null); |
48 | |
} else { |
49 | 38 | items.addAll(Arrays.asList(rest)); |
50 | |
} |
51 | 41 | return items; |
52 | |
} |
53 | |
|
54 | |
static <T> int countOf(T t, Iterable<T> items) { |
55 | 14 | int count = 0; |
56 | 14 | for (T item : items) { |
57 | 28 | if (t == null ? (item == null) : t.equals(item)) { |
58 | 22 | count++; |
59 | |
} |
60 | |
} |
61 | 14 | return count; |
62 | |
} |
63 | |
|
64 | |
static <T> List<Object> countDuplicates(Collection<T> items) { |
65 | 12 | Set<T> itemSet = new HashSet<T>(items); |
66 | 12 | Object[] params = new Object[itemSet.size()]; |
67 | 12 | int n = 0; |
68 | 12 | for (T item : itemSet) { |
69 | 14 | int count = countOf(item, items); |
70 | 14 | params[n++] = (count > 1) ? item + " [" + count + " copies]" : item; |
71 | 14 | } |
72 | 12 | return Arrays.asList(params); |
73 | |
} |
74 | |
|
75 | |
} |