functionalj.lens.lenses.Tuple2Lens 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.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import functionalj.lens.core.AccessParameterized2;
import functionalj.lens.core.LensSpec;
import functionalj.lens.core.LensSpecParameterized2;
import functionalj.lens.core.LensType;
import functionalj.lens.core.LensUtils;
import functionalj.lens.core.WriteLens;
import functionalj.tuple.ImmutableTuple2;
import functionalj.tuple.Tuple2;
import lombok.val;
@FunctionalInterface
public interface Tuple2Lens, T2LENS extends AnyLens>
extends
ObjectLens>,
Tuple2Access {
public static , _2ACCESS extends AnyAccess,
_1LENS extends AnyLens, _2LENS extends AnyLens>
Tuple2Lens of(
Function> read,
WriteLens> write,
LensType _1Type,
LensType _2Type) {
val spec = new LensSpecParameterized2, _1, _2, _1LENS, _2LENS>() {
@Override public LensSpec> getSpec() { return LensSpec.of(read, write); }
@Override public _1LENS createSubLens1(LensSpec subSpec) { return _1Type.newLens(subSpec); }
@Override public _2LENS createSubLens2(LensSpec subSpec) { return _2Type.newLens(subSpec); }
};
return ()->spec;
}
public LensSpecParameterized2, T1, T2, T1LENS, T2LENS> lensSpecParameterized2();
@Override
public default AccessParameterized2, T1, T2, T1LENS, T2LENS> accessParameterized2() {
return lensSpecParameterized2();
}
@Override
public default LensSpec> lensSpec() {
return lensSpecParameterized2().getSpec();
}
@Override
default Tuple2 applyUnsafe(HOST host) throws Exception {
return Tuple2Access.super.applyUnsafe(host);
}
public default T1LENS _1() {
WriteLens, T1> write = (tuple, _1) -> new ImmutableTuple2(_1, tuple._2());
Function, T1> read = Tuple2::_1;
return LensUtils.createSubLens(this, read, write, lensSpecParameterized2()::createSubLens1);
}
public default T2LENS _2() {
WriteLens, T2> write = (tuple, _2) -> new ImmutableTuple2(tuple._1(), _2);
Function, T2> read = Tuple2::_2;
return LensUtils.createSubLens(this, read, write, lensSpecParameterized2()::createSubLens2);
}
public default T1LENS first() {
return _1();
}
public default T2LENS second() {
return _2();
}
public default Function change1To(T1 _1value) {
return host -> {
Tuple2 newTuple = new ImmutableTuple2<>(_1value, apply(host)._2());
return apply(host, newTuple);
};
}
public default Function change2To(T2 _2value) {
return host -> {
Tuple2 newTuple = new ImmutableTuple2<>(apply(host)._1(), _2value);
return apply(host, newTuple);
};
}
public default Function change1By(Supplier _1valueSupplier) {
return host -> {
val newTuple = new ImmutableTuple2<>(_1valueSupplier.get(), apply(host)._2());
return apply(host, newTuple);
};
}
public default Function change2By(Supplier _2valueSupplier) {
return host -> {
val newTuple = new ImmutableTuple2<>(apply(host)._1(), _2valueSupplier.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 ImmutableTuple2<>(new_1, oldTuple._2());
return apply(host, newTuple);
};
}
public default Function change2By(Function _2valueTransformer) {
return host -> {
val oldTuple = apply(host);
val new_2 = _2valueTransformer.apply(oldTuple._2());
val newTuple = new ImmutableTuple2<>(oldTuple._1(), new_2);
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 ImmutableTuple2<>(new_1, oldTuple._2());
return apply(host, newTuple);
};
}
public default Function change2By(BiFunction _2valueTransformer) {
return host -> {
val oldTuple = apply(host);
val new_2 = _2valueTransformer.apply(oldTuple._1(), oldTuple._2());
val newTuple = new ImmutableTuple2<>(oldTuple._1(), new_2);
return apply(host, newTuple);
};
}
public default Function changeFirstTo(T1 _1value) {
return change1To(_1value);
}
public default Function changeSecondTo(T2 _2value) {
return change2To(_2value);
}
public default Function changeFirstBy(Supplier _1valueSupplier) {
return change1By(_1valueSupplier);
}
public default Function changeSecondBy(Supplier _2valueSupplier) {
return change2By(_2valueSupplier);
}
public default Function changeFirstBy(Function _1valueTransformer) {
return change1By(_1valueTransformer);
}
public default Function changeSecondBy(Function _2valueTransformer) {
return change2By(_2valueTransformer);
}
public default Function changeFirstBy(BiFunction _1valueTransformer) {
return change1By(_1valueTransformer);
}
public default Function changeSecondBy(BiFunction _2valueTransformer) {
return change2By(_2valueTransformer);
}
}