What the Duration of negative Infinity mean?
Martin Sladecek
martin.sladecek at oracle.com
Thu Jan 31 09:42:38 PST 2013
The other option is not to throw the exception and keep the Duration
values above zero. If a > b, then b.subtract(a) will result in 0.
So let's say currentTime of the animation is 1s. We'd have a button that
jumps 2 seconds backwards in the animation.
So something like
animation.jumpTo(animation.getCurrentTime().subtract(Duration.seconds(2)));
would result in calling animation.jumpTo with Duration of zero, which
makes sense.
However, with the negative values and going backwards, it actually gets
quite complicated. Negative rates goes backwards, but goes from positive
Duration to 0. So in order to start the animation backwards you havo to
jump to the end ( animation.jumpTo(animation.getTotalDuration()); ) and
then start the animation with negative rate (animation.setRate(-1.0);
animation.play(); ). But what if we set duration to negative? Will the
total duration be also negative and will go from 0 to negative number?
This might break some code as so far it was reasonable to expect that
with positive rate, the next position in the animation will be greater
than the previous. And what about set animations with mixed
positive/negative durations in an sequential/parallel animation? What
would be the duration of the parent animation?
Or will setting a negative duration set the duration to positive value
and automatically set the rate to negative?
I don't think we need negative durations then, as the durations that
animations will work with will be only positive.
What do you think?
-Martin
On 01/31/2013 05:23 PM, Richard Bair wrote:
>>> How does the animation code handle negative duration values?
>> It doesn't handle negative values at all. All code in animations expects the duration to be positive.
> It seems like we have to allow for negative durations for the sake of:
>
> Duration d1 = Duration.seconds(3);
> Duration d2 = Duration.seconds(-1);
> Duration d3 = d1.add(d2);
>
> This is obviously contrived (why don't I just use d1.subtract in the first place?), but if instead of hard coding the vales for 3 & -1, if they were computed by some other match routine (maybe the distance of the mouse from the object you are animating and that distance should form a duration or some such) then you could easily end up in a situation where a negative value is passed in and asking the developer to guard against that will be cumbersome.
>
> If that's true, then we have to decide what to do about negative values in ScheduledService & animation code (and anywhere else in our API that we use a Duration). Throwing an exception from a constructor is a possibility, but from a setter not so much. I could also add an "abs" to Duration so that developers who find themselves passing negative values to an animation could use "abs" to make sure the sign comes out right in the end?
>
> I'm also a little worried about compatibility if we start throwing exceptions for negative durations. Of course, if we did make it illegal for negative durations then the rest of the API / implementation would be much simpler. I'm worried about those Duration math methods though.
>
> Richard
More information about the openjfx-dev
mailing list