functionalj.stream.ArrayBackedIteratorPlus Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
// ============================================================================
// Copyright (c) 2017-2021 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.stream;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntFunction;
import java.util.stream.StreamSupport;
import functionalj.function.Func1;
import functionalj.list.FuncList;
import functionalj.result.AutoCloseableResult;
import functionalj.result.Result;
import lombok.val;
public class ArrayBackedIteratorPlus implements IteratorPlus {
private final DATA[] array;
private final int start;
private final int end;
private final Iterator iterator;
private AtomicInteger current = new AtomicInteger();
private volatile Runnable closeHandler = null;
@SafeVarargs
public static ArrayBackedIteratorPlus of(DATA ... array) {
DATA[] copiedArray = Arrays.copyOf(array, array.length);
return new ArrayBackedIteratorPlus(copiedArray);
}
public static ArrayBackedIteratorPlus from(DATA[] array) {
DATA[] copiedArray = Arrays.copyOf(array, array.length);
return new ArrayBackedIteratorPlus(copiedArray);
}
public static ArrayBackedIteratorPlus from(DATA[] array, int start, int length) {
DATA[] copiedArray = Arrays.copyOf(array, array.length);
return new ArrayBackedIteratorPlus(copiedArray, start, length);
}
ArrayBackedIteratorPlus(DATA[] array, int start, int length) {
this.array = array;
this.start = Math.max(0, Math.min(array.length - 1, start));
this.end = Math.max(0, Math.min(array.length , start + length));
this.iterator = createIterator(array);
this.current.set(this.start - 1);
}
ArrayBackedIteratorPlus(DATA[] array) {
this(array, 0, array.length);
}
private Iterator createIterator(DATA[] array) {
return new Iterator() {
@Override
public boolean hasNext() {
return current.incrementAndGet() < ArrayBackedIteratorPlus.this.end;
}
@Override
public DATA next() {
int index = current.get();
if (index >= array.length)
throw new NoSuchElementException();
if (index < 0)
throw new NoSuchElementException();
return array[index];
}
};
}
public IteratorPlus newIterator() {
return new ArrayBackedIteratorPlus<>(array, start, start + end);
}
public int getStart() {
return start;
}
public int getLength() {
return end - start;
}
public void close() {
if (this.closeHandler != null) {
this.closeHandler.run();
}
}
public IteratorPlus onClose(Runnable closeHandler) {
if (closeHandler != null) {
synchronized (this) {
if (this.closeHandler == null) {
this.closeHandler = closeHandler;
} else {
val thisCloseHandler = this.closeHandler;
this.closeHandler = new Runnable() {
@Override
public void run() {
thisCloseHandler.run();
closeHandler.run();
}
};
}
}
}
return this;
}
@Override
public Iterator asIterator() {
return iterator;
}
public StreamPlus stream() {
return new ArrayBackedStreamPlus(this);
}
public AutoCloseableResult> pullNext(int count) {
val oldIndex = current.getAndAccumulate(count, (o, n) -> o + n) + 1;
int newIndex = current.get();
if ((newIndex >= end) && (count != 0))
return AutoCloseableResult.from(Result.ofNoMore());
return AutoCloseableResult.valueOf(new ArrayBackedIteratorPlus(array, oldIndex, oldIndex + count));
}
public Result mapNext(int count, Func1, TARGET> mapper) {
val old = current.getAndAccumulate(count, (o, n) -> o + n) + 1;
if ((current.get() >= end) && (count != 0))
return Result.ofNoMore();
try (val iterator = new ArrayBackedIteratorPlus(array, old, old + count)){
val stream = iterator.stream();
val value = mapper.apply(stream);
return Result.valueOf(value);
}
}
public FuncList FuncList() {
return FuncList.from(()->{
val iterable = (Iterable)()->newIterator();
return StreamPlus.from(StreamSupport.stream(iterable.spliterator(), false));
});
}
public DATA[] toArray() {
DATA[] copiedArray = Arrays.copyOfRange(array, start, end);
return copiedArray;
}
public A[] toArray(IntFunction generator) {
int length = end - start;
A[] newArray = generator.apply(length);
System.arraycopy(array, start, newArray, 0, length);
return newArray;
}
}