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

io.github.laskowski.shell.output.DefaultStringSubscriber Maven / Gradle / Ivy

Go to download

Library to launch .bat and .sh scripts of your choice with different configurations. Allows you to read tasks and services output from terminal

There is a newer version: 2.0.6.1
Show newest version
package io.github.laskowski.shell.output;

import io.github.laskowski.shell.exceptions.ErrorDetectedException;
import io.github.laskowski.shell.output.messages.ErrorDetectionStrategy;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Flow;
import java.util.stream.Collectors;

public class DefaultStringSubscriber implements StringSubscriber {
    protected Flow.Subscription subscription;
    protected final List lines = new ArrayList<>();
    private final ErrorDetectionStrategy errorDetectionStrategy;

    public DefaultStringSubscriber(@Nullable ErrorDetectionStrategy errorDetectionStrategy) {
        this.errorDetectionStrategy = errorDetectionStrategy;
    }

    @Override
    public void onSubscribe(Flow.Subscription subscription) {
        this.subscription = subscription;
        this.subscription.request(100);
    }

    @Override
    public void onNext(String item) {
        System.out.println(item);

        lines.add(item);
        this.subscription.request(100);
    }

    @Override
    public void onError(Throwable throwable) {
        throwable.printStackTrace();
    }

    @Override
    public void onComplete() {}

    @Override
    public List getLines() throws ErrorDetectedException {
        List lineList = new ArrayList<>(lines).stream().filter(Objects::nonNull).collect(Collectors.toList());

        if (errorDetectionStrategy != null) {
            for (String line : lineList) {
                if (errorDetectionStrategy.test(line)) {
                    throw new ErrorDetectedException("Error Detected!\nLine [%s]", line);
                }
            }
        }

        return lineList;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy