functionalj.lens.lenses.ListLens 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 java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import functionalj.function.Func1;
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 lombok.val;
@FunctionalInterface
public interface ListLens>
extends
ObjectLens>,
ListAccess {
public static >
ListLens of(
Function> read,
WriteLens> write,
Function, TYPELENS> subCreator) {
return LensUtils.createListLens(read, write, subCreator);
}
public static >
ListLens of(LensSpecParameterized, TYPE, TYPELENS> spec) {
return new ListLens() {
@Override
public LensSpecParameterized, TYPE, TYPELENS> lensSpecParameterized() {
return spec;
}
};
}
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 List 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 at(0);
}
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 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)->{
val newList = new ArrayList<>(list);
newList.set(index, newValue);
return newList;
});
}
public default Func1 changeTo(Predicate checker, Function mapper) {
return host -> {
val newList = apply(host).stream()
.map(each -> checker.test(each) ? mapper.apply(each) : each)
.collect(Collectors.toList());
val newHost = apply(host, newList);
return newHost;
};
}
}