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

import net.ranides.assira.collection.query.CQuery;
import net.ranides.assira.collection.query.CQueryAbstract;
import net.ranides.assira.collection.query.support.BaseIterable;
import net.ranides.assira.generic.Ref;

import java.util.Iterator;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * Stream implementation which uses iterator generator as source of data.
 *
 * Obviously, it supports fast iterator, it provided generator create iterator fast.
 *
 * @param <T>
 */
public class CQOptional<T> extends CQueryAbstract<T> {

    private final Ref<T> optional;

    public CQOptional(Ref<T> optional) {
        this.optional = optional;
    }

    @Override
    public Supplier<? extends RuntimeException> message() {
        return optional.message();
    }

    @Override
    public CQuery<T> message(Supplier<? extends RuntimeException> message) {
        return new CQOptional<>(optional.withMessage(message));
    }

    @Override
    public Iterator<T> iterator() {
        return optional.iterator();
    }

    @Override
    public boolean isParallel() {
        return false;
    }

    @Override
    public CQuery<T> parallel() {
        return this;
    }

    @Override
    public CQuery<T> sequential() {
        return this;
    }

    @Override
    public Stream<T> stream() {
        return BaseIterable.stream(this);
    }

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

    @Override
    public CQuery<T> prefetch() {
        return this;
    }

    @Override
    public CQuery<T> fetch() {
        return this;
    }
}


