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

Feature/114 collection copying only when changed #118

Conversation

freelon
Copy link
Contributor

@freelon freelon commented Jun 13, 2022

For #114 again.

Instead of using explicit booleans to track if a collection was changed I track it by the type of the collection.
For that, I added custom mutable private collections (subclassing ArrayList, HashSet, and HashMap) to the builder. This makes it explicitly known for us whether it's "our" mutable collection or if it comes from the outside.

@Randgalt
Copy link
Owner

Randgalt commented Jun 13, 2022

I just did an experiment. I don't think the shim classes are needed. Here's what ensureListMutable could look like:

    private static <T> List<T> __ensureListMutable(List<T> o) {
        if (o == null) return new ArrayList<T>(){};
        if (o instanceof ArrayList && FullRecordBuilder.class.equals(o.getClass().getEnclosingClass()) return o;
        return new ArrayList<>(o){};
    }

The idea is to return an anonymous nested class which takes advantage of the fact that getEnclosingClass() will return the parent builder class.

@freelon
Copy link
Contributor Author

freelon commented Jun 14, 2022

That'd work, too. But I think it hides its meaning quite well (at least I wouldn't get the purpose from looking at the code). Maybe it would make it even more clear if the classes were named, e.g. ModifiedList.

@Randgalt
Copy link
Owner

That'd work, too. But I think it hides its meaning quite well (at least I wouldn't get the purpose from looking at the code). Maybe it would make it even more clear if the classes were named, e.g. ModifiedList.

I'd rather not introduce yet more types in the builder class as it's getting very complex already. While this might be harder to read it's much easier to maintain as it's all located within the method. So, this is my preference.

@freelon
Copy link
Contributor Author

freelon commented Jun 16, 2022

I tried it with the inner anonymous class, technically of course it works, but I'm getting a problem with jacoco then:
Rule violated for bundle record-builder-test: complexity covered ratio is 0.00, but expected minimum is 0.60
It is introduced by the anonymous class, which of course doesn't have a lot of coverage, but since it's anonymous I don't know how to annotate it as @...Generated, so that the coverage test ignores it.

@freelon
Copy link
Contributor Author

freelon commented Jun 16, 2022

In case there is not a smart way to circumvent that coverage thing, I'd suggest to just call the shim methods in the constructor then. If another constructor was created for whatever reason it would just have to be made sure, that the shim methods were called there as well.
That would be an easy and clean way out.

@Randgalt
Copy link
Owner

Randgalt commented Jun 18, 2022

but I'm getting a problem with jacoco then

Farg - I forgot about Jacoco 🤦🏻

OK - then let's stay with your change. However, the class names need to be added to RecordBuilder.Options like the others to be consistent. Can you add that please and then this can be re-merged. Thank you.

@freelon
Copy link
Contributor Author

freelon commented Jun 19, 2022

Here you go!

Out of curiosity: why do you prefer having the names of internal/private classes configurable?

@Randgalt
Copy link
Owner

Randgalt commented Jun 19, 2022

Out of curiosity: why do you prefer having the names of internal/private classes configurable?

The builder classed can be subclassed. So, this is more for consistency than anything at this point. Maybe we should consider removing the configurable names for private classes.

@Randgalt
Copy link
Owner

Looks good - thank you. Please squash to a single commit and I'll merge.

@freelon freelon force-pushed the feature/114-collection-copying-only-when-changed branch from 7699796 to 2db2f1a Compare June 19, 2022 19:52
@freelon
Copy link
Contributor Author

freelon commented Jun 19, 2022

@Randgalt ready to merge 🎉

@freelon freelon force-pushed the feature/114-collection-copying-only-when-changed branch from 2db2f1a to 84c5300 Compare June 19, 2022 20:04
@Randgalt Randgalt merged commit aa072af into Randgalt:master Jun 19, 2022
@Randgalt Randgalt added this to the v34 milestone Jun 19, 2022
@freelon freelon deleted the feature/114-collection-copying-only-when-changed branch June 20, 2022 11:09
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

2 participants