Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 355718

Summary: Add support for date serialization
Product: z_Archived Reporter: Marcelo Paternostro <marcelo.paternostro>
Component: SapphireAssignee: Konstantin Komissarchik <konstantin>
Status: CLOSED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: konstantin
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Suggestion for the implementation of a date serialization service konstantin: iplog+

Description Marcelo Paternostro CLA 2011-08-24 11:13:32 EDT
Created attachment 202094 [details]
Suggestion for the implementation of a date serialization service

Handling date is usually a non-trivial issue, and typically a solution never covers all usages. That said, providing a basic date serialization support in the framework helps in two ways: it may cover some cases (hopefully the majority) and it gives an example that people can use to create their own implementations.

The attached class is an implementation suggestion. The idea is to rely on a fixed number of valid formats to convert a string to a java.util.Date object. The code was written to allow subclasses to provide new supported formats. The reverse operation, from date to string, uses the first supported format by default (it can also be modified on subclasses).

If the suggested implementation is used, I'd recommend keeping the get*Format methods public to enable clients to write code that, for example, displays a text with example of supported date strings.
Comment 1 Konstantin Komissarchik CLA 2011-08-24 18:07:27 EDT
Could you comment on the genesis of the list of default formats used in this serializer implementation?
Comment 2 Marcelo Paternostro CLA 2011-08-24 20:05:43 EDT
The goal with the list is to cover the most common formats and also the ones that can more precisely convey a date-time information. One can always argue in favor of a particular format and it may be the case to add it to the list. But we need to keep in mind that it will never be possible to cover every single case (hence the worry about providing the means to allow clients to display the supported format to users).

As for using and exposing a list, it just allows the API to refer to the "first" format, which, in the supplied code, is the most precise and complete. Some people prefer arrays but I personally don't like the fact that they cannot be set as unmodifiable (one can always do ARRAY[0]=null even if ARRAY is a static final member).
Comment 3 Konstantin Komissarchik CLA 2011-08-24 20:08:54 EDT
I understand that and prefer collections myself. My question is more regarding the method for putting together that list. Was it based on something?
Comment 4 Marcelo Paternostro CLA 2011-08-24 20:19:12 EDT
I'd say common sense and some user feedback.

This list is very similar to the one currently used by the EMF to perform a compatible operation. If my memory servers me well, the suggested value set is a combination of the our initial guess for the EMF code and one or two newsgroup suggestions.
Comment 5 Konstantin Komissarchik CLA 2011-08-24 22:12:19 EDT
Thanks for the clarification.
Comment 6 Konstantin Komissarchik CLA 2011-08-28 15:33:19 EDT
Starting with the attached implementation suggestion, I improved it by allowing it to be parameterized with different formats without subclassing (which is still possible too). Added unit tests (TestModelingMisc0009) and content in the enhancement guide.

Please verify.
Comment 7 Marcelo Paternostro CLA 2011-08-30 13:47:27 EDT
Nice modification: indeed a good use of the annotation's parameter list.

A minor observation is that I use the format list to generate a documentation that includes valid examples of strings that can be converted to date. With the changes, I have to invoke the init method so that formats return a non-null value:

DateSerializationService dateSerializationService =
	new DateSerializationService();
dateSerializationService.init(null, null, new String[0]);
List<? extends DateFormat> formats = dateSerializationService.formats();

I don't think this is a problem but something that worth mentioning. In other words, I am fine marking this bug as verified, unless you think it would be a good idea to handle the scenario above. In this case, the formats() method could be implemented like this:

    public List<? extends DateFormat> formats()
    {
        return formats != null ? formats : STANDARD_FORMATS;
    }
Comment 8 Konstantin Komissarchik CLA 2011-08-30 14:04:22 EDT
> DateSerializationService dateSerializationService =
>    new DateSerializationService();
> dateSerializationService.init(null, null, new String[0]);
> List<? extends DateFormat> formats = dateSerializationService.formats();

Rather than creating a new instance of the service, try accessing it like so:

DateSerializationService dateSerializationService = element.service( property, DateSerializationService.class );
List<? extends DateFormat> formats = dateSerializationService.formats();

The service is located, instantiated and initialized on first access. It then sticks around until the context is disposed. So you would be re-using the same instance of the service actually used for serialization.
Comment 9 Marcelo Paternostro CLA 2011-08-30 14:08:07 EDT
Awesome. That will cause the documentation to reflect possible customizations via the annotation parameters.
Comment 10 Konstantin Komissarchik CLA 2011-08-30 14:10:22 EDT
Yep. Closing.