001/*
002 * Copyright (C) 2022 - 2025, 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 java.util.List;
019import java.util.stream.StreamSupport;
020import org.assertj.core.api.AbstractAssert;
021import org.assertj.core.api.AbstractListAssert;
022import org.assertj.core.api.AssertFactory;
023import org.jspecify.annotations.Nullable;
024
025//@formatter:off
026/**
027 * An implementation of {@link AbstractListAssert} that can perform type-specific assertions on the
028 * members of the container being asserted upon.
029 *
030 * <p>This acts as a bridge with AssertJ now that
031 * {@link org.assertj.core.api.FactoryBasedNavigableListAssert} has been deprecated. Users should
032 * treat this object like any other kind of {@link AbstractListAssert} for all purposes.
033 *
034 * @param <E> the element type to assert upon.
035 * @param <A> the assertion type for the element.
036 * @author Ashley Scopes
037 * @since 3.1.0
038 */
039public final class TypeAwareListAssert<E, A extends AbstractAssert<A, @Nullable E>>
040    extends AbstractListAssert<TypeAwareListAssert<@Nullable E, A>, @Nullable List<? extends @Nullable E>, @Nullable E, A> {
041  //@formatter:on
042
043  private final AssertFactory<@Nullable E, A> assertFactory;
044
045  TypeAwareListAssert(@Nullable List<? extends E> list, AssertFactory<E, A> assertFactory) {
046    super(list, TypeAwareListAssert.class);
047    this.assertFactory = assertFactory;
048  }
049
050  @Override
051  protected A toAssert(@Nullable E value, String description) {
052    return assertFactory.createAssert(value).describedAs(description);
053  }
054
055  @Override
056  protected TypeAwareListAssert<@Nullable E, A> newAbstractIterableAssert(
057      Iterable<? extends @Nullable E> iterable
058  ) {
059    var list = StreamSupport.stream(iterable.spliterator(), false).toList();
060    return new TypeAwareListAssert<>(list, assertFactory);
061  }
062}