functionalj.lens.lenses.ObjectLensImpl 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.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import functionalj.lens.core.AccessUtils;
import functionalj.lens.core.LensSpec;
import functionalj.lens.core.LensUtils;
import functionalj.lens.core.WriteLens;
import functionalj.list.FuncList;
import functionalj.map.FuncMap;
import functionalj.result.Result;
import lombok.val;
import nullablej.nullable.Nullable;
public class ObjectLensImpl implements ObjectLens {
private LensSpec spec;
public ObjectLensImpl(LensSpec spec) {
this.spec = spec;
}
@Override
public LensSpec lensSpec() {
return spec;
}
protected SUBLENS createSubLens(
Function readSub,
WriteLens writeSub,
Function, SUBLENS> subLensCreator) {
return LensUtils.createSubLens(this, readSub, writeSub, subLensCreator);
}
protected IntegerLens createSubLensInt(
ToIntFunction readSubInt,
WriteLens.PrimitiveInt writeSubInt) {
return LensUtils.createSubLens(this, readSubInt, writeSubInt);
}
protected LongLens createSubLensLong(
ToLongFunction readSubLong,
WriteLens.PrimitiveLong writeSubLong) {
return LensUtils.createSubLens(this, readSubLong, writeSubLong);
}
protected DoubleLens createSubLensDouble(
ToDoubleFunction readSubDouble,
WriteLens.PrimitiveDouble writeSubDouble) {
return LensUtils.createSubLens(this, readSubDouble, writeSubDouble);
}
protected BooleanLens createSubLensBoolean(
Predicate readSubBoolean,
WriteLens.PrimitiveBoolean writeSubBoolean) {
return LensUtils.createSubLens(this, readSubBoolean, writeSubBoolean);
}
protected >
ListLens createSubListLens(
Function> readSub,
WriteLens> writeSub,
Function, SUBLENS> subLensCreator) {
val readThis = this.lensSpec().getRead();
val writeThis = this.lensSpec().getWrite();
val subRead = (Function>) LensUtils.createSubRead(readThis, readSub, this.lensSpec().getIsNullSafe());
val subWrite = (WriteLens>)LensUtils.createSubWrite(readThis, writeThis, writeSub, this.lensSpec().getIsNullSafe());
val spec = LensUtils.createLensSpecParameterized(subRead, subWrite, subLensCreator);
val listLens = ListLens.of(spec);
return listLens;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
protected ListLens> createSubListLens(
Function> readSub,
WriteLens> writeSub) {
Function> readObject = (Function)readSub;
WriteLens> writeObject = (WriteLens)writeSub;
return createSubListLens(readObject, writeObject, ObjectLens::of);
}
protected >
FuncListLens createSubFuncListLens(
Function> readSub,
WriteLens> writeSub,
Function, SUBLENS> subLensCreator) {
val readThis = this.lensSpec().getRead();
val writeThis = this.lensSpec().getWrite();
val subRead = (Function>) LensUtils.createSubRead(readThis, readSub, this.lensSpec().getIsNullSafe());
val subWrite = (WriteLens>)LensUtils.createSubWrite(readThis, writeThis, writeSub, this.lensSpec().getIsNullSafe());
val spec = LensUtils.createLensSpecParameterized(subRead, subWrite, subLensCreator);
val listLens = FuncListLens.of(spec);
return listLens;
}
protected >
NullableLens createSubNullableLens(
Function> readSub,
WriteLens> writeSub,
Function, SUBLENS> subCreator) {
val readThis = this.lensSpec().getRead();
val writeThis = this.lensSpec().getWrite();
val subRead = (Function>) LensUtils.createSubRead(readThis, readSub, this.lensSpec().getIsNullSafe());
val subWrite = (WriteLens>)LensUtils.createSubWrite(readThis, writeThis, writeSub, this.lensSpec().getIsNullSafe());
val spec = LensUtils.createLensSpecParameterized(subRead, subWrite, subCreator);
val lens = (NullableLens)()->spec;
return lens;
}
protected >
ResultLens createSubResultLens(
Function> readSub,
WriteLens> writeSub,
Function, SUBLENS> subCreator) {
val readThis = this.lensSpec().getRead();
val writeThis = this.lensSpec().getWrite();
val subRead = (Function>) LensUtils.createSubRead(readThis, readSub, this.lensSpec().getIsNullSafe());
val subWrite = (WriteLens>)LensUtils.createSubWrite(readThis, writeThis, writeSub, this.lensSpec().getIsNullSafe());
val spec = LensUtils.createLensSpecParameterized(subRead, subWrite, subCreator);
val lens = (ResultLens)()->spec;
return lens;
}
protected >
ResultAccess createSubResultAccess(
Function> readSub,
Function, SUBACCESSS> subCreator) {
val readThis = this.lensSpec().getRead();
val subRead = (Function>) LensUtils.createSubRead(readThis, readSub, this.lensSpec().getIsNullSafe());
return AccessUtils.createResultAccess(subRead, subCreator);
}
protected >
OptionalLens createSubOptionalLens(
Function> readSub,
WriteLens> writeSub,
Function, SUBLENS> subCreator) {
val readThis = this.lensSpec().getRead();
val writeThis = this.lensSpec().getWrite();
val subRead = (Function>) LensUtils.createSubRead(readThis, readSub, this.lensSpec().getIsNullSafe());
val subWrite = (WriteLens>)LensUtils.createSubWrite(readThis, writeThis, writeSub, this.lensSpec().getIsNullSafe());
val spec = LensUtils.createLensSpecParameterized(subRead, subWrite, subCreator);
val lens = (OptionalLens)()->spec;
return lens;
}
protected , VALUELENS extends AnyLens>
MapLens createSubMapLens(
Function> readSub,
WriteLens> writeSub,
Function, KEYLENS> keyLensCreator,
Function, VALUELENS> valueLensCreator) {
val readThis = this.lensSpec().getRead();
val writeThis = this.lensSpec().getWrite();
val subRead = (Function>) LensUtils.createSubRead(readThis, readSub, this.lensSpec().getIsNullSafe());
val subWrite = (WriteLens>)LensUtils.createSubWrite(readThis, writeThis, writeSub, this.lensSpec().getIsNullSafe());
return MapLens.of(subRead, subWrite, keyLensCreator, valueLensCreator);
}
protected , VALUELENS extends AnyLens>
FuncMapLens createSubFuncMapLens(
Function> readSub,
WriteLens> writeSub,
Function, KEYLENS> keyLensCreator,
Function, VALUELENS> valueLensCreator) {
val readThis = this.lensSpec().getRead();
val writeThis = this.lensSpec().getWrite();
val subRead = (Function>) LensUtils.createSubRead(readThis, readSub, this.lensSpec().getIsNullSafe());
val subWrite = (WriteLens>)LensUtils.createSubWrite(readThis, writeThis, writeSub, this.lensSpec().getIsNullSafe());
return FuncMapLens.of(subRead, subWrite, keyLensCreator, valueLensCreator);
}
}