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

functionalj.stream.ArrayBackedIteratorPlus Maven / Gradle / Ivy

// ============================================================================
// Copyright (c) 2017-2019 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.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();
    
    @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 = 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];
            }
        };
        this.current.set(this.start - 1);
    }
    ArrayBackedIteratorPlus(DATA[] array) {
        this(array, 0, array.length);
    }
    
    public IteratorPlus newIterator() {
        return new ArrayBackedIteratorPlus<>(array, start, start + end);
    }
    
    public int getStart() {
        return start;
    }
    
    public int getLength() {
        return end - start;
    }
    
    @Override
    public Iterator asIterator() {
        return iterator;
    }
    
    public StreamPlus stream() {
        return new ArrayBackedStream(this);
    }
    
    public Result> pullNext(int count) {
        val oldIndex = current.getAndAccumulate(count, (o, n) -> o + n) + 1;
        int newIndex = current.get();
        if ((newIndex >= end) && (count != 0))
            return Result.ofNoMore();
        
        return Result.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();
        
        val stream = new ArrayBackedIteratorPlus(array, old, old + count).stream();
        val value = mapper.apply(stream);
        return Result.valueOf(value);
    }
    
    public Streamable streamable() {
        return (Streamable)()->{
            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;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy