tag:blogger.com,1999:blog-741750605858169835.post3449735963599732151..comments2024-01-24T14:53:02.919+00:00Comments on Stephen Colebourne's blog: Joda-ConvertStephen Colebournehttp://www.blogger.com/profile/01454237967846880639noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-741750605858169835.post-46887992096862580242010-09-08T08:32:07.000+01:002010-09-08T08:32:07.000+01:00What about this:
public class Foo{
@FromType(S...What about this:<br /><br />public class Foo{<br /><br /> @FromType(String.class) //@FromType get type from parameter<br /> public static Foo parse(String str) { ... }<br /><br /> @ToType(String.class) //@ToType get type from method return type<br /> public String getStringOutput() { ... }<br /><br /><br /> @FromType //@FromType(Bar.class) <br /> public static Foo parse(Bar bar) { ... }<br /><br /> @ToType //@ToType(Bar.class) <br /> public Bar toBar() { ... }<br /><br />}<br /><br />Best Regards,<br />Ali EbrahimiAli Ebrahiminoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-58120870353074360782010-08-23T16:30:22.000+01:002010-08-23T16:30:22.000+01:00The Spring example reminds me of the need for both...The Spring example reminds me of the need for both localised and non localised conversions.Mark Thorntonnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-12758810581760358782010-08-23T10:05:11.000+01:002010-08-23T10:05:11.000+01:00Keith, thanks for the linksKeith, thanks for the linksStephen Colebournenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-77309248789806064322010-08-23T00:44:42.000+01:002010-08-23T00:44:42.000+01:00Stephen,
Spring introduced a dedicated type conve...Stephen,<br /><br />Spring introduced a dedicated type conversion system in version 3.0, separate from and superior to its long-standing support for JavaBean PropertyEditors. This support includes integration with Joda Time for Date/time conversion. Conversion can be driven by type as well as by annotation.<br /><br />You can read more about this particular system here:<br />http://blog.springsource.com/2009/11/17/spring-3-type-conversion-and-validation/<br /><br />and:<br />http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html#core-convert<br /><br />Hope this is a helpful resource for you! (I am the primary author of the system).<br /><br />KeithKeith Donaldhttp://blog.springsource.comnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-85168429951675874242010-08-22T21:28:26.000+01:002010-08-22T21:28:26.000+01:00Curt, I would say that type conversion and formatt...Curt, I would say that type conversion and formatting are two different things, but I'll see if I can add something to explain.<br /><br />SK, Jibx are using a different strategy from the looks of it. Sadly, type conversion is something each and every project seems to do.Stephen Colebournenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-17288817468136624452010-08-21T16:13:47.000+01:002010-08-21T16:13:47.000+01:00Hi Stephen,
Is this the same joda-time <-> S...Hi Stephen,<br />Is this the same joda-time <-> String conversion strategy employed in the jibx library?<br />http://jibx.sourceforge.net/binding/date-time.htmlSKnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-86495447359294306672010-08-21T16:00:16.000+01:002010-08-21T16:00:16.000+01:00Stephen,
The documentation should have some disc...Stephen, <br /><br />The documentation should have some discussion of how it relates to similar facilities that are already part of Java. Adding this to java.text.Format and java.util.Formatter has the potential to add more confusion to the platform.<br /><br />Thanks,<br />CurtCurt Coxnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-37834470743350074632010-08-20T22:32:02.000+01:002010-08-20T22:32:02.000+01:00Arek, The aim of this project should be very simpl...Arek, The aim of this project should be very simple. So, compound decoding would not be supported. Annotations can be accessed by reflection - no APT required.<br /><br />Andy, ProperyEditor has a whole heap of other rubbish on the interface.Stephen Colebournenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-69214070371053449412010-08-20T21:41:00.000+01:002010-08-20T21:41:00.000+01:00Isn't this what java.beans.PropertyEditor is u...Isn't this what java.beans.PropertyEditor is used for?Andy Tnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-70621363752530218072010-08-20T19:00:06.000+01:002010-08-20T19:00:06.000+01:00It is interesting.
I have similar code and if the...It is interesting.<br /><br />I have similar code and if there will be library witch will be more standard (had chances to get to JDK at one point) I will consider using it.<br /><br />However my case is a bit more complicated.<br />I serialize to String like 'dis.127b8156-2396-4cec-ab1c-4f7feadbb62b' where everything after "dis." is UUID.<br />Of course Distance will not be able to deserialize itself from such ID. I have helper class which use DAO to get whole Object.<br /><br />How this will work. I understand you don't use APT to process your annotations and generate some code - as I do.Arekhttp://www.facebook.com/arek.stryjskinoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-31303728563517011652010-08-20T18:37:57.000+01:002010-08-20T18:37:57.000+01:00@Bob whats with the vitriol? What have you contrib...@Bob whats with the vitriol? What have you contributed lately?<br /><br />What if you are inheriting a class that implements a fromString() method with a different contract than you want? The annotation-based method gives you a lot of type independence.Justin Pittsnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-63768256415215161962010-08-20T18:12:00.000+01:002010-08-20T18:12:00.000+01:00Bob, the problem is bigger than you might imagine....Bob, the problem is bigger than you might imagine. No-one can agree on the standard method names for starters!<br /><br />Moreover, in the JDK there are cases where the canonical String representation has changed with time. The toString method on a TimeZone isn't very helpful for example. You might say "well just change the toString", but it turns out that is frequently not backwards compatible. Thus, an annotation is useful for marking the new method which has to be added to avoid breaking compatibility.Stephen Colebournenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-16472364999753970872010-08-20T18:07:58.000+01:002010-08-20T18:07:58.000+01:00There is an interface StringConverter which is typ...There is an interface StringConverter which is type safe and has conversions to and from a string. The reflection is all hidden, so users never see it.<br /><br />As well as the annotation approach, you can implement the interface directly yourself, and register it. This obviously avoids all reflection, at the cost of writing a simple class.Stephen Colebournenoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-64114737185223131652010-08-20T18:01:49.000+01:002010-08-20T18:01:49.000+01:00This looks interesting, but I think I prefer using...This looks interesting, but I think I prefer using something like a Show instance (see http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/fj/Show.html for an example) or better yet a Bijection which represents the invertible function A => B * B => A so that I get a guarantee of compile-time type safety and avoid reflection. <br /><br />Conversion to and from some arbitrary data type (with String being just a common specialization) should really be represented as an invertible function. In Java this of course means you have to add an argument representing the bijection object to the function calls where you want to use it, which is a bit of a pain; in Scala you can use the implicits feature to get the proper bijection instance automatically injected into your method call. Now, it might be interesting to see if there's a way to use Java annotations and reflection to do the lookup and perhaps reflective instantiation and application of the appropriate bijection, but again in this case you'll miss out on the compile-time guarantees.Kris Nuttycombehttp://logji.blogspot.comnoreply@blogger.com