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-2019 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 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;
@SuppressWarnings("javadoc")
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 >
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;
}
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);
}
}