Untitled

mail@pastecode.io avatar
unknown
java
25 days ago
3.4 kB
4
Indexable
Never
package threadpool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

public class StringTransformer {

    private List<String> baseStrings;

    public StringTransformer (List<String> baseStrings) {
        this.baseStrings = baseStrings;
    }

    private String trasformSingleString(String baseString, List<StringFunction> functions){
        String res = baseString;
        for (StringFunction func : functions) {
            res = func.transform(res);
        }
        return res;
    }

    public List<String> transform(List<StringFunction> functions) throws InterruptedException {
        ThreadPoolExecutor poolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);

        List<String> res = new ArrayList<>();

        this.baseStrings.forEach(srt -> poolExecutor.submit(() -> {
            String currTransformed = this.trasformSingleString(srt, functions);
            res.add(currTransformed);
        }));

        while(res.size() < this.baseStrings.size()){
            System.out.println("res.size = ["+res.size()+"], this.baseStrings.size()=[" + this.baseStrings.size()+"]");
            Thread.sleep(1000);
        }

        return res;
    }

    public List<String> transformWithSmartWait(List<StringFunction> functions) throws InterruptedException, ExecutionException {
        ThreadPoolExecutor poolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
        CompletionService<String> completionService = new ExecutorCompletionService<String>(poolExecutor);

        List<String> res = new ArrayList<>();

        this.baseStrings.forEach(srt -> completionService.submit(() -> {
            String currTransformed = trasformSingleString(srt, functions);
            return currTransformed;
        }
        ));

        while(res.size() < this.baseStrings.size()){
            System.out.println("res.size = ["+res.size()+"], this.baseStrings.size()=[" + this.baseStrings.size()+"]");
            Future<String> future = completionService.take();
            res.add(future.get());
        }

        return res;
    }

    public interface StringFunction{
        public String transform(String str);
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        System.out.println("starts");

        List<String> baseStrings = Arrays.asList("one", "two", "Three", "FouR","fiVe");

        List<StringFunction> functions = new ArrayList<>();

        StringFunction toLowerCase = str -> str.substring(0, 1).concat(str.substring(1).toLowerCase());
        functions.add(toLowerCase);

        StringFunction firstCharToUpperCase = new StringFunction() {
            @Override
            public String transform(String str) {
                return str.substring(0, 1).toUpperCase() + str.substring(1);
            }
        };
        functions.add(firstCharToUpperCase);

        StringFunction concatExclamationMark = new StringFunction() {
            @Override
            public String transform(String str) {
                return str.concat("!");
            }
        };
        functions.add(concatExclamationMark);

        StringTransformer stringTransformer = new StringTransformer(baseStrings);

        List<String> res = stringTransformer.transformWithSmartWait(functions);

        System.out.println("res:");
        res.forEach(System.out::println);

    }

}
Leave a Comment