DateRange immutable?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
rapand
Posts: 3
Joined: Tue May 27, 2008 1:59 pm

DateRange immutable?

Post by rapand » Tue May 27, 2008 2:17 pm

Hi

Frankly speaking I am new to JFreeChart, but to me it 'seems' as a bug when DateRange claims to be immutable when the public constructor DateRange (Date lower, Date upper) is using these Date objects when calling either getLowerDate or getUpperDate since Date is not immutable.

Two ways to solve this would be to:
1. call clone on the lowerDate and upperDate when the get methods are called.
or
2. using: e.g. return new Date (use super/Range's getUpperBound () method) for getUpperDate likewise for getLowerDate - since then the responsibility lies only at one place (in Range).
Well I do think it is required that each call to these get methods spawn a new Date object to get this truly immutable.

- Perhaps just for the eye make fields lower and upper in Range final.

Thank you for what seems to be a great library!

rapand
Posts: 3
Joined: Tue May 27, 2008 1:59 pm

FixedMillisecond as well

Post by rapand » Wed May 28, 2008 12:55 pm

Hi again

FixedMillisecond crossed my way as well. It seems that the constructor public FixedMillisecond(Date time) and public Date getTime() also have problems fulfilling that the FixedMillisecond class is immutable as stated by the comment of the class.

Thank you for reading! :-)

jpmaia
Posts: 53
Joined: Fri Feb 22, 2008 10:44 pm

Post by jpmaia » Wed May 28, 2008 8:47 pm

The Java Date class IS immutable. Where did you get the idea it isn't?

Edited: after the subsequent post, am I missing something?

Edited still later: I went back and double-checked the Java doc on Date. The Date class is not immutable as you've stated. Now, where did *I* get the idea that it was immutable? *sigh*
Last edited by jpmaia on Wed May 28, 2008 9:03 pm, edited 3 times in total.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Wed May 28, 2008 8:47 pm

Yes, you are right about this - thanks for reporting it. I'll fix it.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

rapand
Posts: 3
Joined: Tue May 27, 2008 1:59 pm

Post by rapand » Thu May 29, 2008 8:56 am

Thank you David!

I'm sorry to disturb you once again - when I had some more time yesterday I went through org.jfree.data.time.

1. I noticed that SimpleTimePeriod has the same problem.

2. This might only be a less important issue (I guess), but the Day class contains fields that are defined as e.g.: protected static final DateFormat DATE_FORMAT_SHORT

Since DateFormat has at least 4 'set' methods even sub-classes may change global behaviour of the different instances.
(So this is not related to immutability since compareTo and hashCode does not seem to depend on them but more because parseDay of Day depends on it and are related to fields DATE_FORMAT and DATE_FORMAT_SHORT (the other fields for medium and long formats does not seem to be used but might be in subclasses).)

I guess not making the fields static would solve this behaviour depending on the importance of this in relation to parseDay.

3. An also not so important issue is that in TimeSeries removeAgedItems (long latest, boolean notify) TimeZone.getDefault () is used.
Now since TimeZone.setDefault (...) sets the TimeZone globally - one 'could' potentially make use of Dates written in one TimeZone an in accident delete those in another TimeZone where they shouldn't, but how often does it happen that such a case pops up? I do not know - unless you perhaps serialize the objects to another location on the Globe :) Perhaps graphs used in banks simultaneously at different locations - well only speculations.

I admit I'm not into this TimeSeries class, but I noticed that you in some other classes save the default TimeZone during construction e.g. in RegularTimePeriod: public static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getDefault(); - perhaps that is a simple and good fix?

jpmaia
Posts: 53
Joined: Fri Feb 22, 2008 10:44 pm

Post by jpmaia » Thu May 29, 2008 8:55 pm

As far as saving an instance of TimeZone.getDefault....

This shouldn't create a problem in most applications, but if you are in an application where the user can choose their time zone on the fly (as is the case with the application I work on), it could be a bad thing to have various time zone instances saved off in static or instance variables in the code.

Dates stored as long values are all calculated based on GMT, so the local time zone really doesn't apply. But when you work with the Calendar class and the translation of long values into a presentable date, this is where a saved static time zone reference could be at odds with a subsequent time zone choice by the user.

seh4nc
Posts: 3
Joined: Thu Jan 03, 2008 3:37 am

Post by seh4nc » Fri Aug 22, 2008 5:20 pm

The new DateRange resulting from this change (r1081) causes my application to spend a large amount of time and memory creating new Date objects from inside the DateAxis.valueToJava2D() method.

Can we rethink this change? I understand java.util.Date is not immutable but a lot of interfaces return (non-cloned) collections, arrays, and Dates for performance reasons, you just have to trust the caller to respect the immutability contract.

Edited: I'm referring to change to DateRange made in response to the original DateRange immutability issue, not to the TimeZone issues raised later.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Sep 01, 2008 12:00 pm

I'm wondering if this problem could be fixed by adding getLowerMillis() and getUpperMillis() to the DateRange class, and then the valueToJava2D() method can work directly with the millis instead of creating Date instances?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Sep 01, 2008 2:26 pm

Seems to work, so I've committed the change to Subversion for inclusion in the 1.0.11 release.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

seh4nc
Posts: 3
Joined: Thu Jan 03, 2008 3:37 am

Post by seh4nc » Tue Sep 02, 2008 12:57 am

Sounds like an excellent solution. Thanks Dave!

Locked