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

functionalj.stream.StreamableWithSegment Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// 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.Comparator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import functionalj.function.Func1;
import functionalj.function.Func2;
import functionalj.list.FuncList;
import lombok.val;

public interface StreamableWithSegment {
    
    
    public Streamable sorted();
    
    public  Streamable deriveWith(
            Function, Stream> action);
    
    public > Streamable sortedBy(
            Function mapper);
    
    public  Streamable sortedBy(
            Function mapper, 
            Comparator             comparator);
    
    
    //== segment ==
    
    public default Streamable> segment(
            int count) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from   (stream)
                    .segment(count);
        });
    }
    public default Streamable> segment(
            int     count, 
            boolean includeTail) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from   (stream)
                    .segment(count, includeTail);
        });
    }
    public default Streamable> segment(
            Predicate startCondition) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from   (stream)
                    .segment(startCondition);
        });
    }
    public default Streamable> segment(
            Predicate startCondition, 
            boolean         includeTail) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from   (stream)
                    .segment(startCondition, includeTail);
        });
    }
    
    public default Streamable> segment(
            Predicate startCondition, 
            Predicate endCondition) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from   (stream)
                    .segment(startCondition, endCondition);
        });
    }
    
    public default Streamable> segment(
            Predicate startCondition, 
            Predicate endCondition, 
            boolean         includeTail) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from   (stream)
                    .segment(startCondition, endCondition, includeTail);
        });
    }
    
    public default Streamable> segmentSize(
            Func1 segmentSize) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from       (stream)
                    .segmentSize(segmentSize);
        });
    }
    
    public default Streamable collapse(
            Predicate         conditionToCollapse, 
            Func2 concatFunc) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from    (stream)
                    .collapse(conditionToCollapse, concatFunc);
        });
    }
    
    public default Streamable collapseSize(
            Func1    segmentSize, 
            Func2 concatFunc) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from        (stream)
                    .collapseSize(segmentSize, concatFunc);
        });
    }
    
    public default  Streamable collapseSize(
            Func1          segmentSize, 
            Func1           mapper, 
            Func2 concatFunc) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from        (stream)
                    .collapseSize(segmentSize, mapper, concatFunc);
        });
    }
    
    //-- More - then StreamPlus --
    
    public default  Streamable> segmentByPercentiles(int ... percentiles) {
        val percentileList = IntStreamPlus.of(percentiles).mapToObj(Double::valueOf).toImmutableList();
        return segmentByPercentiles(percentileList);
    }
    
    public default  Streamable> segmentByPercentiles(double ... percentiles) {
        val percentileList = DoubleStreamPlus.of(percentiles).mapToObj(Double::valueOf).toImmutableList();
        return segmentByPercentiles(percentileList);
    }
    
    public default  FuncList> segmentByPercentiles(FuncList percentiles) {
        val list = sorted().toImmutableList();
        return Helper.segmentByPercentiles(list, percentiles);
    }
    
    public default > FuncList> segmentByPercentiles(Function mapper, int ... percentiles) {
        val percentileList = IntStreamPlus.of(percentiles).mapToObj(Double::valueOf).toImmutableList();
        return segmentByPercentiles(mapper, percentileList);
    }
    
    public default  FuncList> segmentByPercentiles(Function mapper, Comparator comparator, int ... percentiles) {
        val percentileList = IntStreamPlus.of(percentiles).mapToObj(Double::valueOf).toImmutableList();
        return segmentByPercentiles(mapper, comparator, percentileList);
    }
    
    public default > FuncList> segmentByPercentiles(Function mapper, double ... percentiles) {
        val percentileList = DoubleStreamPlus.of(percentiles).mapToObj(Double::valueOf).toImmutableList();
        return segmentByPercentiles(mapper, percentileList);
    }
    
    public default  FuncList> segmentByPercentiles(Function mapper, Comparator comparator, double ... percentiles) {
        val percentileList = DoubleStreamPlus.of(percentiles).mapToObj(Double::valueOf).toImmutableList();
        return segmentByPercentiles(mapper, comparator, percentileList);
    }
    
    public default > FuncList> segmentByPercentiles(Function mapper, FuncList percentiles) {
        val list = sortedBy(mapper).toImmutableList();
        return Helper.segmentByPercentiles(list, percentiles);
    }
    
    public default  FuncList> segmentByPercentiles(Function mapper, Comparator comparator, FuncList percentiles) {
        val list = sortedBy(mapper, comparator).toImmutableList();
        return Helper.segmentByPercentiles(list, percentiles);
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy