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

functionalj.result.ResultMapAddOn Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// 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.Func1;
import functionalj.function.Func2;
import functionalj.function.Func3;
import functionalj.function.Func4;
import functionalj.function.Func5;
import functionalj.function.Func6;
import functionalj.map.FuncMap;
import functionalj.map.ImmutableMap;
import functionalj.tuple.Tuple2;
import functionalj.tuple.Tuple3;
import functionalj.tuple.Tuple4;
import functionalj.tuple.Tuple5;
import functionalj.tuple.Tuple6;
import lombok.val;

class ResultMapAddOnHelper {
    
    @SafeVarargs
    public static final  Result mapFirst(
            ResultMapAddOn                    result,
            Function ... mappers) {
        return result.map(d -> {
            Exception exception = null;
            boolean hasNull = false;
            for(val mapper : mappers) {
                try {
                    val res = mapper.apply(d);
                    if (res == null)
                         hasNull = true;
                    else return (T)res;
                } catch (Exception e) {
                    if (exception == null)
                        exception = e;
                }
            }
            if (hasNull)
                return (T)null;
            
            throw exception;
        });
    }
}

@SuppressWarnings("javadoc")
public interface ResultMapAddOn {
    
    public  Result map(Func1 mapper);
    
    public default  Result mapTo(Func1 mapper) {
        return map(mapper);
    }
    public default Result mapOnly(
            Predicate   checker,
            Func1 mapper) {
        return map(d -> checker.test(d) ? mapper.apply(d) : d);
    }
    public default  Result mapIf(
            Predicate   checker, 
            Function mapper, 
            Function elseMapper) {
        return map(d -> checker.test(d) ? mapper.apply(d) : elseMapper.apply(d));
    }
    
    // TODO - Find a better name  --- mapFirstOf, mapOneOf, tryMap
    public default  Result mapFirst(
            Function mapper1,
            Function mapper2) {
        return ResultMapAddOnHelper.mapFirst(this, mapper1, mapper2);
    }
    
    public default  Result mapFirst(
            Function mapper1,
            Function mapper2,
            Function mapper3) {
        return ResultMapAddOnHelper.mapFirst(this, mapper1, mapper2, mapper3);
    }
    
    public default  Result mapFirst(
            Function mapper1,
            Function mapper2,
            Function mapper3,
            Function mapper4) {
        return ResultMapAddOnHelper.mapFirst(this, mapper1, mapper2, mapper3, mapper4);
    }
    
    public default  Result mapFirst(
            Function mapper1,
            Function mapper2,
            Function mapper3,
            Function mapper4,
            Function mapper5) {
        return ResultMapAddOnHelper.mapFirst(this, mapper1, mapper2, mapper3, mapper4, mapper5);
    }
    
    public default  Result mapFirst(
            Function mapper1,
            Function mapper2,
            Function mapper3,
            Function mapper4,
            Function mapper5,
            Function mapper6) {
        return ResultMapAddOnHelper.mapFirst(this, mapper1, mapper2, mapper3, mapper4, mapper5, mapper6);
    }
    
    //== Map to tuple. ==
    // ++ Generated with: GeneratorFunctorMapToTupleToObject ++
    
    public default  
        Result> mapTuple(
                Func1 mapper1,
                Func1 mapper2) {
        return mapThen(mapper1, mapper2,
                   (v1, v2) -> Tuple2.of(v1, v2));
    }
    
    public default  
        Result> mapTuple(
                Func1 mapper1,
                Func1 mapper2,
                Func1 mapper3) {
        return mapThen(mapper1, mapper2, mapper3,
                   (v1, v2, v3) -> Tuple3.of(v1, v2, v3));
    }
    
    public default  
        Result> mapTuple(
                Func1 mapper1,
                Func1 mapper2,
                Func1 mapper3,
                Func1 mapper4) {
        return mapThen(mapper1, mapper2, mapper3, mapper4,
                   (v1, v2, v3, v4) -> Tuple4.of(v1, v2, v3, v4));
    }
    
    public default  
        Result> mapTuple(
                Func1 mapper1,
                Func1 mapper2,
                Func1 mapper3,
                Func1 mapper4,
                Func1 mapper5) {
        return mapThen(mapper1, mapper2, mapper3, mapper4, mapper5,
                   (v1, v2, v3, v4, v5) -> Tuple5.of(v1, v2, v3, v4, v5));
    }
    public default  
        Result> mapTuple(
                Func1 mapper1,
                Func1 mapper2,
                Func1 mapper3,
                Func1 mapper4,
                Func1 mapper5,
                Func1 mapper6) {
        return mapThen(mapper1, mapper2, mapper3, mapper4, mapper5, mapper6,
                   (v1, v2, v3, v4, v5, v6) -> Tuple6.of(v1, v2, v3, v4, v5, v6));
    }
    
    //-- Map and combine --
    
    public default  
        Result mapThen(
                Func1 mapper1,
                Func1 mapper2,
                Func2 function) {
        return map(each -> {
            val v1 = mapper1.apply(each);
            val v2 = mapper2.apply(each);
            val v  = function.apply(v1, v2);
            return v;
        });
    }
    public default  
        Result mapThen(
                Func1 mapper1,
                Func1 mapper2,
                Func1 mapper3,
                Func3 function) {
        return map(each -> {
            val v1 = mapper1.apply(each);
            val v2 = mapper2.apply(each);
            val v3 = mapper3.apply(each);
            val v  = function.apply(v1, v2, v3);
            return v;
        });
    }
    public default  
        Result mapThen(
                Func1 mapper1,
                Func1 mapper2,
                Func1 mapper3,
                Func1 mapper4,
                Func4 function) {
        return map(each -> {
            val v1 = mapper1.apply(each);
            val v2 = mapper2.apply(each);
            val v3 = mapper3.apply(each);
            val v4 = mapper4.apply(each);
            val v  = function.apply(v1, v2, v3, v4);
            return v;
        });
    }
    public default  
        Result mapThen(
                Func1 mapper1,
                Func1 mapper2,
                Func1 mapper3,
                Func1 mapper4,
                Func1 mapper5,
                Func5 function) {
        return map(each -> {
            val v1 = mapper1.apply(each);
            val v2 = mapper2.apply(each);
            val v3 = mapper3.apply(each);
            val v4 = mapper4.apply(each);
            val v5 = mapper5.apply(each);
            val v  = function.apply(v1, v2, v3, v4, v5);
            return v;
        });
    }
    public default  
        Result mapThen(
                Func1 mapper1,
                Func1 mapper2,
                Func1 mapper3,
                Func1 mapper4,
                Func1 mapper5,
                Func1 mapper6,
                Func6 function) {
        return map(each -> {
            val v1 = mapper1.apply(each);
            val v2 = mapper2.apply(each);
            val v3 = mapper3.apply(each);
            val v4 = mapper4.apply(each);
            val v5 = mapper5.apply(each);
            val v6 = mapper6.apply(each);
            val v  = function.apply(v1, v2, v3, v4, v5, v6);
            return v;
        });
    }
    
    // -- Generated with: GeneratorFunctorMapToTupleToObject --
    
    public default  Result> mapToMap(
            KEY key, Func1 mapper) {
        return map(data -> ImmutableMap.of(key, mapper.apply(data)));
    }
    
    public default  Result> mapToMap(
            KEY key1, Func1 mapper1,
            KEY key2, Func1 mapper2) {
        return map(data -> ImmutableMap.of(
                key1, mapper1.apply(data),
                key2, mapper2.apply(data)));
    }
    
    public default  Result> mapToMap(
            KEY key1, Func1 mapper1,
            KEY key2, Func1 mapper2,
            KEY key3, Func1 mapper3) {
        return map(data -> ImmutableMap.of(
                key1, mapper1.apply(data),
                key2, mapper2.apply(data),
                key3, mapper3.apply(data)));
    }
    
    public default  Result> mapToMap(
            KEY key1, Func1 mapper1,
            KEY key2, Func1 mapper2,
            KEY key3, Func1 mapper3,
            KEY key4, Func1 mapper4) {
        return map(data -> ImmutableMap.of(
                key1, mapper1.apply(data),
                key2, mapper2.apply(data),
                key3, mapper3.apply(data),
                key4, mapper4.apply(data)));
    }
    
    public default  Result> mapToMap(
            KEY key1, Func1 mapper1,
            KEY key2, Func1 mapper2,
            KEY key3, Func1 mapper3,
            KEY key4, Func1 mapper4,
            KEY key5, Func1 mapper5) {
        return map(data -> ImmutableMap.of(
                key1, mapper1.apply(data),
                key2, mapper2.apply(data),
                key3, mapper3.apply(data),
                key4, mapper4.apply(data),
                key5, mapper5.apply(data)));
    }
    
    public default  Result> mapToMap(
            KEY key1, Func1 mapper1,
            KEY key2, Func1 mapper2,
            KEY key3, Func1 mapper3,
            KEY key4, Func1 mapper4,
            KEY key5, Func1 mapper5,
            KEY key6, Func1 mapper6) {
        return map(data -> ImmutableMap.of(
                key1, mapper1.apply(data),
                key2, mapper2.apply(data),
                key3, mapper3.apply(data),
                key4, mapper4.apply(data),
                key5, mapper5.apply(data),
                key6, mapper6.apply(data)));
    }
    
    public default  Result> mapToMap(
            KEY key1, Func1 mapper1,
            KEY key2, Func1 mapper2,
            KEY key3, Func1 mapper3,
            KEY key4, Func1 mapper4,
            KEY key5, Func1 mapper5,
            KEY key6, Func1 mapper6,
            KEY key7, Func1 mapper7) {
        return map(data -> ImmutableMap.of(
                key1, mapper1.apply(data),
                key2, mapper2.apply(data),
                key3, mapper3.apply(data),
                key4, mapper4.apply(data),
                key5, mapper5.apply(data),
                key6, mapper6.apply(data),
                key7, mapper7.apply(data)));
    }
    
    public default  Result> mapToMap(
            KEY key1, Func1 mapper1,
            KEY key2, Func1 mapper2,
            KEY key3, Func1 mapper3,
            KEY key4, Func1 mapper4,
            KEY key5, Func1 mapper5,
            KEY key6, Func1 mapper6,
            KEY key7, Func1 mapper7,
            KEY key8, Func1 mapper8) {
        return map(data -> ImmutableMap.of(
                key1, mapper1.apply(data),
                key2, mapper2.apply(data),
                key3, mapper3.apply(data),
                key4, mapper4.apply(data),
                key5, mapper5.apply(data),
                key6, mapper6.apply(data),
                key7, mapper7.apply(data),
                key8, mapper8.apply(data)));
    }
    
    public default  Result> mapToMap(
            KEY key1, Func1 mapper1,
            KEY key2, Func1 mapper2,
            KEY key3, Func1 mapper3,
            KEY key4, Func1 mapper4,
            KEY key5, Func1 mapper5,
            KEY key6, Func1 mapper6,
            KEY key7, Func1 mapper7,
            KEY key8, Func1 mapper8,
            KEY key9, Func1 mapper9) {
        return map(data -> ImmutableMap.of(
                key1, mapper1.apply(data),
                key2, mapper2.apply(data),
                key3, mapper3.apply(data),
                key4, mapper4.apply(data),
                key5, mapper5.apply(data),
                key6, mapper6.apply(data),
                key7, mapper7.apply(data),
                key8, mapper8.apply(data),
                key9, mapper9.apply(data)));
    }
    
    public default  Result> mapToMap(
            KEY key1, Func1 mapper1,
            KEY key2, Func1 mapper2,
            KEY key3, Func1 mapper3,
            KEY key4, Func1 mapper4,
            KEY key5, Func1 mapper5,
            KEY key6, Func1 mapper6,
            KEY key7, Func1 mapper7,
            KEY key8, Func1 mapper8,
            KEY key9, Func1 mapper9,
            KEY key10, Func1 mapper10) {
        return map(data -> ImmutableMap.of(
                key1, mapper1.apply(data),
                key2, mapper2.apply(data),
                key3, mapper3.apply(data),
                key4, mapper4.apply(data),
                key5, mapper5.apply(data),
                key6, mapper6.apply(data),
                key7, mapper7.apply(data),
                key8, mapper8.apply(data),
                key9, mapper9.apply(data),
                key10, mapper10.apply(data)));
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy