com.landawn.abacus.util.LongIterator Maven / Gradle / Ivy
/*
* Copyright (C) 2016 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.NoSuchElementException;
import com.landawn.abacus.util.function.BooleanSupplier;
import com.landawn.abacus.util.function.LongSupplier;
import com.landawn.abacus.util.function.Supplier;
/**
*
* @author Haiyang Li
* @since 0.8
*/
public abstract class LongIterator extends ImmutableIterator {
public static final LongIterator EMPTY = new LongIterator() {
@Override
public boolean hasNext() {
return false;
}
@Override
public long nextLong() {
throw new NoSuchElementException();
}
};
public static LongIterator empty() {
return EMPTY;
}
/**
*
* @param a
* @return
*/
@SafeVarargs
public static LongIterator of(final long... a) {
return N.isNullOrEmpty(a) ? EMPTY : of(a, 0, a.length);
}
/**
*
* @param a
* @param fromIndex
* @param toIndex
* @return
*/
public static LongIterator of(final long[] a, final int fromIndex, final int toIndex) {
N.checkFromToIndex(fromIndex, toIndex, a == null ? 0 : a.length);
if (fromIndex == toIndex) {
return EMPTY;
}
return new LongIterator() {
private int cursor = fromIndex;
@Override
public boolean hasNext() {
return cursor < toIndex;
}
@Override
public long nextLong() {
if (cursor >= toIndex) {
throw new NoSuchElementException();
}
return a[cursor++];
}
};
}
/**
* Lazy evaluation.
*
* @param iteratorSupplier
* @return
*/
public static LongIterator of(final Supplier extends LongIterator> iteratorSupplier) {
N.checkArgNotNull(iteratorSupplier, "iteratorSupplier");
return new LongIterator() {
private LongIterator iter = null;
private boolean isInitialized = false;
@Override
public boolean hasNext() {
if (isInitialized == false) {
init();
}
return iter.hasNext();
}
@Override
public long nextLong() {
if (isInitialized == false) {
init();
}
return iter.nextLong();
}
private void init() {
if (isInitialized == false) {
isInitialized = true;
iter = iteratorSupplier.get();
}
}
};
}
/**
* Lazy evaluation.
*
* @param arraySupplier
* @return
*/
public static LongIterator oF(final Supplier arraySupplier) {
N.checkArgNotNull(arraySupplier, "arraySupplier");
return new LongIterator() {
private long[] aar = null;
private int len = 0;
private int cur = 0;
private boolean isInitialized = false;
@Override
public boolean hasNext() {
if (isInitialized == false) {
init();
}
return cur < len;
}
@Override
public long nextLong() {
if (isInitialized == false) {
init();
}
if (cur >= len) {
throw new NoSuchElementException();
}
return aar[cur++];
}
private void init() {
if (isInitialized == false) {
isInitialized = true;
aar = arraySupplier.get();
len = N.len(aar);
}
}
};
}
/**
* Returns an infinite {@code LongIterator}.
*
* @param supplier
* @return
*/
public static LongIterator generate(final LongSupplier supplier) {
N.checkArgNotNull(supplier);
return new LongIterator() {
@Override
public boolean hasNext() {
return true;
}
@Override
public long nextLong() {
return supplier.getAsLong();
}
};
}
/**
*
* @param hasNext
* @param supplier
* @return
*/
public static LongIterator generate(final BooleanSupplier hasNext, final LongSupplier supplier) {
N.checkArgNotNull(hasNext);
N.checkArgNotNull(supplier);
return new LongIterator() {
@Override
public boolean hasNext() {
return hasNext.getAsBoolean();
}
@Override
public long nextLong() {
if (hasNext() == false) {
throw new NoSuchElementException();
}
return supplier.getAsLong();
}
};
}
/**
*
* @return
* @Deprecated use nextLong()
instead.
*/
@Deprecated
@Override
public Long next() {
return nextLong();
}
public abstract long nextLong();
/**
* For each remaining.
*
* @param action
*/
@Override
@Deprecated
public void forEachRemaining(java.util.function.Consumer super Long> action) {
super.forEachRemaining(action);
}
/**
*
* @param
* @param action
* @throws E the e
*/
public void foreachRemaining(Throwables.LongConsumer action) throws E {
N.checkArgNotNull(action);
while (hasNext()) {
action.accept(nextLong());
}
}
/**
*
* @param
* @param action
* @throws E the e
*/
public void foreachIndexed(Throwables.IndexedLongConsumer action) throws E {
N.checkArgNotNull(action);
int idx = 0;
while (hasNext()) {
action.accept(idx++, nextLong());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy