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

functionalj.stream.StreamPlusWithMapGroup Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// 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 static functionalj.stream.StreamPlusHelper.sequential;

import java.util.Spliterators;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.stream.StreamSupport;

import functionalj.function.Func1;
import functionalj.function.Func3;
import functionalj.function.Func4;
import functionalj.function.Func5;
import functionalj.function.Func6;
import functionalj.function.ObjectObjectToDoubleFunctionPrimitive;
import functionalj.function.ObjectObjectToIntFunctionPrimitive;
import functionalj.stream.doublestream.DoubleStreamPlus;
import functionalj.stream.intstream.IntStreamPlus;
import functionalj.tuple.Tuple2;
import functionalj.tuple.Tuple3;
import functionalj.tuple.Tuple4;
import functionalj.tuple.Tuple5;
import functionalj.tuple.Tuple6;
import lombok.val;


class StreamPlusWithMapGroupHelper {
    
    static  StreamPlus mapGroup(
            StreamPlus streamPlus,
            int count,
            Func4, Void> processNormal,
            Func4, Void> processTail) {
        return sequential(streamPlus, stream -> {
            val splitr = stream.spliterator();
            val spliterator = new Spliterators.AbstractSpliterator(splitr.estimateSize(), 0) {
                Object[] array = new Object[count*10];
                int      start = 0;
                int      end   = 0;
                boolean  used  = false;
                @Override
                public boolean tryAdvance(Consumer consumer) {
                    Consumer action = elem -> {
                        array[end] = elem;
                        end++;
                        int length = end - start;
                        if (length >= count) {
                            processNormal.apply(array, start, end, consumer);
                            used = true;
                            start++;
                        }
                        if (end >= array.length) {
                            System.arraycopy(array, start, array, 0, length - 1);
                            start = 0;
                            end   = length - 1;
                        }
                    };
                    boolean hasNext = splitr.tryAdvance(action);
                    if (!hasNext && !used && (processTail != null)) {
                        processTail.apply(array, start, end, consumer);
                    }
                    return hasNext;
                }
            };
            return StreamPlus.from(StreamSupport.stream(spliterator, false));
        });
    }
    
    static  IntStreamPlus mapGroupToInt(
            StreamPlus streamPlus,
            int count,
            Func4 processNormal,
            Func4 processTail) {
        val splitr = streamPlus.spliterator();
        val spliterator = new Spliterators.AbstractIntSpliterator(splitr.estimateSize(), 0) {
            Object[] array = new Object[count*10];
            int      start = 0;
            int      end   = 0;
            boolean  used  = false;
            @Override
            public boolean tryAdvance(IntConsumer consumer) {
                Consumer action = elem -> {
                    array[end] = elem;
                    end++;
                    int length = end - start;
                    if (length >= count) {
                        processNormal.apply(array, start, end, consumer);
                        used = true;
                        start++;
                    }
                    if (end >= array.length) {
                        System.arraycopy(array, start, array, 0, length - 1);
                        start = 0;
                        end   = length - 1;
                    }
                };
                boolean hasNext = splitr.tryAdvance(action);
                if (!hasNext && !used && (processTail != null)) {
                    processTail.apply(array, start, end, consumer);
                }
                return hasNext;
            }
        };
        return IntStreamPlus.from(StreamSupport.intStream(spliterator, false));
    }
    
    static  DoubleStreamPlus mapGroupToDouble(
            StreamPlus streamPlus,
            int count,
            Func4 processNormal,
            Func4 processTail) {
        val splitr = streamPlus.spliterator();
        val spliterator = new Spliterators.AbstractDoubleSpliterator(splitr.estimateSize(), 0) {
            Object[] array = new Object[count*10];
            int      start = 0;
            int      end   = 0;
            boolean  used  = false;
            @Override
            public boolean tryAdvance(DoubleConsumer consumer) {
                Consumer action = elem -> {
                    array[end] = elem;
                    end++;
                    int length = end - start;
                    if (length >= count) {
                        processNormal.apply(array, start, end, consumer);
                        used = true;
                        start++;
                    }
                    if (end >= array.length) {
                        System.arraycopy(array, start, array, 0, length - 1);
                        start = 0;
                        end   = length - 1;
                    }
                };
                boolean hasNext = splitr.tryAdvance(action);
                if (!hasNext && !used && (processTail != null)) {
                    processTail.apply(array, start, end, consumer);
                }
                return hasNext;
            }
        };
        return DoubleStreamPlus.from(StreamSupport.doubleStream(spliterator, false));
    }
}


public interface StreamPlusWithMapGroup {
    
    public StreamPlus streamPlus();
    
    /** @return  the stream of  each previous value and each current value. */
    @SuppressWarnings("unchecked")
    public default StreamPlus> mapTwo() {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroup(
                streamPlus, 2, 
                (array, start, end, consumer) -> {
                    val prev  = (DATA)array[start];
                    val curr  = (DATA)array[start + 1];
                    consumer.accept(Tuple2.of(prev, curr));
                    return (Void)null;
                },
                null);
    }
    
    /** @return  the stream of  each previous value and each current value. */
    @SuppressWarnings("unchecked")
    public default StreamPlus> mapThree() {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroup(
                streamPlus, 3, 
                (array, start, end, consumer) -> {
                    val value1 = (DATA)array[start];
                    val value2 = (DATA)array[start + 1];
                    val value3 = (DATA)array[start + 2];
                    consumer.accept(Tuple3.of(value1, value2, value3));
                    return (Void)null;
                },
                null);
    }
    
    /** @return  the stream of  each previous value and each current value. */
    @SuppressWarnings("unchecked")
    public default StreamPlus> mapFour() {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroup(
                streamPlus, 4, 
                (array, start, end, consumer) -> {
                    val value1 = (DATA)array[start];
                    val value2 = (DATA)array[start + 1];
                    val value3 = (DATA)array[start + 2];
                    val value4 = (DATA)array[start + 3];
                    consumer.accept(Tuple4.of(value1, value2, value3, value4));
                    return (Void)null;
                },
                null);
    }
    
    /** @return  the stream of  each previous value and each current value. */
    @SuppressWarnings("unchecked")
    public default StreamPlus> mapFive() {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroup(
                streamPlus, 5, 
                (array, start, end, consumer) -> {
                    val value1 = (DATA)array[start];
                    val value2 = (DATA)array[start + 1];
                    val value3 = (DATA)array[start + 2];
                    val value4 = (DATA)array[start + 3];
                    val value5 = (DATA)array[start + 4];
                    consumer.accept(Tuple5.of(value1, value2, value3, value4, value5));
                    return (Void)null;
                },
                null);
    }
    
    /** @return  the stream of  each previous value and each current value. */
    @SuppressWarnings("unchecked")
    public default StreamPlus> mapSix() {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroup(
                streamPlus, 6, 
                (array, start, end, consumer) -> {
                    val value1 = (DATA)array[start];
                    val value2 = (DATA)array[start + 1];
                    val value3 = (DATA)array[start + 2];
                    val value4 = (DATA)array[start + 3];
                    val value5 = (DATA)array[start + 4];
                    val value6 = (DATA)array[start + 5];
                    consumer.accept(Tuple6.of(value1, value2, value3, value4, value5, value6));
                    return (Void)null;
                },
                null);
    }
    
    /** Create a stream whose value is the combination between the previous value and the current value of this stream. */
    @SuppressWarnings("unchecked")
    public default  StreamPlus mapGroup(BiFunction combinator) {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroup(
                streamPlus, 2, 
                (array, start, end, consumer) -> {
                    val prev  = (DATA)array[start];
                    val curr  = (DATA)array[start + 1];
                    val value = combinator.apply(prev, curr);
                    consumer.accept(value);
                    return (Void)null;
                },
                null);
    }
    
    /** Create a stream whose value is the combination between the previous value and the current value of this stream. */
    @SuppressWarnings("unchecked")
    public default  StreamPlus mapGroup(Func3 combinator) {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroup(
                streamPlus, 3, 
                (array, start, end, consumer) -> {
                    val value1 = (DATA)array[start];
                    val value2 = (DATA)array[start + 1];
                    val value3 = (DATA)array[start + 2];
                    val value = combinator.apply(value1, value2, value3);
                    consumer.accept(value);
                    return (Void)null;
                },
                null);
    }
    
    /** Create a stream whose value is the combination between the previous value and the current value of this stream. */
    @SuppressWarnings("unchecked")
    public default  StreamPlus mapGroup(Func4 combinator) {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroup(
                streamPlus, 4, 
                (array, start, end, consumer) -> {
                    val value1 = (DATA)array[start];
                    val value2 = (DATA)array[start + 1];
                    val value3 = (DATA)array[start + 2];
                    val value4 = (DATA)array[start + 3];
                    val value = combinator.apply(value1, value2, value3, value4);
                    consumer.accept(value);
                    return (Void)null;
                },
                null);
    }
    
    /** Create a stream whose value is the combination between the previous value and the current value of this stream. */
    @SuppressWarnings("unchecked")
    public default  StreamPlus mapGroup(Func5 combinator) {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroup(
                streamPlus, 5, 
                (array, start, end, consumer) -> {
                    val value1 = (DATA)array[start];
                    val value2 = (DATA)array[start + 1];
                    val value3 = (DATA)array[start + 2];
                    val value4 = (DATA)array[start + 3];
                    val value5 = (DATA)array[start + 4];
                    val value  = combinator.apply(value1, value2, value3, value4, value5);
                    consumer.accept(value);
                    return (Void)null;
                },
                null);
    }
    
    /** Create a stream whose value is the combination between the previous value and the current value of this stream. */
    @SuppressWarnings("unchecked")
    public default  StreamPlus mapGroup(Func6 combinator) {
        return StreamPlusWithMapGroupHelper.mapGroup(
                streamPlus(), 6, 
                (array, start, end, consumer) -> {
                    val value1 = (DATA)array[start];
                    val value2 = (DATA)array[start + 1];
                    val value3 = (DATA)array[start + 2];
                    val value4 = (DATA)array[start + 3];
                    val value5 = (DATA)array[start + 4];
                    val value6 = (DATA)array[start + 5];
                    val value  = combinator.apply(value1, value2, value3, value4, value5, value6);
                    consumer.accept(value);
                    return (Void)null;
                },
                null);
    }
    
    /** @return  the stream of  each previous value and each current value. */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public default StreamPlus> mapGroup(int count) {
        return StreamPlusWithMapGroupHelper.>mapGroup(
                streamPlus(), count, 
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedIteratorPlus<>(array, start, count);
                    val streamPlus = new ArrayBackedStreamPlus<>(iterator);
                    consumer.accept((StreamPlus)(StreamPlus)streamPlus);
                    return (Void)null;
                },
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedIteratorPlus<>(array, start, end - start);
                    val streamPlus = new ArrayBackedStreamPlus<>(iterator);
                    consumer.accept((StreamPlus)(StreamPlus)streamPlus);
                    return (Void)null;
                });
    }
    
    /** @return  the stream of  each previous value and each current value. */
    public default  StreamPlus mapGroup(int count, Func1, ? extends TARGET> combinator) {
        return mapGroup(count)
                .map(combinator);
    }
    
    //== Int ==
    
    /** Create a stream whose value is the combination between the previous value and the current value of this stream. */
    @SuppressWarnings("unchecked")
    public default IntStreamPlus mapTwoToInt(ObjectObjectToIntFunctionPrimitive combinator) {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroupToInt(
                streamPlus, 2, 
                (array, start, end, consumer) -> {
                    val prev  = (DATA)array[start];
                    val curr  = (DATA)array[start + 1];
                    val value = combinator.apply(prev, curr);
                    consumer.accept(value);
                    return (Void)null;
                },
                null);
    }
    
    /** Create a stream whose value is the combination between the previous value and the current value of this stream. */
    @SuppressWarnings("unchecked")
    public default IntStreamPlus mapGroupToInt(int count, ToIntFunction> combinator) {
        return StreamPlusWithMapGroupHelper.mapGroupToInt(
                streamPlus(), count, 
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedIteratorPlus<>(array, start, count);
                    val streamPlus = new ArrayBackedStreamPlus<>(iterator);
                    val value      = combinator.applyAsInt((StreamPlus) streamPlus);
                    consumer.accept(value);
                    return (Void)null;
                },
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedIteratorPlus<>(array, start, end - start);
                    val streamPlus = new ArrayBackedStreamPlus<>(iterator);
                    val value      = combinator.applyAsInt((StreamPlus) streamPlus);
                    consumer.accept(value);
                    return (Void)null;
                });
    }
    
    //== Double ==
    
    /** Create a stream whose value is the combination between the previous value and the current value of this stream. */
    @SuppressWarnings("unchecked")
    public default DoubleStreamPlus mapTwoToDouble(ObjectObjectToDoubleFunctionPrimitive combinator) {
        val streamPlus = streamPlus();
        return StreamPlusWithMapGroupHelper.mapGroupToDouble(
                streamPlus, 2, 
                (array, start, end, consumer) -> {
                    val prev  = (DATA)array[start];
                    val curr  = (DATA)array[start + 1];
                    val value = combinator.apply(prev, curr);
                    consumer.accept(value);
                    return (Void)null;
                },
                null);
    }
    
    /** Create a stream whose value is the combination between the previous value and the current value of this stream. */
    @SuppressWarnings("unchecked")
    public default DoubleStreamPlus mapGroupToDouble(int count, ToDoubleFunction> combinator) {
        return StreamPlusWithMapGroupHelper.mapGroupToDouble(
                streamPlus(), count, 
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedIteratorPlus<>(array, start, count);
                    val streamPlus = new ArrayBackedStreamPlus<>(iterator);
                    val value      = combinator.applyAsDouble((StreamPlus) streamPlus);
                    consumer.accept(value);
                    return (Void)null;
                },
                (array, start, end, consumer) -> {
                    val iterator   = new ArrayBackedIteratorPlus<>(array, start, end - start);
                    val streamPlus = new ArrayBackedStreamPlus<>(iterator);
                    val value      = combinator.applyAsDouble((StreamPlus) streamPlus);
                    consumer.accept(value);
                    return (Void)null;
                });
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy