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

functionalj.result.ResultChainAddOn 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.result;

import java.util.function.Function;
import java.util.function.Predicate;

import functionalj.function.Func0;
import functionalj.function.Func1;
import functionalj.function.Func2;
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> ... mappers) {
        return result.chain(d -> {
            Result exception = null;
            boolean hasNull = false;
            for(Function> 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(Func1> mapper);
    
    public default  Result chain(Func1> mapper) {
        return flatMap(mapper);
    }
    
    public default Result chainOnly(Predicate checker, Func1> mapper) {
        return chain(d -> checker.test(d) ? mapper.apply(d) : asResult());
    }
    
    public default  Result chainIf(
            Predicate checker, 
            Func1> mapper, 
            Func1> elseMapper) {
        return chain(d -> checker.test(d) ? mapper.apply(d) : elseMapper.apply(d));
    }
    
    public default  Result chainAny(
            Function> mapper1,
            Function> mapper2) {
        return ResultChainAddOnHelper.chainAny(this, mapper1, mapper2);
    }
    
    public default  Result chainAny(
            Function> mapper1,
            Function> mapper2,
            Function> mapper3) {
        return ResultChainAddOnHelper.chainAny(this, mapper1, mapper2, mapper3);
    }
    
    public default  Result chainAny(
            Function> mapper1,
            Function> mapper2,
            Function> mapper3,
            Function> mapper4) {
        return ResultChainAddOnHelper.chainAny(this, mapper1, mapper2, mapper3, mapper4);
    }
    
    public default  Result chainAny(
            Function> mapper1,
            Function> mapper2,
            Function> mapper3,
            Function> mapper4,
            Function> mapper5) {
        return ResultChainAddOnHelper.chainAny(this, mapper1, mapper2, mapper3, mapper4, mapper5);
    }
    
    public default  Result chainAny(
            Function> mapper1,
            Function> mapper2,
            Function> mapper3,
            Function> mapper4,
            Function> mapper5,
            Function> mapper6) {
        return ResultChainAddOnHelper.chainAny(this, mapper1, mapper2, mapper3, mapper4, mapper5, mapper6);
    }
    
    public default  
        Result> chainTuple(
            Func1> mapper1,
            Func1> mapper2) {
       return chainThen(mapper1, mapper2, (v1, v2)->{
          return Tuple.of(v1, v2);
       });
    }
    public default  
        Result> chainTuple(
            Func1> mapper1,
            Func1> mapper2,
            Func1> mapper3) {
       return chainThen(mapper1, mapper2, mapper3, (v1, v2, v3)->{
          return Tuple.of(v1, v2, v3);
       });
    }
    public default  
       Result> chainTuple(
            Func1> mapper1,
            Func1> mapper2,
            Func1> mapper3,
            Func1> mapper4) {
       return chainThen(mapper1, mapper2, mapper3, mapper4, (v1, v2, v3, v4)->{
          return Tuple.of(v1, v2, v3, v4);
       });
    }
    public default  
       Result> chainTuple(
            Func1> mapper1,
            Func1> mapper2,
            Func1> mapper3,
            Func1> mapper4,
            Func1> 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(
            Func1> mapper1,
            Func1> mapper2,
            Func1> mapper3,
            Func1> mapper4,
            Func1> mapper5,
            Func1> 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(
            Func1> mapper1,
            Func1> mapper2,
            Func2 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(
            Func1> mapper1,
            Func1> mapper2,
            Func1> 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(
            Func1> mapper1,
            Func1> mapper2,
            Func1> mapper3,
            Func1> 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(
            Func1> mapper1,
            Func1> mapper2,
            Func1> mapper3,
            Func1> mapper4,
            Func1> 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(
            Func1> mapper1,
            Func1> mapper2,
            Func1> mapper3,
            Func1> mapper4,
            Func1> mapper5,
            Func1> 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;
        }));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy