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

functionalj.tuple.IntTuple2Lens Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// 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.tuple;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;

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 functionalj.lens.lenses.AnyLens;
import functionalj.lens.lenses.IntegerLens;
import functionalj.lens.lenses.ObjectLens;
import lombok.val;



public interface IntTuple2Lens>
        extends
            ObjectLens>,
            IntTuple2Access {

    public static >
            IntTuple2Lens of(
                    Function>      read,
                    WriteLens>      write,
                    Function, T2LENS> valueLensCreator) {
        val spec = new LensSpecParameterized, T2, T2LENS>() {
            @Override public LensSpec> getSpec()          { return LensSpec.of(read, write);        }
            @Override public T2LENS createSubLens(LensSpec subSpec) { return valueLensCreator.apply(subSpec); }
        };    
        return ()->spec;
    }

    public LensSpecParameterized, T2, T2LENS> lensSpecParameterized();
    
    @Override
    public default AccessParameterized, T2, T2LENS> accessParameterized() {
        return lensSpecParameterized();
    }

    @Override
    public default LensSpec> lensSpec() {
        return lensSpecParameterized().getSpec();
    }
    
    @Override
    default IntTuple2 applyUnsafe(HOST host) throws Exception {
        return IntTuple2Access.super.apply(host);
    }
    
    public default IntegerLens _1() {
        WriteLens, Integer> write = (tuple, _1) -> new IntTuple2(_1, tuple._2);
        Function , Integer> read  = IntTuple2::_1;
        return LensUtils.createSubLens(this, read, write, IntegerLens::of);
    }
    
    public default T2LENS T2() {
        val write = (WriteLens, T2>)((tuple, T2) -> new IntTuple2(tuple._1, T2));
        val read  = (Function , T2>)IntTuple2::_2;
        return LensUtils.createSubLens(this, read, write, lensSpecParameterized()::createSubLens);
    }
    
    public default Function change1To(int _1value) {
        return host -> {
            val newTuple = new IntTuple2<>(_1value, apply(host)._2);
            return apply(host, newTuple);
        };
    }
    
    public default Function change2To(T2 T2value) {
        return host -> {
            val newTuple = new IntTuple2<>(apply(host)._1, T2value);
            return apply(host, newTuple);
        };
    }
    
    public default Function change1By(Supplier _1valueSupplier) {
        return host -> {
            val newTuple = new IntTuple2<>(_1valueSupplier.get(), apply(host)._2);
            return apply(host, newTuple);
        };
    }
    public default Function change1By(IntSupplier _1valueSupplier) {
        return host -> {
            val newTuple = new IntTuple2<>(_1valueSupplier.getAsInt(), apply(host)._2);
            return apply(host, newTuple);
        };
    }
    
    public default Function change2By(Supplier T2valueSupplier) {
        return host -> {
            val newTuple = new IntTuple2<>(apply(host)._1, T2valueSupplier.get());
            return apply(host, newTuple);
        };
    }
    
    public default Function change1By(Function _1valueTransformer) {
        return host -> {
            val oldTuple = apply(host);
            val new_1    = _1valueTransformer.apply(oldTuple._1);
            val newTuple = new IntTuple2<>(new_1, oldTuple._2);
            return apply(host, newTuple);
        };
    }
    public default Function change1By(IntFunction _1valueTransformer) {
        return host -> {
            val oldTuple = apply(host);
            val new_1    = _1valueTransformer.apply(oldTuple._1);
            val newTuple = new IntTuple2<>(new_1, oldTuple._2);
            return apply(host, newTuple);
        };
    }
    public default Function change1By(ToIntFunction _1valueTransformer) {
        return host -> {
            val oldTuple = apply(host);
            val new_1    = _1valueTransformer.applyAsInt(oldTuple._1);
            val newTuple = new IntTuple2<>(new_1, oldTuple._2);
            return apply(host, newTuple);
        };
    }
    
    public default Function change2By(Function T2valueTransformer) {
        return host -> {
            val oldTuple = apply(host);
            val newT2 = T2valueTransformer.apply(oldTuple._2);
            val newTuple = new IntTuple2<>(oldTuple._1, newT2);
            return apply(host, newTuple);
        };
    }
    
    public default Function change1By(BiFunction _1valueTransformer) {
        return host -> {
            val oldTuple = apply(host);
            val new_1    = _1valueTransformer.apply(oldTuple._1, oldTuple._2);
            val newTuple = new IntTuple2<>(new_1, oldTuple._2);
            return apply(host, newTuple);
        };
    }
    
    public default Function change2By(BiFunction T2valueTransformer) {
        return host -> {
            val oldTuple = apply(host);
            val newT2    = T2valueTransformer.apply(oldTuple._1, oldTuple._2);
            val newTuple = new IntTuple2<>(oldTuple._1, newT2);
            return apply(host, newTuple);
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy