Last active
July 20, 2020 18:52
-
-
Save culmat/a3bcc646fa4401641ac6eb01f3719065 to your computer and use it in GitHub Desktop.
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 common; | |
/** | |
* <blockquote> | |
* <pre> | |
* Map<String, Integer> myMap = map("a", 1).with("b", 2); | |
* </pre> | |
* </blockquote> | |
* | |
* The first tuple determines the map type, so cast as appropriate: | |
* <blockquote> | |
* <pre> | |
* Map<String, Shape> myShapes = map("rect", (Shape) new Rectangle()).with("line", new Line2D.Double()); | |
* </pre> | |
* </blockquote> | |
*/ | |
public class FluentHashMap<K, V> extends java.util.HashMap<K, V> { | |
public FluentHashMap<K, V> with(K key, V value) { | |
put(key, value); | |
return this; | |
} | |
public static <K, V> FluentHashMap<K, V> map(K key, V value) { | |
return new FluentHashMap<K, V>().with(key, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment