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

functionalj.stream.StreamableAddtionalOperators 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.stream;

import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import functionalj.result.Result;
import functionalj.tuple.Tuple2;

public interface StreamableAddtionalOperators {
    
    public  Streamable deriveWith(
            Function, Stream> action);
    
    
    //--map with condition --
    
    public default Streamable mapOnly(
            Predicate      checker, 
            Function mapper) {
        return deriveWith(stream -> {
                return StreamPlus
                        .from(stream)
                        .mapOnly(checker, mapper);
            });
    }
    
    public default  Streamable mapIf(
            Predicate   checker, 
            Function mapper, 
            Function elseMapper) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .mapIf(checker, mapper, elseMapper);
        });
    }
    
    //-- mapWithIndex --
    
    public default Streamable> mapWithIndex() {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .mapWithIndex();
        });
    }
    
    public default  Streamable mapWithIndex(
            BiFunction mapper) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .mapWithIndex(mapper);
        });
    }
    
    public default  Streamable mapWithIndex(
                Function       mapper1,
                BiFunction mapper) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .mapWithIndex(mapper1, mapper);
        });
    }
    
    //-- mapWithPrev --
    
    public default  Streamable mapWithPrev(
            BiFunction, ? super DATA, ? extends TARGET> mapper) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .mapWithPrev(mapper);
        });
    }
    
    //-- Filter --
    
    public default Streamable, ? super DATA>> mapWithPrev() {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .mapWithPrev();
        });
    }
    
    public default Streamable filterIn(
            Collection collection) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .filterIn(collection);
        });
    }
    
    public default Streamable exclude(
            Predicate predicate) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .exclude(predicate);
        });
    }
    
    public default Streamable excludeIn(
            Collection collection) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .excludeIn(collection);
        });
    }
    
    public default  Streamable filter(
            Class clzz) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .filter(clzz);
        });
    }
    
    public default  Streamable filter(
            Class             clzz, 
            Predicate theCondition) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .filter(clzz, theCondition);
        });
    }
    
    public default  Streamable filter(
            Function mapper, 
            Predicate      theCondition) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .filter(mapper, theCondition);
        });
    }
    
    public default Streamable filterWithIndex(
            BiFunction predicate) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .filterWithIndex(predicate);
        });
    }
    
    //-- Peek --
    
    public default  Streamable peek(
            Class            clzz, 
            Consumer theConsumer) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .peek(clzz, theConsumer);
        });
    }
    public default Streamable peek(
            Predicate selector, 
            Consumer  theConsumer) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .peek(selector, theConsumer);
        });
    }
    public default  Streamable peek(
            Function mapper, 
            Consumer       theConsumer) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .peek(mapper, theConsumer);
        });
    }
    
    public default  Streamable peek(
            Function mapper, 
            Predicate      selector, 
            Consumer       theConsumer) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .peek(mapper, selector, theConsumer);
        });
    }
    
    //-- FlatMap --
    
    public default Streamable flatMapOnly(
            Predicate                            checker, 
            Function> mapper) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .flatMapOnly(
                            checker, 
                            d -> mapper.apply(d).stream());
        });
    }
    public default  Streamable flatMapIf(
            Predicate                         checker, 
            Function> mapper, 
            Function> elseMapper) {
        return deriveWith(stream -> {
            return StreamPlus
                    .from(stream)
                    .flatMapIf(
                            checker, 
                            d -> mapper    .apply(d).stream(), 
                            d -> elseMapper.apply(d).stream());
        });
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy