Last active
July 28, 2020 09:55
-
-
Save marchrius/29d6ea3ad734c924e061ebee670617c7 to your computer and use it in GitHub Desktop.
Maps builder utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import org.marchirus.utils.Maps; | |
public class Example { public static void main(String[] args) { | |
Maps.Builder<String, String> builder = Maps.builder(); | |
builder.pair("Key1", "Value1") | |
.key("Key2").value("Value2") | |
.pair("Key3", "Value3") | |
.pair("Key4", "Value4") | |
.key("Key5").value("Value5") | |
; | |
Map<String, String> map = builder.build(); | |
System.out.println("map Count: " + map.size()); | |
System.out.println("map Content: " + prettyPrint(map)); | |
Map<String, String> map2 = Maps.<String, String>builder() | |
.pair("Key1", "Value1") | |
.key("Key2").value("Value2") | |
.pair("Key3", "Value3") | |
.pair("Key4", "Value4") | |
.key("Key5").value("Value5") | |
.last("Last", "Last") | |
; | |
System.out.println("map2 Count: " + map2.size()); | |
System.out.println("map2 Content: " + prettyPrint(map2)); | |
Maps.Builder<String, String> builder2 = Maps.builder(); | |
builder2.pair("Key1", "Value1") | |
.key("Key2").value("Value2") | |
.pair("Key3", "Value3") | |
.pair("Key4", "Value4") | |
.key("Key5").value("Value5") | |
; | |
Map<String, String> map3 = builder2.buildAndContinue(); | |
Map<String, String> map4 = builder2.pair("Key9", "Value9") | |
.key("Key10").value("Value10") | |
.build(); | |
System.out.println("map3 Count: " + map3.size()); | |
System.out.println("map3 Content: " + prettyPrint(map3)); | |
System.out.println("map4 Count: " + map4.size()); | |
System.out.println("map4 Content: " + prettyPrint(map4)); | |
} | |
private static <K, V> String prettyPrint(Map<K, V> map) { | |
StringBuilder sb = new StringBuilder(); | |
Iterator<Entry<K, V>> iter = map.entrySet().iterator(); | |
sb.append('{').append('\n'); | |
while (iter.hasNext()) { | |
Entry<K, V> entry = iter.next(); | |
sb.append(' ').append(' ') | |
.append(entry.getKey()).append(':').append(' ') | |
.append('"') | |
.append(entry.getValue()) | |
.append('"'); | |
if (iter.hasNext()) { | |
sb.append(','); | |
} | |
sb.append('\n'); | |
} | |
sb.append("}, ").append("size: ").append(map.size()); | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.marchrius.utils; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Maps { | |
private Maps() { | |
throw new UnsupportedOperationException("This is an utility class. Do not instantiate it."); | |
} | |
public static <K, V> Map<K, V> newHashMap() { | |
return new HashMap<>(); | |
} | |
public static <K, V> Builder<K, V> pair(K key, V value) { | |
return Maps.<K, V>builder().pair(key, value); | |
} | |
public static <K, V> Map<K, V> single(K key, V value) { | |
return Maps.<K, V>builder().last(key, value); | |
} | |
public static <K> Builder<K, Object> simple(K key, Object value) { | |
return Maps.<K>objectBuilder().pair(key, value); | |
} | |
public static <K, V> Builder<K, V> builder() { | |
return new Builder<>(); | |
} | |
public static <K> ObjectBuilder<K> objectBuilder() { | |
return new ObjectBuilder<>(); | |
} | |
public static <K, V> Builder<K, V> builder(Map<K, V> source) { | |
return new Builder<>(source); | |
} | |
public static class ObjectBuilder<K> extends Builder<K, Object> { | |
public ObjectBuilder<K> pair(K key, ObjectBuilder<K> builder) { | |
add(key, builder.build()); | |
return this; | |
} | |
} | |
public static class Builder<K, V> { | |
private final Map<K, V> map = new HashMap<>(); | |
private Builder() { } | |
private Builder(Map<K, V> source) { | |
this(); | |
if (source != null) { | |
map.putAll(source); | |
} | |
} | |
public Builder<K, V> pair(K key, V value) { | |
return add(key, value); | |
} | |
public Map<K, V> last(K key, V value) { | |
add(key, value); | |
return build(); | |
} | |
public Builder<K, V> merge(Map<K, V> map) { | |
if (map == null) return this; | |
for (Map.Entry<K, V> entry : map.entrySet()) { | |
add(entry.getKey(), entry.getValue()); | |
} | |
return this; | |
} | |
public PairBuilder<K, V> key(K key) { | |
return new PairBuilder<>(this, key); | |
} | |
public Map<K, V> build() { | |
Map<K, V> newMap = buildAndContinue(); | |
map.clear(); | |
return newMap; | |
} | |
public Map<K, V> buildAndContinue() { | |
Map<K, V> newMap = newHashMap(); | |
newMap.putAll(map); | |
return newMap; | |
} | |
protected Builder<K, V> add(K key, V value) { | |
map.put(key, value); | |
return this; | |
} | |
} | |
public static class PairBuilder<K, V> { | |
private final Builder<K, V> parentBuilder; | |
private final K key; | |
private PairBuilder(Builder<K, V> parentBuilder, K key) { | |
this.parentBuilder = parentBuilder; | |
this.key = key; | |
} | |
public Builder<K, V> value(V value) { | |
return parentBuilder.add(this.key, value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment