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

io.polyglotted.common.util.MapBuilder Maven / Gradle / Ivy

package io.polyglotted.common.util;

import com.google.common.collect.ImmutableMap;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import static io.polyglotted.common.util.ReflectionUtil.fieldValue;

@SuppressWarnings({"unchecked", "unused", "WeakerAccess"})
public interface MapBuilder> {
    MapBuilder put(K key, V value);

    MapBuilder putList(K key, List values);

    MapBuilder putAll(Map map);

    int size();

    M build();

    static  ImmutableMapBuilder immutableMapBuilder() { return new ImmutableMapBuilder<>(); }

    static  ImmutableMap immutableMap() { return ImmutableMap.of(); }

    static  ImmutableMap immutableMap(K k1, V v1) { return MapBuilder.immutableMapBuilder().put(k1, v1).build(); }

    static  ImmutableMap immutableMap(K k1, V v1, K k2, V v2) {
        return MapBuilder.immutableMapBuilder().put(k1, v1).put(k2, v2).build();
    }

    static  ImmutableMap immutableMap(K k1, V v1, K k2, V v2, K k3, V v3) {
        return MapBuilder.immutableMapBuilder().put(k1, v1).put(k2, v2).put(k3, v3).build();
    }

    static  ImmutableMap immutableMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
        return MapBuilder.immutableMapBuilder().put(k1, v1).put(k2, v2).put(k3, v3).put(k4, v4).build();
    }

    static  ImmutableMap immutableMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
        return MapBuilder.immutableMapBuilder().put(k1, v1).put(k2, v2).put(k3, v3).put(k4, v4).put(k5, v5).build();
    }

    static  ImmutableMap immutableMap(Map map) { return MapBuilder.immutableMapBuilder().putAll(map).build(); }

    static  SimpleMapBuilder simpleMapBuilder() { return simpleMapBuilder(LinkedHashMap::new); }

    static  SimpleMapBuilder simpleMapBuilder(Supplier> supplier) { return new SimpleMapBuilder<>(supplier.get()); }

    static  Map simpleMap() { return new LinkedHashMap<>(); }

    static  Map simpleMap(K k1, V v1) { return MapBuilder.simpleMapBuilder().put(k1, v1).build(); }

    static  Map simpleMap(K k1, V v1, K k2, V v2) { return MapBuilder.simpleMapBuilder().put(k1, v1).put(k2, v2).build(); }

    static  Map simpleMap(K k1, V v1, K k2, V v2, K k3, V v3) {
        return MapBuilder.simpleMapBuilder().put(k1, v1).put(k2, v2).put(k3, v3).build();
    }

    static  Map simpleMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
        return MapBuilder.simpleMapBuilder().put(k1, v1).put(k2, v2).put(k3, v3).put(k4, v4).build();
    }

    static  Map simpleMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
        return MapBuilder.simpleMapBuilder().put(k1, v1).put(k2, v2).put(k3, v3).put(k4, v4).put(k5, v5).build();
    }

    static  Map simpleMap(Map map) { return MapBuilder.simpleMapBuilder().putAll(map).build(); }

    @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
    class ImmutableMapBuilder implements MapBuilder> {
        private final ImmutableMap.Builder builder = ImmutableMap.builder();

        @Override public MapBuilder> put(K key, V value) { if (value != null) builder.put(key, value); return this; }

        @Override public MapBuilder> putList(K key, List values) {
            if (values != null && !values.isEmpty()) builder.put(key, (V) values); return this;
        }

        @Override public MapBuilder> putAll(Map map) {
            for (Map.Entry e : map.entrySet()) { put(e.getKey(), e.getValue()); } return this;
        }

        @Override public int size() { return fieldValue(builder, "size"); }

        @Override public ImmutableMap build() { return builder.build(); }
    }

    @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
    class SimpleMapBuilder implements MapBuilder> {
        private final Map builder;

        @Override public MapBuilder> put(K key, V value) { if (value != null) builder.put(key, value); return this; }

        @Override public MapBuilder> putList(K key, List values) {
            if (values != null && !values.isEmpty()) builder.put(key, (V) values); return this;
        }

        @Override public MapBuilder> putAll(Map map) {
            for (Map.Entry e : map.entrySet()) { put(e.getKey(), e.getValue()); } return this;
        }

        @Override public int size() { return builder.size(); }

        @Override public Map build() { return builder; }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy