{"id":98,"date":"2009-03-02T15:36:51","date_gmt":"2009-03-02T14:36:51","guid":{"rendered":"http:\/\/lqd.hybird.org\/journal\/?p=98"},"modified":"2009-08-13T12:58:56","modified_gmt":"2009-08-13T11:58:56","slug":"fun-friday-trick-generics-type-inference-static-imports-dont-wait-for-java-7","status":"publish","type":"post","link":"https:\/\/lqd.hybird.org\/journal\/?p=98","title":{"rendered":"Fun Friday Trick: Generics type inference + static imports = don&#8217;t wait for Java 7"},"content":{"rendered":"<p>Welcome to another fun edition of fun friday on a fun monday. <\/p>\n<p>Today, we&#8217;ll see a very simple trick, that uses the regular generics type inference with a factory method and static imports, to make generics more readable.<\/p>\n<p>Reading the java 7 language &#8220;pre&#8221; proposals (from polls and all), and especially the one about adding more type inference on generic objects construction, i remembered a very simple trick i stumbled upon years ago.<\/p>\n<p>What if you named your factory method as a constructor and imported it using the static import feature ? Turns out, i kinda like how it cleans up the code.<\/p>\n<p>Today, we&#8217;re using this, and it&#8217;s a mouthful:<br \/> Map&lt;String, List&lt;String&gt;&gt; anagrams = new HashMap&lt;String, List&lt;String&gt;&gt;();<\/p>\n<p>I think the java 7 proposal (when it&#8217;s released, there&#8217;s no draft for it yet, only informal polls, and a starting proposal <a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/coin-dev\/2009-February\/000009.html\"> here<\/a>) will allow to turn it into<br \/> Map&lt;String, List&lt;String&gt;&gt; anagrams = new HashMap&lt;&gt;();  <\/p>\n<p>First of, notice the ugly empty &lt;&gt;, it would look cleaner without it, but because of backwards compatibility, it might not be &#8220;possible&#8221;. We&#8217;ll see.<\/p>\n<p>While today&#8217;s trick turns it into this, (assuming it was added to HashMap of course)<\/p>\n<p>[sourcecode language=&#8217;java&#8217;]<br \/>\n\/\/ in java.util.HashMap for instance<br \/>\n\/\/ very common code except for the name<br \/>\npublic static <K, V> HashMap<K,V> HashMap()<br \/>\n{<br \/>\n   return new HashMap<K, V>();<br \/>\n}<\/p>\n<p>\/\/ in your code, where the magic happens<br \/>\nimport static java.util.HashMap.HashMap;<\/p>\n<p>\/\/ oooh, pretty<br \/>\nMap<String, List<String>> anagrams = HashMap();<br \/>\n[\/sourcecode]<\/p>\n<p>It&#8217;s less code. It&#8217;s cleaner without the &lt;&gt;. It looks a little like a C++ constructor &#8211; from a distance (: &#8211; , but it has added perks too, it&#8217;s a real factory method, so you can verify arguments and throw exceptions *without* creating objects, and so on.<\/p>\n<p>The way we&#8217;re doing it today is relatively easy to write since IDEs are helping, so you could see the java 7 proposal as of limited value, but code is written once, and read multiple times. That&#8217;s why i think both the proposal and today&#8217;s trick have value.<\/p>\n<p>The IDEs are however a little less successfull at providing you intellisense for static imports it seems. You can solve that by modifying the default class template, or by typing HashMap.HashMap() and refactoring it to a static import (or soon will be able to do so). <\/p>\n<p>You don&#8217;t have to wait for java 7 for generics to become easier to use, and even though it would be cleaner if the function was in, say, HashMap, you could still have a helper class \u00e0 la java.util.Collections or google collections&#8217; Lists and Maps, and so on, if you really wanted to.<\/p>\n<p>So, this is really about a way of naming factory methods and importing them statically, making it rather transparent to use today, without waiting for potential language changes. Both are not perfectly clean, but i don&#8217;t think we&#8217;ll be getting a real clean way (full type inference on the variable) any time soon, so, in the meantime, pick your poison.<\/p>\n<p>Next up: more composite tricks, and the real reason why they might be useful to you. Yes, there is a plan behind the randomness.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to another fun edition of fun friday on a fun monday. Today, we&#8217;ll see a very simple trick, that uses the regular generics type inference with a factory method and static imports, to make generics more readable. Reading the java 7 language &#8220;pre&#8221; proposals (from polls and all), and especially the one about adding [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[16],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-java","tag-fun-friday"],"_links":{"self":[{"href":"https:\/\/lqd.hybird.org\/journal\/index.php?rest_route=\/wp\/v2\/posts\/98","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lqd.hybird.org\/journal\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lqd.hybird.org\/journal\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lqd.hybird.org\/journal\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lqd.hybird.org\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=98"}],"version-history":[{"count":25,"href":"https:\/\/lqd.hybird.org\/journal\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/lqd.hybird.org\/journal\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/lqd.hybird.org\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lqd.hybird.org\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lqd.hybird.org\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}