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

com.landawn.abacus.util.stream.ObjIteratorEx Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.2.4
Show newest version
/*
 * Copyright (C) 2016, 2017, 2018, 2019 HaiYang Li
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.landawn.abacus.util.stream;

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

import com.landawn.abacus.annotation.Internal;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.ObjIterator;

/**
 *
 */
@Internal
public abstract class ObjIteratorEx extends ObjIterator implements IteratorEx {
    @SuppressWarnings("rawtypes")
    public static final ObjIteratorEx EMPTY = new BufferedIterator(0) { //NOSONAR
        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public Object next() {
            throw new NoSuchElementException();
        }

        @Override
        public void advance(long n) {
            // Do nothing.
        }

        @Override
        public long count() {
            return 0;
        }

        @Override
        public void close() {
            // Do nothing.
        }
    };

    public static  ObjIteratorEx empty() { //NOSONAR
        return EMPTY;
    }

    @SafeVarargs
    public static  ObjIteratorEx of(final T... a) {
        return a == null ? EMPTY : of(a, 0, a.length);
    }

    public static  ObjIteratorEx of(final T[] a, final int fromIndex, final int toIndex) {
        N.checkFromToIndex(fromIndex, toIndex, N.len(a));

        if (fromIndex == toIndex) {
            return EMPTY;
        }

        return new BufferedIterator(toIndex - fromIndex) {
            private int cursor = fromIndex;

            @Override
            public boolean hasNext() {
                return cursor < toIndex;
            }

            @Override
            public T next() {
                if (cursor >= toIndex) {
                    throw new NoSuchElementException();
                }

                return a[cursor++];
            }

            @Override
            public void advance(long n) {
                cursor = n < toIndex - cursor ? cursor + (int) n : toIndex;
            }

            @Override
            public long count() {
                return toIndex - cursor; //NOSONAR
            }

            @Override
            public  A[] toArray(A[] output) {
                if (output.length < toIndex - cursor) {
                    output = N.copyOf(output, toIndex - cursor);
                }

                N.copy(a, cursor, output, 0, toIndex - cursor);

                return output;
            }

            @Override
            public List toList() {
                return N.asList((T[]) toArray());
            }

            @Override
            public void close() {
                // Do nothing.
            }
        };
    }

    public static  ObjIteratorEx of(final Collection c) {
        if (c == null) {
            return empty();
        }

        final Iterator iter = c.iterator();

        return new BufferedIterator(c.size()) {
            @Override
            public boolean hasNext() {
                return iter.hasNext();
            }

            @Override
            public T next() {
                return iter.next();
            }

            @Override
            public void close() {
                // Do nothing.
            }
        };
    }

    public static  ObjIteratorEx of(final Iterator iter) {
        if (iter == null) {
            return empty();
        } else if (iter instanceof ObjIteratorEx) {
            return ((ObjIteratorEx) iter);
        }

        return new ObjIteratorEx<>() {
            @Override
            public boolean hasNext() {
                return iter.hasNext();
            }

            @Override
            public T next() {
                return iter.next();
            }

            @Override
            public void close() {
                // Do nothing.
            }
        };
    }

    public static  ObjIteratorEx of(final Iterable iterable) {
        return iterable == null ? ObjIteratorEx. empty() : of(iterable.iterator());
    }

    //    /**
    //     * Lazy evaluation.
    //     *
    //     * @param arraySupplier
    //     * @return
    //     */
    //    public static  ObjIteratorEx from(final Supplier arraySupplier) {
    //        N.checkArgNotNull(arraySupplier, "arraySupplier");
    //
    //        return new ObjIteratorEx<>() {
    //            private ObjIteratorEx iterEx = null;
    //            private boolean isInitialized = false;
    //
    //            @Override
    //            public boolean hasNext() {
    //                if (!isInitialized) {
    //                    init();
    //                }
    //
    //                return iterEx.hasNext();
    //            }
    //
    //            @Override
    //            public T next() {
    //                if (!isInitialized) {
    //                    init();
    //                }
    //
    //                return iterEx.next();
    //            }
    //
    //            @Override
    //            public void advance(long n) {
    //                if (!isInitialized) {
    //                    init();
    //                }
    //
    //                iterEx.advance(n);
    //            }
    //
    //            @Override
    //            public long count() {
    //                if (!isInitialized) {
    //                    init();
    //                }
    //
    //                return iterEx.count();
    //            }
    //
    //            private void init() {
    //                if (!isInitialized) {
    //                    isInitialized = true;
    //                    iterEx = ObjIteratorEx.of(arraySupplier.get());
    //                }
    //            }
    //        };
    //    }

    /**
     * Lazy evaluation.
     *
     * @param iteratorSupplier
     * @return
     */
    public static  ObjIteratorEx defer(final Supplier> iteratorSupplier) {
        N.checkArgNotNull(iteratorSupplier, "iteratorSupplier");

        return new ObjIteratorEx<>() {
            private Iterator iter = null;
            private ObjIteratorEx iterEx = null;
            private boolean isInitialized = false;

            @Override
            public boolean hasNext() {
                if (!isInitialized) {
                    init();
                }

                return iter.hasNext();
            }

            @Override
            public T next() {
                if (!isInitialized) {
                    init();
                }

                return iter.next();
            }

            @Override
            public void advance(long n) {
                if (!isInitialized) {
                    init();
                }

                if (iterEx != null) {
                    iterEx.advance(n);
                } else {
                    super.advance(n);
                }
            }

            @Override
            public long count() {
                if (!isInitialized) {
                    init();
                }

                if (iterEx != null) {
                    return iterEx.count();
                } else {
                    return super.count();
                }
            }

            @Override
            public void close() {
                if (!isInitialized) {
                    init();
                }

                if (iterEx != null) {
                    iterEx.close();
                }
            }

            private void init() {
                if (!isInitialized) {
                    isInitialized = true;
                    iter = iteratorSupplier.get();
                    iterEx = iter instanceof ObjIteratorEx ? (ObjIteratorEx) iter : null;
                }
            }
        };
    }

    @Override
    public void advance(long n) {
        N.checkArgNotNegative(n, "n");

        while (n > 0 && hasNext()) {
            next();
            n--;
        }
    }

    @Override
    public long count() {
        long result = 0;

        while (hasNext()) {
            next();
            result++;
        }

        return result;
    }

    @Override
    public void close() {
        // Do nothing.
    }

    abstract static class BufferedIterator extends ObjIteratorEx {
        private final int bufferSize;

        BufferedIterator(int bufferSize) {
            this.bufferSize = bufferSize;
        }

        public int max() {
            return bufferSize;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy