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

Contravariant arguments for CaffeineProxyManager #356

Merged
merged 1 commit into from Mar 6, 2023

Conversation

namhptran
Copy link
Contributor

Hi, using CaffeineProxyManager has a problem of having to cast using raw types which may not be type safe. This should improve the experience and provide some type safety for custom settings applied to the cache.

@vladimir-bukhtoyarov
Copy link
Collaborator

Hello @namhptran

Could you provide example of code when you faced with unchecked cast?

@namhptran
Copy link
Contributor Author

namhptran commented Mar 6, 2023

@vladimir-bukhtoyarov the test that is already there is very similar to what I had to do to use Caffeine

protected ProxyManager<String> getProxyManager() {
  Caffeine<String, RemoteBucketState> builder = (Caffeine) Caffeine.newBuilder().maximumSize(100);
  return new CaffeineProxyManager<>(builder, Duration.ofMinutes(1));
}

This however provides warnings Unchecked assignment: 'com.github.benmanes.caffeine.cache.Caffeine' to 'com.github.benmanes.caffeine.cache.Caffeine<java.lang.String,io.github.bucket4j.distributed.remote.RemoteBucketState>' due to casting to raw type then unchecked casting back to a generic.

Alternatively, this was my original desired approach, but it doesn't work when it really should

protected ProxyManager<String> getProxyManager() {
  return new CaffeineProxyManager<>(Caffeine.newBuilder().maximumSize(100), Duration.ofMinutes(1));
}

This doesn't compile due to trying to assign Caffeine<Object, Object> to a Caffeine<K, RemoteBucketState>

Ideally CaffeineProxyManager should allow for contravariance on the Caffeine builder type, and we should avoid having to cast to raw types where possible

@namhptran namhptran changed the title Improve generic arguments for CaffeineProxyManager Contravariant arguments for CaffeineProxyManager Mar 6, 2023
@vladimir-bukhtoyarov vladimir-bukhtoyarov merged commit a78b6cd into bucket4j:master Mar 6, 2023
@vladimir-bukhtoyarov
Copy link
Collaborator

Looks reasonable. Thank you.

@vladimir-bukhtoyarov
Copy link
Collaborator

Released with version 8.3.0

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