Skip to main content

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

  • From: Werner Keil < >
  • To: " " < >
  • Subject: Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory
  • Date: Fri, 31 Oct 2014 10:42:07 +0100

That's a good Point. While the line of code in your own test is usually
very vital, one would rarely debug or try to fix a core type like Class in
Java itself (even for OpenJDK that is highly unlikely to happen[?])

Size matters especially in ME. Oracle recently published an Optimization
guide: http://docs.oracle.com/javame/8.1/me-dev-guide/optimization.htm
And it contains not only advice to avoid auto-boxing (something, especially
on SE I keep seeing a lot in new code[?]) but once general recommendations
like use StringBuffer (actually could be StringBuilder now for ME 8[?])
instead of concatinating strings with "+".
Thus a seemingly tiny difference of 100 bytes between the two calls can
make quite a difference if you have many classes in ME.

The guide also refers to obfuscation, but note, "Stripping" is almost
impossible to be available or acceptable in ME 8. Mainly for legal
(license/compatibility) not technical reasons, but as long as Oracle and
other JCP Members (and their legal teams) have not come to a conclusion,
it's just like Apache Harmony or the recent Apache license issues, it won't
happen until they find another solution or fix the old legal dispute...

@Martin, do you have an opinion or preference for the Quantities class?
As of now, we have exactly 2 votes, one for each Option.

The final decision should also Impact the piece of code you mentioned, but
especially for ME/RI we appreciate your findings and will prefer the option
that creates less/smaller code and fewer "noise" in debug messages or
exceptions. Ideally RI and other implementations should act in a similar
way, but unlike the public API and being compatible with that there is a
bit of freedom for such internals.

Werner





On Fri, Oct 31, 2014 at 3:20 AM, Martin Desruisseaux <
>
 wrote:

>  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.
>
> I will not debate since this issue is not important - at least the use of
> Class.isinstance or Class.cast is not wrong. But I will just mention that
> using them in this context increase the size of the .class file for no
> purpose, and distracts the user by a bigger stack track than needed.
>
> This test:
>
> public class Test {
>     public static void main(String[] args) {
>         Object a = "test";
>         Integer b = (Integer) a;
>     }
> }
>
>  is easy to read and produces a .class file 475 bytes long, while this
> test:
>
> public class Test {
>     public static void main(String[] args) {
>         Object a = "test";
>         Integer b = Integer.class.cast(a);
>     }
> }
>
>  produces a .class file 559 bytes long. Furthermore the failure causes,
> in the first case:
>
> java.lang.ClassCastException: java.lang.String cannot be cast to 
> java.lang.Integer
>       at Test.main(Test.java:4)
>
>  While the failure causes, in the second case:
>
> 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)
>
>  So usage of Class.isinstance or Class.cast 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.
>
> Martin
>
>
> Le 31/10/14 03:12, Otávio Gonçalves de Santana a écrit :
>
>  This method are equivalent, just cleaner and OO aspect.
>
>
> 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#cast-java.lang.Object-
>
> On Thu, Oct 30, 2014 at 4:35 PM, Martin Desruisseaux <
>  >
>  wrote:
>
>>  What would be the purpose to have Quanties to return QuantityFactory,
>> since QuantityFactory should probably be obtained by whatever Service
>> Provider mechanism the platform use (e.g. OSGi, or
>> java.util.ServiceLoader on JavaSE)?
>>
>> As a side note, I have a quick look at the code. Why code like that:
>>
>> if (Long.class.isInstance(value)) {
>>     return new LongQuantity<>(Long.class.cast(value), unit);
>> }
>>
>>  instead of:
>>
>> if (value instanceof Long) {
>>     return new LongQuantity<>((Long) value, unit);
>> }
>>
>>  The Class.isInstance and Class.cast methods are useful when the class
>> is unknown at compile time. But in the above code they are not necessary.
>>
>>     Martin
>>
>>
>>
>> Le 30/10/14 23:25, Werner Keil a écrit :
>>
>>  Hi,
>>
>>  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[?]
>>
>>  Should *Quantities *(
>https://github.com/unitsofmeasurement/unit-ri/blob/master/src/main/java/tec/units/ri/quantity/Quantities.java)
>> return
>> 1) a *Quantity *instance like it does now in both RI and SE port
>>     or
>> 2) a *QuantityFactory*?
>>     similar to what JSR 354 does with MonetaryAmounts:
>>
>https://github.com/JavaMoney/jsr354-api/blob/master/src/main/java/javax/money/MonetaryAmounts.java
>> )
>>
>>  See https://java.net/jira/browse/UNITSOFMEASUREMENT-65
>> 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
>> [?]) 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)
>>
>>  Iit is not directly related to
>https://java.net/jira/browse/UNITSOFMEASUREMENT-67 but both aim at
>> static "convenience factory" methods.
>>
>>  Making this simple, please try to reply like
>> +1 for 1)
>> or
>> +1 for 2)
>>
>>  not with a too lengthy discussion around it yet. It worked well for JSR
>> 365, hope we can also get similar results here[?]
>>
>>  Thanks,
>>
>>    Werner
>>
>>
>>
>>
>>
>>
>>
>>
>
>
>  --
>  Otávio Gonçalves de Santana
>
> blog:     http://otaviosantana.blogspot.com.br/
> twitter: http://twitter.com/otaviojava
> site:     *http://about.me/otaviojava ;<http://about.me/otaviojava>*
> 55 (11) 98255-3513
>
>
>

Attachment: 338.gif
Description: GIF image

Attachment: 329.gif
Description: GIF image

Attachment: 347.gif
Description: GIF image



[VOTE] Should a Quantities facade return Quantity or QuantityFactory

Werner Keil 10/30/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Werner Keil 10/30/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Martin Desruisseaux 10/30/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Werner Keil 10/30/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Werner Keil 10/30/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Otávio Gonçalves de Santana 10/30/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Otávio Gonçalves de Santana 10/30/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Martin Desruisseaux 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Werner Keil 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Martin Desruisseaux 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Otávio Gonçalves de Santana 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Martin Desruisseaux 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Werner Keil 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Jean-Marie Dautelle 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Jean-Marie Dautelle 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Werner Keil 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Martin Desruisseaux 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Werner Keil 10/31/2014

Re: [VOTE] Should a Quantities facade return Quantity or QuantityFactory

Jean-Marie Dautelle 10/31/2014
 
 
Close
loading
Please Confirm
Close