functionalj.lens.lenses.FuncListLens 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-2021 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.lens.lenses;
import static functionalj.function.Func.alwaysTrue;
import java.util.ArrayList;
import java.util.function.Function;
import java.util.function.Predicate;
import functionalj.lens.core.AccessParameterized;
import functionalj.lens.core.LensSpec;
import functionalj.lens.core.LensSpecParameterized;
import functionalj.lens.core.LensUtils;
import functionalj.lens.core.WriteLens;
import functionalj.list.FuncList;
import functionalj.list.ImmutableFuncList;
import lombok.val;
public interface FuncListLens>
extends
ObjectLens>,
FuncListAccess {
public static >
FuncListLens of(
Function> read,
WriteLens> write,
Function, TYPELENS> subCreator) {
return LensUtils.createFuncListLens(read, write, subCreator);
}
public static >
FuncListLens of(LensSpecParameterized, TYPE, TYPELENS> spec) {
return ()->spec;
}
// :-( Duplicate
public LensSpecParameterized, TYPE, TYPELENS> lensSpecParameterized();
public default AccessParameterized, TYPE, TYPELENS> accessParameterized() {
return lensSpecParameterized();
}
@Override
public default TYPELENS createSubAccess(Function, TYPE> accessToSub) {
return accessParameterized()
.createSubAccess(accessToSub);
}
@Override
public default LensSpec> lensSpec() {
return lensSpecParameterized()
.getSpec();
}
@Override
public default FuncList applyUnsafe(HOST host) throws Exception {
return lensSpec()
.getRead()
.apply(host);
}
public default TYPELENS createSubLens(Function, TYPE> readSub, WriteLens, TYPE> writeSub) {
return LensUtils.createSubLens(this, readSub, writeSub, lensSpecParameterized()::createSubLens);
}
public default TYPELENS first() {
return createSubLens(
(list) -> list.first().get(),
(list, newValue) -> list.with(0, newValue));
}
public default TYPELENS last() {
return createSubLens(
(list) -> {
if (list == null)
return null;
if (list.isEmpty())
return null;
return list.get(list.size() - 1);
},
(list, newValue)->{
val newList = new ArrayList<>(list);
newList.set(list.size() - 1, newValue);
return ImmutableFuncList.from(newList);
});
}
public default TYPELENS at(int index) {
return createSubLens(
(list) -> {
if (list == null)
return null;
if (list.isEmpty())
return null;
if (index < 0)
return null;
if (index >= list.size())
return null;
return list.get(index);
},
(list, newValue) -> {
return list.with(index, newValue);
});
}
public default ListLensEach each() {
return new ListLensEach<>(this, alwaysTrue());
}
public default ListLensEach eachOf(Predicate checker) {
return new ListLensEach<>(this, checker);
}
}