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

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

The newest version!
package com.bestvike.linq.enumerable;

import com.bestvike.linq.IEnumerable;
import com.bestvike.linq.IEnumerator;
import com.bestvike.linq.adapter.enumerable.ArrayListEnumerable;
import com.bestvike.linq.adapter.enumerable.BooleanArrayEnumerable;
import com.bestvike.linq.adapter.enumerable.ByteArrayEnumerable;
import com.bestvike.linq.adapter.enumerable.CharEnumerable;
import com.bestvike.linq.adapter.enumerable.CharacterArrayEnumerable;
import com.bestvike.linq.adapter.enumerable.CollectionEnumerable;
import com.bestvike.linq.adapter.enumerable.DoubleArrayEnumerable;
import com.bestvike.linq.adapter.enumerable.EnumerationEnumerable;
import com.bestvike.linq.adapter.enumerable.EnumeratorEnumerable;
import com.bestvike.linq.adapter.enumerable.FloatArrayEnumerable;
import com.bestvike.linq.adapter.enumerable.GenericArrayEnumerable;
import com.bestvike.linq.adapter.enumerable.IntegerArrayEnumerable;
import com.bestvike.linq.adapter.enumerable.IterableEnumerable;
import com.bestvike.linq.adapter.enumerable.IteratorEnumerable;
import com.bestvike.linq.adapter.enumerable.LineEnumerable;
import com.bestvike.linq.adapter.enumerable.LinkedListEnumerable;
import com.bestvike.linq.adapter.enumerable.LongArrayEnumerable;
import com.bestvike.linq.adapter.enumerable.ShortArrayEnumerable;
import com.bestvike.linq.adapter.enumerable.SingletonEnumerable;
import com.bestvike.linq.adapter.enumerable.SpliteratorEnumerable;
import com.bestvike.linq.adapter.enumerable.StreamEnumerable;
import com.bestvike.linq.adapter.enumerable.WordEnumerable;

import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.RandomAccess;
import java.util.Spliterator;
import java.util.stream.Stream;

/**
 * Created by 许崇雷 on 2018-04-27.
 */
@SuppressWarnings("unchecked")
public final class Enumerable {
    private Enumerable() {
    }

    public static  IEnumerable empty() {
        return EmptyPartition.instance();
    }

    public static  IEnumerable singleton(TSource item) {
        return new SingletonEnumerable<>(item);
    }

    public static  IEnumerable ofNullable(TSource item) {
        return item == null ? EmptyPartition.instance() : new SingletonEnumerable<>(item);
    }

    public static IEnumerable of(boolean[] source) {
        return source == null ? EmptyPartition.instance() : new BooleanArrayEnumerable(source);
    }

    public static IEnumerable of(byte[] source) {
        return source == null ? EmptyPartition.instance() : new ByteArrayEnumerable(source);
    }

    public static IEnumerable of(short[] source) {
        return source == null ? EmptyPartition.instance() : new ShortArrayEnumerable(source);
    }

    public static IEnumerable of(int[] source) {
        return source == null ? EmptyPartition.instance() : new IntegerArrayEnumerable(source);
    }

    public static IEnumerable of(long[] source) {
        return source == null ? EmptyPartition.instance() : new LongArrayEnumerable(source);
    }

    public static IEnumerable of(float[] source) {
        return source == null ? EmptyPartition.instance() : new FloatArrayEnumerable(source);
    }

    public static IEnumerable of(double[] source) {
        return source == null ? EmptyPartition.instance() : new DoubleArrayEnumerable(source);
    }

    public static IEnumerable of(char[] source) {
        return source == null ? EmptyPartition.instance() : new CharacterArrayEnumerable(source);
    }

    public static  IEnumerable of(TSource[] source) {
        return source == null ? EmptyPartition.instance() : new GenericArrayEnumerable<>(source);
    }

    public static  IEnumerable of(List source) {
        return source == null ? EmptyPartition.instance() : source instanceof RandomAccess ? new ArrayListEnumerable<>(source) : new LinkedListEnumerable<>(source);
    }

    public static  IEnumerable of(Collection source) {
        return source == null ? EmptyPartition.instance() : new CollectionEnumerable<>(source);
    }

    public static  IEnumerable of(IEnumerable source) {
        return source == null ? EmptyPartition.instance() : source;
    }

    public static  IEnumerable of(IEnumerator source) {
        return source == null ? EmptyPartition.instance() : new EnumeratorEnumerable<>(source);
    }

    public static  IEnumerable of(Iterable source) {
        return source == null ? EmptyPartition.instance() : new IterableEnumerable<>(source);
    }

    public static  IEnumerable of(Iterator source) {
        return source == null ? EmptyPartition.instance() : new IteratorEnumerable<>(source);
    }

    public static  IEnumerable of(Stream source) {
        return source == null ? EmptyPartition.instance() : new StreamEnumerable<>(source);
    }

    public static  IEnumerable of(Spliterator source) {
        return source == null ? EmptyPartition.instance() : new SpliteratorEnumerable<>(source);
    }

    public static  IEnumerable of(Enumeration source) {
        return source == null ? EmptyPartition.instance() : new EnumerationEnumerable<>(source);
    }

    public static  IEnumerable> of(Map source) {
        return source == null ? EmptyPartition.instance() : new CollectionEnumerable<>(source.entrySet());
    }

    public static  IEnumerable as(Object source) {
        if (source == null)
            return EmptyPartition.instance();
        if (source instanceof boolean[])
            return (IEnumerable) new BooleanArrayEnumerable((boolean[]) source);
        if (source instanceof byte[])
            return (IEnumerable) new ByteArrayEnumerable((byte[]) source);
        if (source instanceof short[])
            return (IEnumerable) new ShortArrayEnumerable((short[]) source);
        if (source instanceof int[])
            return (IEnumerable) new IntegerArrayEnumerable((int[]) source);
        if (source instanceof long[])
            return (IEnumerable) new LongArrayEnumerable((long[]) source);
        if (source instanceof float[])
            return (IEnumerable) new FloatArrayEnumerable((float[]) source);
        if (source instanceof double[])
            return (IEnumerable) new DoubleArrayEnumerable((double[]) source);
        if (source instanceof char[])
            return (IEnumerable) new CharacterArrayEnumerable((char[]) source);
        if (source instanceof Object[])
            return new GenericArrayEnumerable<>((TSource[]) source);
        if (source instanceof List)
            return source instanceof RandomAccess ? new ArrayListEnumerable<>((List) source) : new LinkedListEnumerable<>((List) source);
        if (source instanceof Collection)
            return new CollectionEnumerable<>((Collection) source);
        if (source instanceof IEnumerable)
            return (IEnumerable) source;
        if (source instanceof Iterable)
            return new IterableEnumerable<>((Iterable) source);
        if (source instanceof Stream)
            return new StreamEnumerable<>((Stream) source);
        if (source instanceof IEnumerator)
            return new EnumeratorEnumerable<>((IEnumerator) source);
        if (source instanceof Iterator)
            return new IteratorEnumerable<>((Iterator) source);
        if (source instanceof Spliterator)
            return new SpliteratorEnumerable<>((Spliterator) source);
        if (source instanceof Enumeration)
            return new EnumerationEnumerable<>((Enumeration) source);
        if (source instanceof Map)
            return (IEnumerable) new CollectionEnumerable<>(((Map) source).entrySet());
        return null;
    }

    public static IEnumerable chars(CharSequence source) {
        return source == null ? EmptyPartition.instance() : new CharEnumerable(source);
    }

    public static IEnumerable words(CharSequence source) {
        return source == null ? EmptyPartition.instance() : new WordEnumerable(source);
    }

    public static IEnumerable lines(CharSequence source) {
        return source == null ? EmptyPartition.instance() : new LineEnumerable(source);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy