com.landawn.abacus.util.ObjIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (c) 2017, 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;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.util.u.Nullable;
import com.landawn.abacus.util.stream.Stream;
/**
*
* @author Haiyang Li
* @param
* @since 0.9
*/
@SuppressWarnings({ "java:S6548" })
public abstract class ObjIterator extends ImmutableIterator {
@SuppressWarnings("rawtypes")
private static final ObjIterator EMPTY = new ObjIterator() {
@Override
public boolean hasNext() {
return false;
}
@Override
public Object next() {
throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
}
@Override
public ObjIterator skipNulls() {
return this;
}
};
/**
*
* @param
* @return
*/
public static ObjIterator empty() {
return EMPTY;
}
/**
*
* @param
* @param val
* @return
*/
public static ObjIterator just(final T val) {
return new ObjIterator<>() {
private boolean done = false;
@Override
public boolean hasNext() {
return !done;
}
@Override
public T next() {
if (done) {
throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
}
done = true;
return val;
}
};
}
/**
*
* @param
* @param a
* @return
*/
@SafeVarargs
public static ObjIterator of(final T... a) {
return N.isEmpty(a) ? EMPTY : of(a, 0, a.length);
}
/**
*
* @param
* @param a
* @param fromIndex
* @param toIndex
* @return
*/
public static ObjIterator of(final T[] a, final int fromIndex, final int toIndex) {
N.checkFromToIndex(fromIndex, toIndex, a == null ? 0 : a.length);
if (fromIndex == toIndex) {
return EMPTY;
}
return new ObjIterator<>() {
private int cursor = fromIndex;
@Override
public boolean hasNext() {
return cursor < toIndex;
}
@Override
public T next() {
if (cursor >= toIndex) {
throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
}
return a[cursor++];
}
@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());
}
};
}
/**
*
* @param
* @param iter
* @return
*/
public static ObjIterator of(final Iterator extends T> iter) {
if (iter == null) {
return empty();
} else if (iter instanceof ObjIterator) {
return (ObjIterator) iter;
}
return new ObjIterator<>() {
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public T next() {
return iter.next();
}
};
}
/**
*
* @param
* @param iterable
* @return
*/
public static ObjIterator of(final Collection extends T> iterable) {
return iterable == null ? ObjIterator. empty() : of(iterable.iterator());
}
/**
*
* @param
* @param iterable
* @return
*/
public static ObjIterator of(final Iterable extends T> iterable) {
return iterable == null ? ObjIterator. empty() : of(iterable.iterator());
}
// /**
// * Lazy evaluation.
// *
// * @param
// * @param arraySupplier
// * @return
// */
// public static ObjIterator from(final Supplier arraySupplier) {
// N.checkArgNotNull(arraySupplier, "arraySupplier");
//
// return new ObjIterator<>() {
// private T[] aar = null;
// private int len = 0;
// private int cur = 0;
// private boolean isInitialized = false;
//
// @Override
// public boolean hasNext() {
// if (!isInitialized) {
// init();
// }
//
// return cur < len;
// }
//
// @Override
// public T next() {
// if (!isInitialized) {
// init();
// }
//
// if (cur >= len) {
// throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
// }
//
// return aar[cur++];
// }
//
// private void init() {
// if (!isInitialized) {
// isInitialized = true;
// aar = arraySupplier.get();
// len = N.len(aar);
// }
// }
// };
// }
/**
* Lazy evaluation.
*
* @param
* @param iteratorSupplier
* @return
*/
public static ObjIterator defer(final Supplier extends Iterator extends T>> iteratorSupplier) {
N.checkArgNotNull(iteratorSupplier, "iteratorSupplier");
return new ObjIterator<>() {
private Iterator extends T> iter = 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();
}
private void init() {
if (!isInitialized) {
isInitialized = true;
iter = iteratorSupplier.get();
}
}
};
}
/**
* Returns an infinite {@code ObjIterator}.
*
* @param
* @param supplier
* @return
*/
public static ObjIterator generate(final Supplier extends T> supplier) {
N.checkArgNotNull(supplier);
return new ObjIterator<>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public T next() { // NOSONAR
return supplier.get();
}
};
}
/**
*
* @param
* @param hasNext
* @param supplier
* @return
*/
public static ObjIterator generate(final BooleanSupplier hasNext, final Supplier extends T> supplier) {
N.checkArgNotNull(hasNext);
N.checkArgNotNull(supplier);
return new ObjIterator<>() {
@Override
public boolean hasNext() {
return hasNext.getAsBoolean();
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
}
return supplier.get();
}
};
}
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param n
// * @return
// * @see Iterators#skip(Iterator, long)
// */
// public ObjIterator skip(final long n) {
// return Iterators.skip(this, n);
// }
//
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param count
// * @return
// * @see Iterators#limit(Iterator, long)
// */
// public ObjIterator limit(final long count) {
// return Iterators.limit(this, count);
// }
//
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param offset
// * @param count
// * @return
// * @see Iterators#skipAndLimit(Iterator, long, long)
// */
// public ObjIterator skipAndLimit(final long offset, final long count) {
// return Iterators.skipAndLimit(this, offset, count);
// }
//
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param filter
// * @return
// * @see Iterators#filter(Iterator, Predicate)
// */
// public ObjIterator filter(final Predicate super T> filter) {
// return Iterators.filter(this, filter);
// }
//
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param filter
// * @return
// * @see Iterators#takeWhile(Iterator, Predicate)
// */
// public ObjIterator takeWhile(final Predicate super T> filter) {
// return Iterators.takeWhile(this, filter);
// }
//
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param filter
// * @return
// * @see Iterators#takeWhileInclusive(Iterator, Predicate)
// */
// public ObjIterator takeWhileInclusive(final Predicate super T> filter) {
// return Iterators.takeWhileInclusive(this, filter);
// }
//
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param filter
// * @return
// * @see Iterators#dropWhile(Iterator, Predicate)
// */
// public ObjIterator dropWhile(final Predicate super T> filter) {
// return Iterators.dropWhile(this, filter);
// }
//
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param filter
// * @return
// * @see Iterators#skipUntil(Iterator, Predicate)
// */
// public ObjIterator skipUntil(final Predicate super T> filter) {
// return Iterators.skipUntil(this, filter);
// }
//
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param
// * @param mapper
// * @return
// * @see Iterators#map(Iterator, Function)
// */
// public ObjIterator map(final Function super T, U> mapper) {
// return Iterators.map(this, mapper);
// }
//
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param
// * @param mapper
// * @return
// * @see Iterators#flatMap(Iterator, Function)
// */
// public ObjIterator flatMap(final Function super T, ? extends Collection extends U>> mapper) {
// return Iterators.flatMap(this, mapper);
// }
//
// /**
// * Returns a new {@code ObjIterator}.
// *
// * @param
// * @param mapper
// * @return
// * @see Iterators#flatmap(Iterator, Function)
// */
// public ObjIterator flatmap(final Function super T, ? extends U[]> mapper) {
// return Iterators.flatmap(this, mapper);
// }
/**
*
* @param
* @param
* @param init
* @param hasNext
* @param supplier
* @return
*/
public static ObjIterator generate(final U init, final Predicate super U> hasNext, final Function super U, T> supplier) {
N.checkArgNotNull(hasNext);
N.checkArgNotNull(supplier);
return new ObjIterator<>() {
@Override
public boolean hasNext() {
return hasNext.test(init);
}
@Override
public T next() {
if (hasNext() == false) {
throw new NoSuchElementException();
}
return supplier.apply(init);
}
};
}
/**
*
* @param
* @param
* @param init
* @param hasNext
* @param supplier
* @return
*/
public static ObjIterator generate(final U init, final BiPredicate super U, T> hasNext, final BiFunction super U, T, T> supplier) {
N.checkArgNotNull(hasNext);
N.checkArgNotNull(supplier);
return new ObjIterator<>() {
private T prev = null;
@Override
public boolean hasNext() {
return hasNext.test(init, prev);
}
@Override
public T next() {
if (hasNext() == false) {
throw new NoSuchElementException();
}
return (prev = supplier.apply(init, prev));
}
};
}
/**
*
*
* @param n
* @return
*/
public ObjIterator skip(final long n) {
N.checkArgNotNegative(n, "n");
if (n <= 0) {
return this;
}
final ObjIterator iter = this;
return new ObjIterator<>() {
private boolean skipped = false;
@Override
public boolean hasNext() {
if (!skipped) {
skip();
}
return iter.hasNext();
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
}
return iter.next();
}
private void skip() {
long idx = 0;
while (idx++ < n && iter.hasNext()) {
iter.next();
}
skipped = true;
}
};
}
/**
*
*
* @param count
* @return
*/
public ObjIterator limit(final long count) {
N.checkArgNotNegative(count, "count");
if (count == 0) {
return ObjIterator. empty();
}
final ObjIterator iter = this;
return new ObjIterator<>() {
private long cnt = count;
@Override
public boolean hasNext() {
return cnt > 0 && iter.hasNext();
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
}
cnt--;
return iter.next();
}
};
}
/**
*
*
* @param predicate
* @return
*/
public ObjIterator filter(final Predicate super T> predicate) {
return Iterators.filter(this, predicate);
}
/**
*
*
* @param
* @param mapper
* @return
*/
@Beta
public ObjIterator map(final Function super T, U> mapper) {
return Iterators.map(this, mapper);
}
/**
*
*
* @return
*/
public Nullable first() {
if (hasNext()) {
return Nullable.of(next());
} else {
return Nullable. empty();
}
}
/**
*
*
* @return
*/
public u.Optional firstNonNull() {
T next = null;
while (hasNext()) {
next = next();
if (next != null) {
return u.Optional.of(next);
}
}
return u.Optional.empty();
}
/**
*
*
* @return
*/
public Nullable last() {
if (hasNext()) {
T next = next();
while (hasNext()) {
next = next();
}
return Nullable.of(next);
} else {
return Nullable. empty();
}
}
/**
*
* @return
*/
public ObjIterator skipNulls() {
return Iterators.skipNulls(this);
}
/**
*
*
* @return
*/
public Object[] toArray() {
return toArray(N.EMPTY_OBJECT_ARRAY);
}
/**
*
*
* @param
* @param a
* @return
*/
public A[] toArray(A[] a) {
return toList().toArray(a);
}
/**
*
*
* @return
*/
public List toList() {
final List list = new ArrayList<>();
while (hasNext()) {
list.add(next());
}
return list;
}
/**
*
*
* @return
*/
public Stream stream() {
return Stream.of(this);
}
/**
*
*
* @return
*/
@Beta
public ObjIterator> indexed() {
return indexed(0);
}
/**
*
*
* @param startIndex
* @return
*/
@Beta
public ObjIterator> indexed(final long startIndex) {
if (startIndex < 0) {
throw new IllegalArgumentException("Invalid start index: " + startIndex);
}
final ObjIterator iter = this;
return new ObjIterator<>() {
private long idx = startIndex;
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public Indexed next() {
return Indexed.of(iter.next(), idx++);
}
};
}
/**
*
* @param
* @param action
* @throws E the e
*/
public void foreachRemaining(Throwables.Consumer super T, E> action) throws E {
N.checkArgNotNull(action);
while (hasNext()) {
action.accept(next());
}
}
/**
*
* @param
* @param action
* @throws E the e
*/
public void foreachIndexed(Throwables.IntObjConsumer super T, E> action) throws E {
N.checkArgNotNull(action);
int idx = 0;
while (hasNext()) {
action.accept(idx++, next());
}
}
}