• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

leeonky / test-charm-java / 390

18 Oct 2025 04:40PM UTC coverage: 75.298% (+0.01%) from 75.288%
390

push

circleci

leeonky
refactor

8 of 8 new or added lines in 5 files covered. (100.0%)

19 existing lines in 8 files now uncovered.

8895 of 11813 relevant lines covered (75.3%)

0.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.24
/bean-util/src/main/java/com/github/leeonky/util/CollectionHelper.java
1
package com.github.leeonky.util;
2

3
import java.lang.reflect.Array;
4
import java.lang.reflect.Type;
5
import java.util.*;
6
import java.util.stream.Collectors;
7
import java.util.stream.IntStream;
8
import java.util.stream.Stream;
9
import java.util.stream.StreamSupport;
10

11
import static com.github.leeonky.util.BeanClass.create;
12

UNCOV
13
public class CollectionHelper {
×
14

15
    @SuppressWarnings("unchecked")
16
    public static <E> Stream<E> toStream(Object collection) {
17
        if (collection != null) {
1✔
18
            Class<?> collectionType = collection.getClass();
1✔
19
            if (collectionType.isArray())
1✔
20
                return IntStream.range(0, Array.getLength(collection)).mapToObj(i -> (E) Array.get(collection, i));
1✔
21
            else if (collection instanceof Iterable)
1✔
22
                return StreamSupport.stream(((Iterable<E>) collection).spliterator(), false);
1✔
23
            else if (collection instanceof Stream)
1✔
24
                return (Stream<E>) collection;
1✔
25
        }
26
        throw new CannotToStreamException(collection);
1✔
27
    }
28

29
    public static <T> T convert(Object collection, BeanClass<T> toType) {
30
        return convert(collection, toType, Converter.getInstance());
1✔
31
    }
32

33
    @SuppressWarnings("unchecked")
34
    public static <T> T convert(Object collection, BeanClass<T> toType, Converter elementConverter) {
35
        if (collection != null) {
1✔
36
            Class<?> elementType = toType.getElementType().getType();
1✔
37
            return (T) toType.createCollection((toStream(collection).map(o ->
1✔
38
                    elementConverter.convert(elementType, o))).collect(Collectors.toList()));
1✔
39
        }
40
        return null;
1✔
41
    }
42

43
    public static Object createCollection(Collection<?> elements, BeanClass<?> type) {
44
        if (type.getType().isArray())
1✔
45
            return createArray(elements, type);
1✔
46
        if (type.getType().isInterface())
1✔
47
            return createInterfaceCollection(elements, type);
1✔
48
        if (Collection.class.isAssignableFrom(type.getType()))
1✔
49
            return createClassCollection(elements, type);
1✔
50
        throw new IllegalStateException(String.format("Cannot create instance of collection type %s", type.getName()));
1✔
51
    }
52

53
    @SuppressWarnings("unchecked")
54
    private static Collection<Object> createClassCollection(Collection<?> elements, BeanClass<?> type) {
55
        Collection<Object> collection = (Collection<Object>) type.newInstance();
1✔
56
        collection.addAll(elements);
1✔
57
        return collection;
1✔
58
    }
59

60
    private static AbstractCollection<?> createInterfaceCollection(Collection<?> elements, BeanClass<?> type) {
61
        if (Set.class.isAssignableFrom(type.getType()))
1✔
62
            return new LinkedHashSet<>(elements);
1✔
63
        if (Iterable.class.isAssignableFrom(type.getType()))
1✔
64
            return new ArrayList<>(elements);
1✔
UNCOV
65
        throw new IllegalStateException(String.format("Cannot create instance of collection type %s", type.getName()));
×
66
    }
67

68
    private static Object createArray(Collection<?> elements, BeanClass<?> type) {
69
        Object array = Array.newInstance(type.getType().getComponentType(), elements.size());
1✔
70
        int i = 0;
1✔
71
        for (Object element : elements)
1✔
72
            Array.set(array, i++, element);
1✔
73
        return array;
1✔
74
    }
75

76
    public static boolean equals(Object obj1, Object obj2) {
77
        if (obj1 == null || obj2 == null)
1✔
78
            return Objects.equals(obj1, obj2);
1✔
79
        return convert(obj1, create(List.class)).equals(convert(obj2, create(List.class)));
1✔
80
    }
81

82
    @SuppressWarnings("unchecked")
83
    public static <T> BeanClass<T> reify(Class<?> original, Type elementType) {
84
        if (original.isArray())
1✔
85
            return (BeanClass<T>) create(Array.newInstance((Class<?>) elementType, 0).getClass());
1✔
86
        return GenericBeanClass.create(original, elementType);
1✔
87
    }
88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc