MCPcopy Create free account
hub / github.com/LFYSec/MScan / CollectionUtils

Class CollectionUtils

src/main/java/pascal/taie/util/collection/CollectionUtils.java:35–108  ·  view source on GitHub ↗

Utility methods for Collection. We name it CollectionUtils instead of Collections to avoid name collision with java.util.Collections.

Source from the content-addressed store, hash-verified

33 * with {@link java.util.Collections}.
34 */
35public final class CollectionUtils {
36
37 private CollectionUtils() {
38 }
39
40 /**
41 * Iterates the elements in the specific collection, in the order they are
42 * returned by the collection's iterator, and finds the first element
43 * of given collection that satisfies the predicate. If not such element
44 * is found, returns {@code null}.
45 */
46 @Nullable
47 public static <T> T findFirst(Collection<? extends T> c,
48 Predicate<? super T> p) {
49 for (T e : c) {
50 if (p.test(e)) {
51 return e;
52 }
53 }
54 return null;
55 }
56
57 /**
58 * @return an arbitrary element of the given collection.
59 */
60 public static <T> T getOne(Collection<T> c) {
61 return c.iterator().next();
62 }
63
64 /**
65 * Creates a list of given collection, appends a specific element to
66 * the list and returns it.
67 */
68 public static <T> List<T> append(Collection<? extends T> c, T e) {
69 List<T> result = new ArrayList<>(c.size() + 1);
70 result.addAll(c);
71 result.add(e);
72 return result;
73 }
74
75 /**
76 * Maps each element in given collection to an integer and computes
77 * the sum of the integers.
78 */
79 public static <T> long sum(Collection<? extends T> c, ToIntFunction<T> toInt) {
80 long sum = 0;
81 for (var e : c) {
82 sum += toInt.applyAsInt(e);
83 }
84 return sum;
85 }
86
87 /**
88 * Converts a collection to a string.
89 * The elements in the collection are <b>sorted</b> by their
90 * string representation (in alphabet order) in the resulting string.
91 * This is particularly useful for comparing expected results
92 * with the ones given by the analysis.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected