Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.github.stewseo.client.util.MapBuilder Maven / Gradle / Ivy
package io.github.stewseo.client.util;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
public class MapBuilder implements ObjectBuilder> {
private final Map map = new HashMap<>();
private final Supplier builderCtor;
public MapBuilder(Supplier builderCtor) {
this.builderCtor = builderCtor;
}
public MapBuilder put(K key, V value) {
map.put(key, value);
return this;
}
public MapBuilder put(K key, Function> fn) {
return put(key, fn.apply(builderCtor.get()).build());
}
public MapBuilder putAll(Map extends K, ? extends V> map) {
this.map.putAll(map);
return this;
}
public MapBuilder putAll(Iterable> entries) {
for (Map.Entry extends K, ? extends V> entry: entries) {
this.map.put(entry.getKey(), entry.getValue());
}
return this;
}
@Override
public Map build() {
return map;
}
public static Map of(K k1, V v1) {
return Collections.singletonMap(k1, v1);
}
public static Map of(K k1, V v1, K k2, V v2) {
return makeMap(k1, v1, k2, v2);
}
public static Map of(K k1, V v1, K k2, V v2, K k3, V v3) {
return makeMap(k1, v1, k2, v2, k3, v3);
}
public static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
return makeMap(k1, v1, k2, v2, k3, v3, k4, v4);
}
public static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
return makeMap(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
}
private static Map makeMap(Object... values) {
Map result = new HashMap<>(values.length/2);
for (int i = 0; i < values.length; i+=2) {
@SuppressWarnings("unchecked")
K k = (K)values[i];
@SuppressWarnings("unchecked")
V v = (V)values[i+1];
result.put(k, v);
}
return result;
}
}