All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bestvike.linq.enumerable.DefaultIfEmpty Maven / Gradle / Ivy

package com.bestvike.linq.enumerable;

import com.bestvike.collections.generic.ICollection;
import com.bestvike.linq.IEnumerable;
import com.bestvike.linq.IEnumerator;
import com.bestvike.linq.exception.ExceptionArgument;
import com.bestvike.linq.exception.ThrowHelper;
import com.bestvike.linq.util.ArrayUtils;

import java.util.List;

/**
 * Created by 许崇雷 on 2018-04-27.
 */
public final class DefaultIfEmpty {
    private DefaultIfEmpty() {
    }

    public static  IEnumerable defaultIfEmpty(IEnumerable source) {
        return defaultIfEmpty(source, null);
    }

    public static  IEnumerable defaultIfEmpty(IEnumerable source, TSource defaultValue) {
        if (source == null)
            ThrowHelper.throwArgumentNullException(ExceptionArgument.source);

        return new DefaultIfEmptyIterator<>(source, defaultValue);
    }
}


final class DefaultIfEmptyIterator extends Iterator implements IIListProvider {
    private final IEnumerable source;
    private final TSource defaultValue;
    private IEnumerator enumerator;

    DefaultIfEmptyIterator(IEnumerable source, TSource defaultValue) {
        assert source != null;
        this.source = source;
        this.defaultValue = defaultValue;
    }

    @Override
    public Iterator clone() {
        return new DefaultIfEmptyIterator<>(this.source, this.defaultValue);
    }

    @Override
    public boolean moveNext() {
        switch (this.state) {
            case 1:
                this.enumerator = this.source.enumerator();
                if (this.enumerator.moveNext()) {
                    this.current = this.enumerator.current();
                    this.state = 2;
                    return true;
                }
                this.current = this.defaultValue;
                this.state = 3;
                return true;
            case 2:
                if (this.enumerator.moveNext()) {
                    this.current = this.enumerator.current();
                    return true;
                }
                this.state = 3;
            case 3:
                this.close();
                return false;
            default:
                return false;
        }
    }

    @Override
    public void close() {
        if (this.enumerator != null) {
            this.enumerator.close();
            this.enumerator = null;
        }
        super.close();
    }

    @Override
    public TSource[] _toArray(Class clazz) {
        TSource[] array = this.source.toArray(clazz);
        return array.length == 0 ? ArrayUtils.singleton(clazz, this.defaultValue) : array;
    }

    @Override
    public Object[] _toArray() {
        Object[] array = ToCollection.toArray(this.source);
        return array.length == 0 ? ArrayUtils.singleton(this.defaultValue) : array;
    }

    @Override
    public List _toList() {
        List list = this.source.toList();
        if (list.isEmpty())
            list.add(this.defaultValue);
        return list;
    }

    @Override
    public int _getCount(boolean onlyIfCheap) {
        int count;
        if (!onlyIfCheap || this.source instanceof ICollection)
            count = this.source.count();
        else
            count = this.source instanceof IIListProvider ? ((IIListProvider) this.source)._getCount(true) : -1;
        return count == 0 ? 1 : count;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy