functionalj.lens.lenses.ListLensEach Maven / Gradle / Ivy
package functionalj.lens.lenses;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import functionalj.function.Func;
import functionalj.function.Func1;
import functionalj.lens.core.AccessParameterized;
import functionalj.list.FuncList;
import lombok.val;
public class ListLensEach >
implements FuncListAccess {
private final FuncListLens parentLens;
private final Predicate checker;
public ListLensEach(FuncListLens parentLens, Predicate checker) {
this.parentLens = parentLens;
this.checker = checker;
}
@Override
public AccessParameterized, TYPE, TYPELENS> accessParameterized() {
val read = Func.f((HOST host) -> {
return apply(host)
.filter(checker);
});
val specWithSub = new AccessParameterized, TYPE, TYPELENS>() {
@Override
public FuncList applyUnsafe(HOST host) throws Exception {
return read.apply(host);
}
@Override
public TYPELENS createSubAccessFromHost(Function accessToParameter) {
return parentLens.accessParameterized().createSubAccessFromHost(accessToParameter);
}
};
return specWithSub;
}
public Func1 changeToNull() {
return changeTo((TYPE)null);
}
public Func1 changeTo(TYPE newValue) {
return host -> {
val orgList = ListLensEach.this.parentLens.apply(host);
val newList = orgList.mapOnly(checker, __ -> newValue);
val newHost = ListLensEach.this.parentLens.changeTo(newList).apply(host);
return newHost;
};
}
public Func1 changeTo(Supplier supplier) {
return host -> {
val orgList = ListLensEach.this.parentLens.apply(host);
val newList = orgList.mapOnly(checker, __ -> supplier.get());
val newHost = ListLensEach.this.parentLens.changeTo(newList).apply(host);
return newHost;
};
}
public Func1 changeTo(Function mapper) {
return host -> {
val orgList = ListLensEach.this.parentLens.apply(host);
val newList = orgList.mapOnly(checker, mapper);
val newHost = ListLensEach.this.parentLens.changeTo(newList).apply(host);
return newHost;
};
}
}