public static <T> Collection<T> checkedSubTypeCast(
            Collection<? super T> collection, Class<T> subTypeClass) {
        for (Object o : collection) {
            // probe each object, will throw ClassCastException on mismatch.
            subTypeClass.cast(o);
        }
        return subTypeCast(collection);
    }
@Test
    void testCheckedSubTypeCast() {
        List<A> list = new ArrayList<>();
        B b = new B();
        C c = new C();
        list.add(b);
        list.add(c);
        list.add(null);
        Collection<B> castSuccess = CollectionUtil.checkedSubTypeCast(list, B.class);
        Iterator<B> iterator = castSuccess.iterator();
        assertThat(iterator.next()).isEqualTo(b);
        assertThat(iterator.next()).isEqualTo(c);
        assertThat(iterator.next()).isNull();
        assertThat(iterator).isExhausted();
        assertThatThrownBy(() -> CollectionUtil.checkedSubTypeCast(list, C.class))
                .isInstanceOf(ClassCastException.class);
    }