Skip to content

Instantly share code, notes, and snippets.

@lovelock
Created February 8, 2021 14:19

Revisions

  1. lovelock created this gist Feb 8, 2021.
    81 changes: 81 additions & 0 deletions CompletableFutureTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    package fun.happyhacker;

    import lombok.Data;
    import org.apache.commons.codec.digest.DigestUtils;

    import java.util.*;
    import java.util.concurrent.TimeUnit;

    /**
    * @author Frost Wong <frostwong@hotmail.com>
    * @version 2021/2/8
    **/
    public class CompletableFutureTest {
    public static void main(String[] args) throws InterruptedException {
    CompletableFutureTest completableFutureTest = new CompletableFutureTest();
    completableFutureTest.compose();
    }

    private List<Target> compose() throws InterruptedException {
    List<Long> ids = genIds();
    Map<Long, String> contentMap = fun1(ids);
    Map<Long, List<String>> aMap = fun2(ids);
    Map<Long, String> bMap = hash(contentMap);

    List<Target> list = new ArrayList<>();
    for (Long id : ids) {
    Target target = new Target();
    target.setId(id);
    target.setContent(contentMap.get(id));
    target.setList(aMap.get(id));
    target.setMd5(bMap.get(id));
    }

    return list;
    }

    private List<Long> genIds() {
    return Collections.emptyList();
    }

    private Map<Long, String> fun1(List<Long> ids) throws InterruptedException {
    Map<Long, String> result = new HashMap<>();
    for (Long id : ids) {
    TimeUnit.MILLISECONDS.sleep(100);
    result.put(id, id.toString());
    }

    return result;
    }

    private Map<Long, List<String>> fun2(List<Long> ids) throws InterruptedException {
    Map<Long, List<String>> result = new HashMap<>();

    for (Long id : ids) {
    TimeUnit.MILLISECONDS.sleep(100);
    result.put(id, List.of(id.toString(), id.toString()));
    }

    return result;
    }

    private Map<Long, String> hash(Map<Long, String> map) throws InterruptedException {
    Map<Long, String> result = new HashMap<>();

    for (Map.Entry<Long, String> entry : map.entrySet()) {
    TimeUnit.MILLISECONDS.sleep(100);
    result.put(entry.getKey(), DigestUtils.md5Hex(entry.getValue()));
    }

    return result;
    }
    }


    @Data
    class Target {
    private Long id;
    private String content;
    private String md5;
    private List<String> list;
    }