-
Notifications
You must be signed in to change notification settings - Fork 181
Add OfFloat.from methods in Point/Rectangle #2486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add OfFloat.from methods in Point/Rectangle #2486
Conversation
|
||
public static Point.OfFloat from(Point point) { | ||
if (point instanceof Point.OfFloat pointOfFloat) { | ||
return new Point.OfFloat(pointOfFloat.getX(), pointOfFloat.getY()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before this PR one simply returned the same point (no instantiation).
Is there a reason for returning new Point.OfFloat(...)
instead of just the existing pointOfFloat
?
Same goes for the rectangles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can only guess it is because of MonitorPoints that also Point.OfFloat but carry the monitor.
In general I think all these workarounds show that we have a major design flaw with the current way of how measure of position/sizes work and its to complicated to get right I therefore opened:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the from method is used to create a new object from the previous object. The idea is to allow any consumer to make a new Point.OfFloat object from any Point instance. We do not want to share the same instance as the consumer can manipulate this new object and we do not want to affect the old object. The from method semantically creates new object from the provided ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not want to share the same instance as the consumer can manipulate this new object and we do not want to affect the old object
Should we probably (as far as possible) make the object unmodifable then instead? And instead make thing like "set" mor a copy of a new object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the fields are public, we cannot make it unmodifiable completely, atleast not in a simple way. It will break all the consumers which reset these fields.
The idea of this PR is to move these helper methods which were implemented in DPIUtils and Win32DPIUtils inside the classes which should actually own it. I did such modifications in the from method only to make it semantically correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so basically you fixed a brittle design by making a defensive copy of the parameter. Something that wasn't happening before but should have happened.
I'm fine with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with that.
At a minimum the behavior must be documented (e.g. that you always get a fresh copy you are free to modify/use in any way).
Beside that only because on can modify x/y (int) it does not mean we should/must allow this for the Point.OfFloat
returned here, so an API using Point.OfFloat might consequently simply never use the integer variants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with that.
At a minimum the behavior must be documented (e.g. that you always get a fresh copy you are free to modify/use in any way).
@amartya4256 can you please mention it in the JavaDocs? Something like "Returns a new instance..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure.
b45d40f
to
ac95b91
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some questions on this proposal:
- Are the methods supposed to be public API or shall they only be used internal for now? In the latter case, they need to be marked
@noreference
. Otherwise, why do they need to be static at all and cannot just betoFloat()
, as they become public API anyway? But since the classes themselves are currently@noreference
, this method should probably be as well. In any case, they need a documentation. - Since the methods are supposed to create OfFloat versions out of any other, wouldn't it make sense to place them inside
Point.OfFloat
, so that you writePoint.OfFloat.From(something)
instead ofPoint.from(something)
but giving you an OfFloat instead of a pure Point. - Is it correct and expected that a
Point.WithMonitor
loses the monitor information when passed to that method?
We do not need @noreference since the classes are already marked with it. Adding the documentation to the method.
They are already inside Point.OfFloat.
Adapting the method to call clone internally. |
0712c9f
to
36d25ab
Compare
I see. I expected them to be in You need to add an |
This commit adds Rectangle.OfFloat.from and Point.OfFloat.from methods and removes the Win32DPIUtils.FloatAwareGeometryFactory class to make these methods OS-independent. It additionally adds clone methods in Point class and sub-classes and Rectangle.OfFloat::clone.
36d25ab
to
59ed121
Compare
This PR adds Rectangle.OfFloat.from and Point.OfFloat.from methods and removes the Win32DPIUtils.FloatAwareGeometryFactory class to make these methods OS-independent.