{"id":432,"date":"2009-08-09T22:37:31","date_gmt":"2009-08-09T20:37:31","guid":{"rendered":"http:\/\/raftaman.net\/?p=432"},"modified":"2021-05-15T11:46:41","modified_gmt":"2021-05-15T09:46:41","slug":"java-generics-and-comparables","status":"publish","type":"post","link":"https:\/\/possiblelossofprecision.net\/?p=432","title":{"rendered":"Java Generics and Comparables"},"content":{"rendered":"<p>When designing a generic class which needs a parameter that is comparable you will probably end up with <a href=\"http:\/\/www.tbray.org\/ongoing\/When\/200x\/2005\/06\/12\/Comparable\">something like this<\/a>:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\npublic interface Page&lt;K extends Comparable&lt;K&gt;&gt;\r\npublic class LocalPage&lt;K extends Comparable&lt;K&gt;&gt; implements Page&lt;K&gt;\r\n<\/pre>\n<p>Unfortunately, using Comparable isn&#8217;t as &#8220;easy&#8221;. The Page interface described above can&#8217;t be instantiated for a type like <code>java.sql.Time<\/code>, which is not Comparable to itself, but to a supertype (i.e., <code>java.sql.Time implements Comparable&lt;java.util.Date&gt;<\/code>).<br \/>\n<a href=\"http:\/\/jroller.com\/dhall\/date\/20050615#tim_bray_has_problems_with\">David Hall suggests<\/a>: &#8220;If you&#8217;re going to declare types that implement Comparable, you probably need to be in the habit of writing the declaration as:&#8221;<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\npublic interface Page&lt;K extends Comparable&lt;? super K&gt;&gt;\r\npublic class LocalPage&lt;K extends Comparable&lt;? super K&gt;&gt; implements Page&lt;K&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When designing a generic class which needs a parameter that is comparable you will probably end up with something like this: public interface Page&lt;K extends Comparable&lt;K&gt;&gt; public class LocalPage&lt;K extends Comparable&lt;K&gt;&gt; implements Page&lt;K&gt; Unfortunately, using Comparable isn&#8217;t as &#8220;easy&#8221;. The Page interface described above can&#8217;t be instantiated for a type like java.sql.Time, which is not Comparable to itself, but to&#8230; <a href=\"https:\/\/possiblelossofprecision.net\/?p=432\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4],"class_list":["post-432","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java"],"_links":{"self":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=432"}],"version-history":[{"count":7,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/432\/revisions"}],"predecessor-version":[{"id":2695,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/432\/revisions\/2695"}],"wp:attachment":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}