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

Add LazyEncodingRegistry #24

Merged
merged 8 commits into from
May 16, 2023
Merged

Add LazyEncodingRegistry #24

merged 8 commits into from
May 16, 2023

Conversation

blackdiz
Copy link
Contributor

@blackdiz blackdiz commented May 4, 2023

A lazy initialization implementation of EncodingRegistry. It does not register any encoding until either the getEncoding(EncodingType) or getEncoding(String) method is called.
When one of these methods is called, the requested EncodingType is registered.

@blackdiz blackdiz requested a review from tox-p as a code owner May 4, 2023 09:05
A lazy initialization implementation of EncodingRegistry. It does not register any encoding until either the getEncoding(EncodingType) or getEncoding(String) method is called.
When one of these methods is called, the requested EncodingType is registered.
Copy link
Contributor

@tox-p tox-p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this issue :) I think there are some minor problems that we need to get fixed before merging this

There are some more details to it, than I thought. I commented them inline. It would be great, if you could also write tests for the new LazyEncodingRegistry to ensure that the lazy loading works for every case and no regressions creep in

@@ -10,12 +10,21 @@ public final class Encodings {
*
* @return the new {@link EncodingRegistry}
*/
public static EncodingRegistry newDefaultEncodingRegistry() {
final DefaultEncodingRegistry registry = new DefaultEncodingRegistry();
public static EncodingRegistry newEagerEncodingRegistry() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep the newDefaultEncodingRegistry naming here in the API. While your naming is more expressive, it will break every consumer of this library on upgrade, which I would like to avoid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I forgot that this is a library. I'll change the name back to its original one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🙂


@Override
public Optional<Encoding> getEncoding(String encodingName) {
addEncoding(EncodingType.valueOf(encodingName));
Copy link
Contributor

@tox-p tox-p May 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getEncoding(String) method is the only method that registered custom encodings can be retrieved with. In this current implementation, it will throw, if encodingName does not correspond to a pre-defined encoding. This leads to the LazyEncodingRegistry being unusable with custom encodings, which is not what we want 😄

The flow of this method should be:

  • Check if a encoding for that string is registered (if yes, return it)
  • If not, check if the encodingName matches a pre-defined encoding
  • If yes, re-use the lazy initialization flow of the getEncoding(EncodingType) method
  • It not, return an empty optional

@blackdiz blackdiz requested a review from tox-p May 5, 2023 13:47
@blackdiz blackdiz marked this pull request as draft May 5, 2023 13:50
Prevent from loading an encoding multiple times.
This method is for getting an EncodingType from the encoding type name.
- Create LazyEncodingRegistryTest for testing LazyEncodingRegistry.
- Refactor tests, create BaseEncodingRegistryTest for reusing tests for LazyEncodingRegistryTest and DefaultEncodingRegistryTest.
@blackdiz blackdiz marked this pull request as ready for review May 14, 2023 08:25
@blackdiz
Copy link
Contributor Author

Hi, @tox-p, in addition to the suggestions you provided, I have also created a new BaseEncodingRegistryTest to reuse tests for both LazyEncodingRegistryTest and DefaultEncodingRegistryTest.

Copy link
Contributor

@tox-p tox-p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the tests and changes :) As written in the in-line comment, I think the fastest way forward is to merge it as is and I make the one little change that I found myself :)

I'll try to push a new release version today :)

return encoding;
}

addEncoding(EncodingType.fromName(encodingName).orElseThrow(() -> new IllegalArgumentException("Unknown encoding type: " + encodingName)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not throw but instead return an empty optional to keep the method contract ("if there is no custom encoding with that name and there is no predefined encoding with that name, return optional#empty")

To prevent further roundtrips, I'm gonna make this little change after merge :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thank you!

@tox-p tox-p merged commit 653e3c1 into knuddelsgmbh:main May 16, 2023
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.

2 participants