functionalj.lens.core.LensSpec 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.core;
import java.util.function.BooleanSupplier;
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.lenses.PrimitiveLensSpecs;
public class LensSpec
implements Function {
public static Function selfRead() { return self->self; }
public static WriteLens selfWrite() { return (self,newValue)->newValue; }
private final Function read;
private final WriteLens write;
private final BooleanSupplier isNullSafe; // May regret this later .. but WTH.
public Function getRead() {
return read;
}
public WriteLens getWrite() {
return write;
}
public BooleanSupplier getIsNullSafe() {
return isNullSafe;
}
public static final BooleanSupplier SUPPLY_TRUE = new BooleanSupplier() {
@Override public boolean getAsBoolean() { return true; }
@Override public String toString() { return "BooleanSupplier(true)"; }
};
public static final BooleanSupplier SUPPLY_FALSE = new BooleanSupplier() {
@Override public boolean getAsBoolean() { return false; }
@Override public String toString() { return "BooleanSupplier(false)"; }
};
public static final BooleanSupplier booleanSupplierOf(boolean bool) {
return bool ? SUPPLY_TRUE : SUPPLY_FALSE;
}
public static LensSpec of(Class dataClass) {
return new LensSpec(selfRead(), selfWrite());
}
public static LensSpec of(Function read) {
return of(read::apply, (WriteLens)null);
}
public static LensSpec of(Function read, WriteLens write) {
if (write == null)
return new LensSpec(read::apply, null);
return new LensSpec(read::apply, write::apply);
}
public static LensSpec of(
Function read,
WriteLens write,
boolean isNullSafe) {
return new LensSpec(read::apply, write::apply, booleanSupplierOf(isNullSafe));
}
public static LensSpec of(
Function read,
WriteLens write,
BooleanSupplier isNullSafe) {
return new LensSpec(read::apply, write::apply, isNullSafe);
}
public static PrimitiveLensSpecs.IntegerLensSpecPrimitive ofPrimitive(
ToIntFunction readInt,
WriteLens.PrimitiveInt writeInt) {
return new PrimitiveLensSpecs.IntegerLensSpecPrimitive(readInt, writeInt);
}
public static PrimitiveLensSpecs.LongLensSpecPrimitive ofPrimitive(
ToLongFunction readLong,
WriteLens.PrimitiveLong writeLong) {
return new PrimitiveLensSpecs.LongLensSpecPrimitive(readLong, writeLong);
}
public static PrimitiveLensSpecs.DoubleLensSpecPrimitive ofPrimitive(
ToDoubleFunction readDouble,
WriteLens.PrimitiveDouble writeDouble) {
return new PrimitiveLensSpecs.DoubleLensSpecPrimitive(readDouble, writeDouble);
}
public static PrimitiveLensSpecs.BooleanLensSpecPrimitive ofPrimitive(
Predicate readBoolean,
WriteLens.PrimitiveBoolean writeBoolean) {
return new PrimitiveLensSpecs.BooleanLensSpecPrimitive(readBoolean, writeBoolean);
}
public LensSpec(Function read, WriteLens write) {
this(read, write, SUPPLY_TRUE);
}
public LensSpec(Function read, WriteLens write, BooleanSupplier isNullSafe) {
this.read = read;
this.write = write;
this.isNullSafe = isNullSafe;
}
public DATA apply(HOST host) {
return read.apply(host);
}
public boolean isNullSafe() {
return isNullSafe.getAsBoolean();
}
public LensSpec withNullSafety(boolean isNullSafe) {
return (isNullSafe && SUPPLY_TRUE.equals(this.isNullSafe))
? this
: new LensSpec<>(read, write, isNullSafe ? SUPPLY_TRUE : SUPPLY_FALSE);
}
public LensSpec toNullSafe() {
return SUPPLY_TRUE.equals(this.isNullSafe) ? this : new LensSpec<>(read, write, SUPPLY_TRUE);
}
public LensSpec toNullUnsafe() {
return SUPPLY_FALSE.equals(this.isNullSafe) ? this : new LensSpec<>(read, write, SUPPLY_FALSE);
}
public LensSpec then(LensSpec sub) {
return LensSpec.of(
LensUtils.createSubRead (read, sub.read, isNullSafe),
LensUtils.createSubWrite(read, write, sub.write, isNullSafe),
isNullSafe);
}
public PrimitiveLensSpecs.IntegerLensSpecPrimitive thenPrimitive(PrimitiveLensSpecs.IntegerLensSpecPrimitive sub) {
ToIntFunction subReadInt = sub.getReadInt();
WriteLens.PrimitiveInt subWriteInt = sub.getWriteInt();
ToIntFunction readInt = LensUtils.createSubReadInt (read, subReadInt);
WriteLens.PrimitiveInt writeInt = LensUtils.createSubWriteInt(read, write, subWriteInt);
return LensSpec.ofPrimitive(readInt, writeInt);
}
public PrimitiveLensSpecs.LongLensSpecPrimitive thenPrimitive(PrimitiveLensSpecs.LongLensSpecPrimitive sub) {
ToLongFunction subReadLong = sub.getReadLong();
WriteLens.PrimitiveLong subWriteLong = sub.getWriteLong();
ToLongFunction readLong = LensUtils.createSubReadLong (read, subReadLong);
WriteLens.PrimitiveLong writeLong = LensUtils.createSubWriteLong(read, write, subWriteLong);
return LensSpec.ofPrimitive(readLong, writeLong);
}
public PrimitiveLensSpecs.DoubleLensSpecPrimitive thenPrimitive(PrimitiveLensSpecs.DoubleLensSpecPrimitive sub) {
ToDoubleFunction subReadDouble = sub.getReadDouble();
WriteLens.PrimitiveDouble subWriteDouble = sub.getWriteDouble();
ToDoubleFunction readDouble = LensUtils.createSubReadDouble (read, subReadDouble);
WriteLens.PrimitiveDouble writeDouble = LensUtils.createSubWriteDouble(read, write, subWriteDouble);
return LensSpec.ofPrimitive(readDouble, writeDouble);
}
public PrimitiveLensSpecs.BooleanLensSpecPrimitive thenPrimitive(PrimitiveLensSpecs.BooleanLensSpecPrimitive sub) {
Predicate subReadBoolean = sub.getReadBoolean();
WriteLens.PrimitiveBoolean subWriteBoolean = sub.getWriteBoolean();
Predicate readBoolean = LensUtils.createSubReadBoolean (read, subReadBoolean);
WriteLens.PrimitiveBoolean writeBoolean = LensUtils.createSubWriteBoolean(read, write, subWriteBoolean);
return LensSpec.ofPrimitive(readBoolean, writeBoolean);
}
}