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.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSortedMap;
import io.polyglotted.common.model.MapResult;
import io.polyglotted.common.model.MapResult.ImmutableMapResult;
import io.polyglotted.common.model.MapResult.ImmutableResult;
import io.polyglotted.common.model.MapResult.SimpleMapResult;
import io.polyglotted.common.model.Pair;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

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

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

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

    default MB putList(K key, List values) {
        if (values != null && !values.isEmpty()) put(key, (V) values); return (MB) this;
    }

    default MB putAll(Map map) {
        for (Map.Entry e : map.entrySet()) { put(e.getKey(), e.getValue()); } return (MB) this;
    }

    default MB putTransformed(Map map, PairTransformer transformer) {
        for (Map.Entry e : map.entrySet()) { Pair pair = transformer.transform(e); put(pair._a, pair._b); } return (MB) this;
    }

    int size();

    M build();

    ImmutableMap immutable();

    MapResult result();

    ImmutableResult immutableResult();

    static  ImmutableMultimap immutableMultimap(Map map) { return ImmutableMultimap.copyOf(safeEntries(map)); }

    static  ImmutableMultimap immutableMultimap() { return ImmutableMultimap.of(); }

    static , MB extends MapBuilder> ImmutableBiMap immutableBiMap(MapBuilder builder) {
        return immutableBiMap(builder.build());
    }

    static  ImmutableBiMap immutableBiMap(Map map) { return ImmutableBiMap.copyOf(map); }

    static  ImmutableBiMap immutableBiMap() { return ImmutableBiMap.of(); }

    static , MB extends MapBuilder> ImmutableSortedMap immutableSortedMap(MapBuilder bl) {
        return immutableSortedMap(bl.build());
    }

    static  ImmutableSortedMap immutableSortedMap(Map map) { return ImmutableSortedMap.copyOf(map); }

    static  ImmutableSortedMap immutableSortedMap() { return ImmutableSortedMap.of(); }

    static  ImmutableMapBuilder immutableMapBuilder() { return immutableMapBuilder(ImmutableMap::builder); }

    static  ImmutableMapBuilder immutableMapBuilder(Supplier> s) { return new ImmutableMapBuilder<>(s.get()); }

    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, ImmutableMapBuilder> {
        private final ImmutableMap.Builder builder;

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

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

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

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

        @Override public MapResult result() { return immutableResult(); }

        @Override public ImmutableResult immutableResult() { return new ImmutableMapResult((ImmutableMap) builder.build()); }
    }

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

        public boolean containsKey(K key) { return builder.containsKey(key); }

        public V get(K key) { return builder.get(key); }

        public V getOrDefault(K key, V defaultValue) { return builder.getOrDefault(key, defaultValue); }

        public V remove(K key) { return builder.remove(key); }

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

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

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

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

        @Override public MapResult result() {
            return builder instanceof MapResult ? (MapResult) builder : new SimpleMapResult((Map) builder);
        }

        @Override public ImmutableResult immutableResult() { return new ImmutableMapResult(ImmutableMap.copyOf((Map) builder)); }
    }

    interface PairTransformer {
        Pair transform(Map.Entry entry);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy