All Downloads are FREE. Search and download functionalities are using the official Maven repository.

functionalj.lens.core.LensSpec Maven / Gradle / Ivy

// ============================================================================
// 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.core;

import java.util.function.BooleanSupplier;
import java.util.function.Function;

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy