functionalj.stream.StreamableAddtionalOperators 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.Collection;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import functionalj.result.Result;
import functionalj.tuple.Tuple2;
public interface StreamableAddtionalOperators {
public Streamable deriveWith(
Function, Stream> action);
//--map with condition --
public default Streamable mapOnly(
Predicate super DATA> checker,
Function super DATA, DATA> mapper) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.mapOnly(checker, mapper);
});
}
public default Streamable mapIf(
Predicate super DATA> checker,
Function super DATA, T> mapper,
Function super DATA, T> elseMapper) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.mapIf(checker, mapper, elseMapper);
});
}
//-- mapWithIndex --
public default Streamable> mapWithIndex() {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.mapWithIndex();
});
}
public default Streamable mapWithIndex(
BiFunction super Integer, ? super DATA, T> mapper) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.mapWithIndex(mapper);
});
}
public default Streamable mapWithIndex(
Function super DATA, ? extends T1> mapper1,
BiFunction super Integer, ? super T1, T> mapper) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.mapWithIndex(mapper1, mapper);
});
}
//-- mapWithPrev --
public default Streamable mapWithPrev(
BiFunction super Result, ? super DATA, ? extends TARGET> mapper) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.mapWithPrev(mapper);
});
}
//-- Filter --
public default Streamable, ? super DATA>> mapWithPrev() {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.mapWithPrev();
});
}
public default Streamable filterIn(
Collection super DATA> collection) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.filterIn(collection);
});
}
public default Streamable exclude(
Predicate super DATA> predicate) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.exclude(predicate);
});
}
public default Streamable excludeIn(
Collection super DATA> collection) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.excludeIn(collection);
});
}
public default Streamable filter(
Class clzz) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.filter(clzz);
});
}
public default Streamable filter(
Class clzz,
Predicate super T> theCondition) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.filter(clzz, theCondition);
});
}
public default Streamable filter(
Function super DATA, T> mapper,
Predicate super T> theCondition) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.filter(mapper, theCondition);
});
}
public default Streamable filterWithIndex(
BiFunction super Integer, ? super DATA, Boolean> predicate) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.filterWithIndex(predicate);
});
}
//-- Peek --
public default Streamable peek(
Class clzz,
Consumer super T> theConsumer) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.peek(clzz, theConsumer);
});
}
public default Streamable peek(
Predicate super DATA> selector,
Consumer super DATA> theConsumer) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.peek(selector, theConsumer);
});
}
public default Streamable peek(
Function super DATA, T> mapper,
Consumer super T> theConsumer) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.peek(mapper, theConsumer);
});
}
public default Streamable peek(
Function super DATA, T> mapper,
Predicate super T> selector,
Consumer super T> theConsumer) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.peek(mapper, selector, theConsumer);
});
}
//-- FlatMap --
public default Streamable flatMapOnly(
Predicate super DATA> checker,
Function super DATA, ? extends Streamable> mapper) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.flatMapOnly(
checker,
d -> mapper.apply(d).stream());
});
}
public default Streamable flatMapIf(
Predicate super DATA> checker,
Function super DATA, ? extends Streamable> mapper,
Function super DATA, ? extends Streamable> elseMapper) {
return deriveWith(stream -> {
return StreamPlus
.from(stream)
.flatMapIf(
checker,
d -> mapper .apply(d).stream(),
d -> elseMapper.apply(d).stream());
});
}
}