package com.tzld.piaoquan.growth.common.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author： TanJingyu
 * @create：2023-06-12 13:28:18
 **/
public class MapBuilder {

    private MapBuilder() {
    }

    public static <K, V> Builder<K, V> builder() {
        return new Builder<>(null);
    }

    public static <K, V> Builder<K, V> builder(Map<K, V> map) {
        return new Builder<>(map);
    }

    public static class Builder<K, V> {
        private Map<K, V> resultMap = new HashMap<>();

        private Builder(Map<K, V> map) {
            if (Objects.nonNull(map)) {
                resultMap = map;
            }
        }

        public Builder<K, V> put(K k, V v) {
            resultMap.put(k, v);
            return this;
        }

        public Map<K, V> build() {
            return resultMap;
        }
    }


    public static void main(String[] args) {
        Map<String, Object> build = MapBuilder.<String, Object>builder().put("name", "忘记")
                .put("age", 23)
                .build();
        System.out.println(build);
    }
}
