<html> <head> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> </head> <body bgcolor="#FFFFFF" text="#000000"> <p>Yes, I known well those methods. But I disagree that they are cleaner and OO aspect - they are for an other purpose than the one in this code.<br> </p> <p>I will not debate since this issue is not important - at least the use of <tt>Class.isinstance</tt> or <tt>Class.cast</tt> is not wrong. But I will just mention that using them in this context increase the size of the <tt>.class</tt> file for no purpose, and distracts the user by a bigger stack track than needed.<br> </p> <p>This test:<br> </p> <blockquote> <pre>public class Test { public static void main(String[] args) { Object a = "test"; Integer b = (Integer) a; } } </pre> </blockquote> <p>is easy to read and produces a <tt>.class</tt> file 475 bytes long, while this test:<br> </p> <blockquote> <pre>public class Test { public static void main(String[] args) { Object a = "test"; Integer b = Integer.class.cast(a); } } </pre> </blockquote> <p>produces a <tt>.class</tt> file 559 bytes long. Furthermore the failure causes, in the first case:<br> </p> <blockquote> <pre>java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at Test.main(Test.java:4) </pre> </blockquote> <p>While the failure causes, in the second case:<br> </p> <blockquote> <pre>java.lang.ClassCastException: Cannot cast java.lang.String to java.lang.Integer at java.lang.Class.cast(Class.java:3361) at Test.main(Test.java:4) </pre> </blockquote> <p>So usage of <tt>Class.isinstance</tt> or <tt>Class.cast</tt> in this context is unnecessary weight, and the argument that they are cleaner is questionable. But as I said it doesn't matter - they are not wrong.</p> <p>Martin</p> <br> <br> <div class="moz-cite-prefix">Le 31/10/14 03:12, Otávio Gonçalves de Santana a écrit :<br> </div> <blockquote cite="mid:CAPc8kR2S= " type="cite"> <div dir="ltr"> <div>This method are equivalent, just cleaner and OO aspect.</div> <div><br> </div> <a moz-do-not-send="true" href="http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#isInstance-java.lang.Object-">http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#isInstance-java.lang.Object-</a><br> <div><a moz-do-not-send="true" href="http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#cast-java.lang.Object-">http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#cast-java.lang.Object-</a><br> </div> </div> <div class="gmail_extra"><br> <div class="gmail_quote">On Thu, Oct 30, 2014 at 4:35 PM, Martin Desruisseaux <span dir="ltr"><<a moz-do-not-send="true" href="mailto: " target="_blank"> </a>></span> wrote:<br> <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div bgcolor="#FFFFFF" text="#000000"> <p>What would be the purpose to have <tt>Quanties</tt> to return <tt>QuantityFactory</tt>, since <tt>QuantityFactory</tt> should probably be obtained by whatever Service Provider mechanism the platform use (e.g. OSGi, or <tt>java.util.ServiceLoader</tt> on JavaSE)?</p> <p>As a side note, I have a quick look at the code. Why code like that:</p> <blockquote> <pre>if (Long.class.isInstance(value)) { return new LongQuantity<>(Long.class.cast(value), unit); } </pre> </blockquote> <p>instead of:<br> </p> <blockquote> <pre>if (value instanceof Long) { return new LongQuantity<>((Long) value, unit); }</pre> </blockquote> <p>The <tt>Class.isInstance</tt> and <tt>Class.cast</tt> methods are useful when the class is unknown at compile time. But in the above code they are not necessary.<br> </p> <p> Martin<br> </p> <p><br> </p> <br> <div>Le 30/10/14 23:25, Werner Keil a écrit :<br> </div> <div> <div class="h5"> <blockquote type="cite"> <div dir="ltr"> <div>Hi,</div> <div><br> </div> <div>As Antoine just did some of those for CDI 2 on the Mailing list, let me also try the same here. The last doodle triggered so much discussion on the Mailing list, that the question probably could have been asked here in the first place, too<img style="margin:0px 0.2ex;vertical-align:middle" src="cid:part4.06070301.08060002@geomatys.fr" goomoji="329"></div> <div><br> </div> <div>Should <strong>Quantities </strong>(<a moz-do-not-send="true" href="https://github.com/unitsofmeasurement/unit-ri/blob/master/src/main/java/tec/units/ri/quantity/Quantities.java" target="_blank">https://github.com/unitsofmeasurement/unit-ri/blob/master/src/main/java/tec/units/ri/quantity/Quantities.java</a>) return </div> <div>1) a <strong>Quantity </strong>instance like it does now in both RI and SE port</div> <div> or</div> <div>2) a <strong>QuantityFactory</strong>? </div> <div> similar to what JSR 354 does with MonetaryAmounts:</div> <div> <a moz-do-not-send="true" href="https://github.com/JavaMoney/jsr354-api/blob/master/src/main/java/javax/money/MonetaryAmounts.java" target="_blank">https://github.com/JavaMoney/jsr354-api/blob/master/src/main/java/javax/money/MonetaryAmounts.java</a>)</div> <div><br> </div> <div>See <a moz-do-not-send="true" href="https://java.net/jira/browse/UNITSOFMEASUREMENT-65" target="_blank">https://java.net/jira/browse/UNITSOFMEASUREMENT-65</a> </div> <div>The outcome decides, whether we treat QuantityFactory as a core/mandatory part of the API (keeping it under "function" I think it's better than a slightly cluttered impression javax.money makes with 30 top level elements<img style="margin:0px 0.2ex;vertical-align:middle" src="cid:part8.03060508.03080303@geomatys.fr" goomoji="347">) or should move it to the optional SPI (which is optional especially because the entire service part is optional in MEEP, too, hence providing services on a device that has no use for it would be a waste)</div> <div><br> </div> <div>Iit is not directly related to <a moz-do-not-send="true" href="https://java.net/jira/browse/UNITSOFMEASUREMENT-67" target="_blank">https://java.net/jira/browse/UNITSOFMEASUREMENT-67</a> but both aim at static "convenience factory" methods.</div> <div><br> </div> <div>Making this simple, please try to reply like</div> <div>+1 for 1)</div> <div>or</div> <div>+1 for 2)</div> <div><br> </div> <div>not with a too lengthy discussion around it yet. It worked well for JSR 365, hope we can also get similar results here<img style="margin:0px 0.2ex;vertical-align:middle" src="cid:part4.06070301.08060002@geomatys.fr" goomoji="329"></div> <div><br> </div> <div>Thanks,</div> <div><br clear="all"> </div> <div> <div> <div dir="ltr"><span style="font-family:arial,sans-serif"> <p style="margin:0px;font-size:13px;border-collapse:collapse"><span style="color:rgb(0,0,0);text-transform:none;text-indent:0px;letter-spacing:normal;word-spacing:0px;white-space:normal;border-collapse:separate;font-size-adjust:none;font-stretch:normal"><span style="font-family:Helvetica"><span style="font-family:Calibri;font-size:12px"></span></span></span></p> <div><font face="Arial"><span style="font-family:arial,sans-serif"> <p style="margin:0px;font-size:13px;border-collapse:collapse"><span style="font-family:Calibri" lang="EN-US">Werner</span></p> </span> <p style="margin:0px;font-size:13px;border-collapse:collapse"><br> </p> </font> <p style="margin:0px;font-size:13px;border-collapse:collapse"><br> </p> </div> <p style="margin:0px;font-size:13px;border-collapse:collapse"><br> </p> </span><span lang="EN-GB"> <p style="margin:0px;font-size:13px;border-collapse:collapse"><br> </p> <div><br> </div> </span></div> </div> </div> </div> </blockquote> <br> </div> </div> </div> </blockquote> </div> <br> <br clear="all"> <div><br> </div> -- <br> <div class="gmail_signature"> <div dir="ltr">Otávio Gonçalves de Santana <div> <div style="text-align:center"><br> <div style="text-align:left"><font face="Tahoma">blog: </font><a moz-do-not-send="true" href="http://otaviosantana.blogspot.com.br/" style="text-align:center" target="_blank">http://otaviosantana.blogspot.com.br/</a></div> <div style="text-align:left">twitter: <a moz-do-not-send="true" href="http://twitter.com/otaviojava" target="_blank">http://twitter.com/otaviojava</a></div> <div style="text-align:left">site: <span style="text-align:center"><font color="#0000ee"><u><a moz-do-not-send="true" href="http://about.me/otaviojava" target="_blank">http://about.me/otaviojava</a></u></font></span></div> <div style="text-align:left">55 (11) 98255-3513</div> <div style="text-align:-webkit-auto"><br> </div> </div> </div> </div> </div> </div> </blockquote> <br> </body> </html>
Attachment:
gifVJc4FQxRAn.gif
Description: GIF image
Attachment:
gif1_GHha0OZ9.gif
Description: GIF image