functionalj.stream.StreamableWithSegment 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-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 super DATA, T> mapper);
public Streamable sortedBy(
Function super DATA, T> 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 super DATA, T> mapper, int ... percentiles) {
val percentileList = IntStreamPlus.of(percentiles).mapToObj(Double::valueOf).toImmutableList();
return segmentByPercentiles(mapper, percentileList);
}
public default FuncList> segmentByPercentiles(Function super DATA, T> mapper, Comparator comparator, int ... percentiles) {
val percentileList = IntStreamPlus.of(percentiles).mapToObj(Double::valueOf).toImmutableList();
return segmentByPercentiles(mapper, comparator, percentileList);
}
public default > FuncList> segmentByPercentiles(Function super DATA, T> mapper, double ... percentiles) {
val percentileList = DoubleStreamPlus.of(percentiles).mapToObj(Double::valueOf).toImmutableList();
return segmentByPercentiles(mapper, percentileList);
}
public default FuncList> segmentByPercentiles(Function super DATA, T> mapper, Comparator comparator, double ... percentiles) {
val percentileList = DoubleStreamPlus.of(percentiles).mapToObj(Double::valueOf).toImmutableList();
return segmentByPercentiles(mapper, comparator, percentileList);
}
public default > FuncList> segmentByPercentiles(Function super DATA, T> mapper, FuncList percentiles) {
val list = sortedBy(mapper).toImmutableList();
return Helper.segmentByPercentiles(list, percentiles);
}
public default FuncList> segmentByPercentiles(Function super DATA, T> mapper, Comparator comparator, FuncList percentiles) {
val list = sortedBy(mapper, comparator).toImmutableList();
return Helper.segmentByPercentiles(list, percentiles);
}
}