Getting Started

Installation

Add the Pipe4j library to your project by including the following dependency in your project:

Maven

<dependency>
    <groupId>io.igventurelli</groupId>
    <artifactId>pipe4j</artifactId>
    <version>1.0.24</version>
</dependency>

Gradle

implementation 'io.igventurelli:pipe4j:1.0.24'

Usage

Here's a basic example demonstrating how to use Pipe4j:

package com.example.demo;

import io.igventurelli.Pipe4j;

import java.util.Random;

public class DemoApplication {

    public static void main(String[] args) {
        var random = new Random().nextInt(100);
        
        var pipe = Pipe4j
            .from(10)
            .perform(input -> input * 2) // input = 10 - from the previous step
            .perform(res -> res / 4) // res = 20 - result of the multiply operation from the previous step
            .perform(res -> new SomeService().shallIThrowException(random % 2 == 0, res), e -> {
                System.out.println("An exception has been thrown: [" + e.getMessage() + "] - Providing fallback value:");
                return 99; // default value in case of exception
            })
            .get();
            
        System.out.println(pipe);
    }

    private static class SomeService {
        public String shallIThrowException(boolean shallI, Integer operand) throws Exception {
            if (shallI) {
                throw new Exception("A checked exception has been thrown");
            }
            return "It works, your result is: " + operand;
        }
    }
}

Last updated