001/* 002 * Copyright (C) 2022 - 2024, the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.github.ascopes.jct.assertions; 017 018import static java.util.stream.Collectors.collectingAndThen; 019import static java.util.stream.Collectors.toList; 020 021import java.util.List; 022import java.util.function.Function; 023import java.util.stream.StreamSupport; 024import org.apiguardian.api.API; 025import org.apiguardian.api.API.Status; 026import org.assertj.core.api.AbstractAssert; 027import org.assertj.core.api.AbstractListAssert; 028import org.assertj.core.api.AssertFactory; 029import org.jspecify.annotations.Nullable; 030 031/** 032 * An implementation of {@link AbstractListAssert} that can perform type-specific assertions on the 033 * members of the container being asserted upon. 034 * 035 * <p>This acts as a bridge with AssertJ now that 036 * {@link org.assertj.core.api.FactoryBasedNavigableListAssert} has been deprecated. Users should 037 * treat this object like any other kind of {@link AbstractListAssert} for all purposes. 038 * 039 * @param <E> the element type to assert upon. 040 * @param <A> the assertion type for the element. 041 * @author Ashley Scopes 042 * @since 3.1.0 043 */ 044@API(since = "3.1.0", status = Status.STABLE) 045public final class TypeAwareListAssert<E, A extends AbstractAssert<A, @Nullable E>> 046 extends AbstractListAssert<TypeAwareListAssert<@Nullable E, A>, @Nullable List<? extends @Nullable E>, @Nullable E, A> { 047 048 private final AssertFactory<@Nullable E, A> assertFactory; 049 050 TypeAwareListAssert(@Nullable List<? extends E> list, AssertFactory<E, A> assertFactory) { 051 super(list, TypeAwareListAssert.class); 052 this.assertFactory = assertFactory; 053 } 054 055 @Override 056 protected A toAssert(@Nullable E value, String description) { 057 return assertFactory.createAssert(value).describedAs(description); 058 } 059 060 @Override 061 protected TypeAwareListAssert<@Nullable E, A> newAbstractIterableAssert( 062 Iterable<? extends @Nullable E> iterable 063 ) { 064 return StreamSupport 065 .stream(iterable.spliterator(), false) 066 .collect(collectingAndThen(toList(), curry())); 067 } 068 069 private Function<@Nullable List<@Nullable E>, TypeAwareListAssert<@Nullable E, A>> curry() { 070 return list -> new TypeAwareListAssert<>(list, assertFactory); 071 } 072}