package net.ranides.assira.collection.query.derived;

import lombok.RequiredArgsConstructor;

import net.ranides.assira.collection.iterators.IteratorUtils;
import net.ranides.assira.collection.lists.ReversedList;
import net.ranides.assira.collection.query.CQuery;
import net.ranides.assira.collection.query.base.CQListSupplier;

import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;

/**
 * Adapter query: it converts source query into list and reverses order.
 * Used by {@link CQuery#reverse()} operation.
 *
 * It supports all fast features, if source supports fast "list".
 * It does not support any fast features in other case.
 *
 * @param <T> T
 */
public class CQReverse<T> extends CQListSupplier<T> {
    private final CQuery<T> that;

    public CQReverse(Supplier<? extends RuntimeException> message, CQuery<T> that) {
        super(message);
        this.that = that;
    }

    @Override
    public boolean hasFastList() {
        return that.features().hasFastList();
    }

    @Override
    public boolean hasFastLength() {
        return that.features().hasFastLength();
    }

    @Override
    public List<T> list() {
        return new ReversedList<>(that.list());
    }

    @Override
    public int size() {
        return that.size();
    }
}
