functionalj.result.ResultChainAddOn 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.result;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import functionalj.function.Func0;
import functionalj.function.Func3;
import functionalj.function.Func4;
import functionalj.function.Func5;
import functionalj.function.Func6;
import functionalj.tuple.Tuple;
import functionalj.tuple.Tuple2;
import functionalj.tuple.Tuple3;
import functionalj.tuple.Tuple4;
import functionalj.tuple.Tuple5;
import functionalj.tuple.Tuple6;
import lombok.val;
class ResultChainAddOnHelper {
@SafeVarargs
public static final Result chainAny(
ResultChainAddOn result,
Function super D, Result> ... mappers) {
return result.chain(d -> {
Result exception = null;
boolean hasNull = false;
for(Function super D, Result> mapper : mappers) {
try {
val res = mapper.apply(d);
if (res.isNull())
hasNull = true;
else if (res.isException()) {
exception = res;
continue;
}
else return (Result)res;
} catch (Exception e) {
if (exception == null)
exception = Result.ofException(e);
}
}
if (hasNull)
return Result.ofNull();
return exception;
});
}
}
public interface ResultChainAddOn {
/**
* Returns this object as a result.
*
* @return this object as a result.
**/
public Result asResult();
public Result flatMap(Function super DATA, ? extends Result> mapper);
public default Result chain(Function super DATA, ? extends Result> mapper) {
return flatMap(mapper);
}
public default Result chainOnly(Predicate super DATA> checker, Function super DATA, Result> mapper) {
return chain(d -> checker.test(d) ? mapper.apply(d) : asResult());
}
public default Result chainIf(
Predicate super DATA> checker,
Function super DATA, Result> mapper,
Function super DATA, Result> elseMapper) {
return chain(d -> checker.test(d) ? mapper.apply(d) : elseMapper.apply(d));
}
public default Result chainAny(
Function super DATA, Result> mapper1,
Function super DATA, Result> mapper2) {
return ResultChainAddOnHelper.chainAny(this, mapper1, mapper2);
}
public default Result chainAny(
Function super DATA, Result> mapper1,
Function super DATA, Result> mapper2,
Function super DATA, Result> mapper3) {
return ResultChainAddOnHelper.chainAny(this, mapper1, mapper2, mapper3);
}
public default Result chainAny(
Function super DATA, Result> mapper1,
Function super DATA, Result> mapper2,
Function super DATA, Result> mapper3,
Function super DATA, Result> mapper4) {
return ResultChainAddOnHelper.chainAny(this, mapper1, mapper2, mapper3, mapper4);
}
public default Result chainAny(
Function super DATA, Result> mapper1,
Function super DATA, Result> mapper2,
Function super DATA, Result> mapper3,
Function super DATA, Result> mapper4,
Function super DATA, Result> mapper5) {
return ResultChainAddOnHelper.chainAny(this, mapper1, mapper2, mapper3, mapper4, mapper5);
}
public default Result chainAny(
Function super DATA, Result> mapper1,
Function super DATA, Result> mapper2,
Function super DATA, Result> mapper3,
Function super DATA, Result> mapper4,
Function super DATA, Result> mapper5,
Function super DATA, Result> mapper6) {
return ResultChainAddOnHelper.chainAny(this, mapper1, mapper2, mapper3, mapper4, mapper5, mapper6);
}
public default
Result> chainTuple(
Function super DATA, ? extends Result> mapper1,
Function super DATA, ? extends Result> mapper2) {
return chainThen(mapper1, mapper2, (v1, v2)->{
return Tuple.of(v1, v2);
});
}
public default
Result> chainTuple(
Function super DATA, ? extends Result> mapper1,
Function super DATA, ? extends Result> mapper2,
Function super DATA, ? extends Result> mapper3) {
return chainThen(mapper1, mapper2, mapper3, (v1, v2, v3)->{
return Tuple.of(v1, v2, v3);
});
}
public default
Result> chainTuple(
Function super DATA, ? extends Result> mapper1,
Function super DATA, ? extends Result> mapper2,
Function super DATA, ? extends Result> mapper3,
Function super DATA, ? extends Result> mapper4) {
return chainThen(mapper1, mapper2, mapper3, mapper4, (v1, v2, v3, v4)->{
return Tuple.of(v1, v2, v3, v4);
});
}
public default
Result> chainTuple(
Function super DATA, ? extends Result> mapper1,
Function super DATA, ? extends Result> mapper2,
Function super DATA, ? extends Result> mapper3,
Function super DATA, ? extends Result> mapper4,
Function super DATA, ? extends Result> mapper5) {
return chainThen(mapper1, mapper2, mapper3, mapper4, mapper5, (v1, v2, v3, v4, v5)->{
return Tuple.of(v1, v2, v3, v4, v5);
});
}
public default
Result> chainTuple(
Function super DATA, ? extends Result> mapper1,
Function super DATA, ? extends Result> mapper2,
Function super DATA, ? extends Result> mapper3,
Function super DATA, ? extends Result> mapper4,
Function super DATA, ? extends Result> mapper5,
Function super DATA, ? extends Result> mapper6) {
return chainThen(mapper1, mapper2, mapper3, mapper4, mapper5, mapper6, (v1, v2, v3, v4, v5, v6)->{
return Tuple.of(v1, v2, v3, v4, v5, v6);
});
}
public default
Result chainThen(
Function super DATA, ? extends Result> mapper1,
Function super DATA, ? extends Result> mapper2,
BiFunction mapper) {
return Result.of(Func0.of(()->{
val value = asResult().orThrow();
val result1 = mapper1.apply(value);
val value1 = result1.orThrow();
val result2 = mapper2.apply(value);
val value2 = result2.orThrow();
val target = mapper.apply(value1, value2);
return target;
}));
}
public default
Result chainThen(
Function super DATA, ? extends Result> mapper1,
Function super DATA, ? extends Result> mapper2,
Function super DATA, ? extends Result> mapper3,
Func3 mapper) {
return Result.of(Func0.of(()->{
val value = asResult().orThrow();
val result1 = mapper1.apply(value);
val value1 = result1.orThrow();
val result2 = mapper2.apply(value);
val value2 = result2.orThrow();
val result3 = mapper3.apply(value);
val value3 = result3.orThrow();
val target = mapper.apply(value1, value2, value3);
return target;
}));
}
public default
Result chainThen(
Function super DATA, ? extends Result> mapper1,
Function super DATA, ? extends Result> mapper2,
Function super DATA, ? extends Result> mapper3,
Function super DATA, ? extends Result> mapper4,
Func4 mapper) {
return Result.of(Func0.of(()->{
val value = asResult().orThrow();
val result1 = mapper1.apply(value);
val value1 = result1.orThrow();
val result2 = mapper2.apply(value);
val value2 = result2.orThrow();
val result3 = mapper3.apply(value);
val value3 = result3.orThrow();
val result4 = mapper4.apply(value);
val value4 = result4.orThrow();
val target = mapper.apply(value1, value2, value3, value4);
return target;
}));
}
public default
Result chainThen(
Function super DATA, ? extends Result> mapper1,
Function super DATA, ? extends Result> mapper2,
Function super DATA, ? extends Result> mapper3,
Function super DATA, ? extends Result> mapper4,
Function super DATA, ? extends Result> mapper5,
Func5 mapper) {
return Result.of(Func0.of(()->{
val value = asResult().orThrow();
val result1 = mapper1.apply(value);
val value1 = result1.orThrow();
val result2 = mapper2.apply(value);
val value2 = result2.orThrow();
val result3 = mapper3.apply(value);
val value3 = result3.orThrow();
val result4 = mapper4.apply(value);
val value4 = result4.orThrow();
val result5 = mapper5.apply(value);
val value5 = result5.orThrow();
val target = mapper.apply(value1, value2, value3, value4, value5);
return target;
}));
}
public default
Result chainThen(
Function super DATA, ? extends Result> mapper1,
Function super DATA, ? extends Result> mapper2,
Function super DATA, ? extends Result> mapper3,
Function super DATA, ? extends Result> mapper4,
Function super DATA, ? extends Result> mapper5,
Function super DATA, ? extends Result> mapper6,
Func6 mapper) {
return Result.of(Func0.of(()->{
val value = asResult().orThrow();
val result1 = mapper1.apply(value);
val value1 = result1.orThrow();
val result2 = mapper2.apply(value);
val value2 = result2.orThrow();
val result3 = mapper3.apply(value);
val value3 = result3.orThrow();
val result4 = mapper4.apply(value);
val value4 = result4.orThrow();
val result5 = mapper5.apply(value);
val value5 = result5.orThrow();
val result6 = mapper6.apply(value);
val value6 = result6.orThrow();
val target = mapper.apply(value1, value2, value3, value4, value5, value6);
return target;
}));
}
}