Skip to content
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

Replace usage of Maps.newHashMapWithExpectedSize() with standard Java #2976

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

HannesWell
Copy link
Member

In Java-19 there is even static factory method HashMap.newHashMap(int) that can be used as a drop-in replacement once Java-19 or later is required.
For now I calculate the necessary capacity explicitly using the formula: expected-size *4/3+1.

The change in XtextGeneratorTemplates also removes the usage of Maps.newHashMapWithExpectedSize() of many generated Activators, but I haven't changed them manually. I assume they should be regenerated, shouldn't they?

Part of #2975

@LorenzoBettini
Copy link
Contributor

I think @szarnekow should review this, but personally, I'm not a big fan of such strange expressions for computing the size ;)

another note: Maps.of returns an immutable map, IIRC. Could this potentially lead to breakage if the current factory method returns a mutable list?

@HannesWell
Copy link
Member Author

I think @szarnekow should review this, but personally, I'm not a big fan of such strange expressions for computing the size ;)

Is it about the expression or specifying the size at all?

If it is the latter, I can also remove it. If it is the former I'm fine to restrict this PR to the change of XtextGeneratorTemplates.xtend, which is 'required' for eclipse/mwe#287 (comment) or at least the changes that use only constants. I mainly did the others for completeness.
The remaining parts could then be replaced as soon as Java-21 is required using the method HashMap.newHashMap(int) introduced in Java-19 as mentioned in my initial message.

another note: Maps.of returns an immutable map, IIRC. Could this potentially lead to breakage if the current factory method returns a mutable list?

That's right. But I have only used it in two places where the map is passed as userData to EObjectDescription.create(). And if I have followed the code-path correctly that map is only read there.

@HannesWell HannesWell force-pushed the replace-Maps_newHashMapWithExpectedSize branch from 4394639 to 85ca11f Compare April 12, 2024 23:22
@HannesWell HannesWell marked this pull request as ready for review April 12, 2024 23:22
@HannesWell
Copy link
Member Author

I have now reverted the changes that require computation of the capacity. That can be replaced once the Java-19 HashMap.newHashMap(int) is available for Xtext.
The main change for this PR in XtextGeneratorTemplates.xtend is not affected by that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants