functionalj.stream.StreamPlusWithFillNull Maven / Gradle / Ivy
package functionalj.stream;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import functionalj.function.Func1;
import functionalj.function.Func2;
import functionalj.lens.core.WriteLens;
import functionalj.lens.lenses.AnyLens;
import lombok.val;
public interface StreamPlusWithFillNull {
public StreamPlus deriveWith(Function, Stream> action);
//== fillNull ==
public default StreamPlus fillNull(
AnyLens lens,
VALUE replacement) {
return fillNull(
(Func1)lens,
((WriteLens)lens)::apply,
replacement);
}
public default StreamPlus fillNull(
Func1 get,
Func2 set,
VALUE replacement) {
return deriveWith(stream -> {
return (Stream)stream
.map(orgElmt -> {
val value = get.apply(orgElmt);
if (value == null) {
val newElmt = set.apply(orgElmt, replacement);
return (DATA)newElmt;
}
return orgElmt;
});
});
}
public default StreamPlus fillNull(
AnyLens lens,
Supplier replacementSupplier) {
return fillNull(
(Func1)lens,
((WriteLens)lens)::apply,
replacementSupplier);
}
public default StreamPlus fillNull(
Func1 get,
Func2 set,
Supplier replacementSupplier) {
return deriveWith(stream -> {
return (Stream)stream
.map(orgElmt -> {
val value = get.apply(orgElmt);
if (value == null) {
val replacement = replacementSupplier.get();
val newElmt = set.apply(orgElmt, replacement);
return (DATA)newElmt;
}
return orgElmt;
});
});
}
public default StreamPlus fillNull(
AnyLens lens,
Func1 replacementFunction) {
return fillNull(
(Func1)lens,
((WriteLens)lens)::apply,
replacementFunction);
}
public default StreamPlus fillNull(
Func1 get,
Func2 set,
Func1 replacementFunction) {
return deriveWith(stream -> {
return (Stream)stream
.map(orgElmt -> {
val value = get.apply(orgElmt);
if (value == null) {
val replacement = replacementFunction.apply(orgElmt);
val newElmt = set.apply(orgElmt, replacement);
return (DATA)newElmt;
}
return orgElmt;
});
});
}
}