Skip to content

Commit

Permalink
Refactor and fix compiler warnings in JedisExtension.
Browse files Browse the repository at this point in the history
  • Loading branch information
jxblum committed Sep 20, 2023
1 parent 4a47976 commit c402a20
Showing 1 changed file with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* callbacks. The following resource types are supported by this extension:
* <ul>
* <li>{@link Jedis} (singleton)</li>
* <li>{@link JediCluster} (singleton)</li>
* <li>{@link JedisCluster} (singleton)</li>
* </ul>
*
* <pre class="code">
Expand All @@ -66,6 +66,7 @@
* is managed by this extension.
*
* @author Mark Paluch
* @author John Blum
* @see ParameterResolver
* @see BeforeEachCallback
*/
Expand All @@ -86,18 +87,14 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
}

/**
* Attempt to resolve the {@code requestedResourceType}.
*
* @param extensionContext
* @param requestedResourceType
* @param <T>
* @return
* Attempt to resolve the {@link Class requestedResourceType}.
*/
@SuppressWarnings("unchecked")
public <T> T resolve(ExtensionContext extensionContext, Class<T> requestedResourceType) {

ExtensionContext.Store store = getStore(extensionContext);

return (T) store.getOrComputeIfAbsent(requestedResourceType, it -> findSupplier(requestedResourceType).get());
return (T) store.getOrComputeIfAbsent(requestedResourceType, it -> doGetInstance(requestedResourceType));
}

@Override
Expand All @@ -111,6 +108,10 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
return store.getOrComputeIfAbsent(parameter.getType(), it -> doGetInstance(parameterizedType));
}

private Object doGetInstance(Type parameterizedType) {
return findSupplier(parameterizedType).get();
}

@SuppressWarnings("unchecked")
private static Supplier<Object> findSupplier(Type type) {

Expand All @@ -120,30 +121,26 @@ private static Supplier<Object> findSupplier(Type type) {

ResolvableType providedType = ResolvableType.forType(it.getClass()).as(Supplier.class).getGeneric(0);

if (requested.isAssignableFrom(providedType)) {
return true;
}
return false;
return requested.isAssignableFrom(providedType);

}).findFirst().orElseThrow(() -> new NoSuchElementException("Cannot find a factory for " + type));

return (Supplier) supplier;
}

private Object doGetInstance(Type parameterizedType) {
return findSupplier(parameterizedType).get();
}

private ExtensionContext.Store getStore(ExtensionContext extensionContext) {
return extensionContext.getStore(NAMESPACE);
}

static class ResourceFunction {

final Function<Object, Object> function;

final ResolvableType dependsOn;
final ResolvableType provides;
final Function<Object, Object> function;

public ResourceFunction(ResolvableType dependsOn, ResolvableType provides, Function<?, ?> function) {

this.dependsOn = dependsOn;
this.provides = provides;
this.function = (Function) function;
Expand All @@ -155,9 +152,11 @@ enum JedisSupplier implements Supplier<Jedis> {
INSTANCE;

final Lazy<Jedis> lazy = Lazy.of(() -> {

Jedis client = new Jedis(SettingsUtils.getHost(), SettingsUtils.getPort());

ShutdownQueue.INSTANCE.register(client);
ShutdownQueue.register(client);

return client;
});

Expand All @@ -172,9 +171,13 @@ enum JedisClusterSupplier implements Supplier<JedisCluster> {
INSTANCE;

final Lazy<JedisCluster> lazy = Lazy.of(() -> {
JedisCluster client = new JedisCluster(new HostAndPort(SettingsUtils.getHost(), SettingsUtils.getClusterPort()));

ShutdownQueue.INSTANCE.register(client);
HostAndPort hostAndPort = new HostAndPort(SettingsUtils.getHost(), SettingsUtils.getClusterPort());

JedisCluster client = new JedisCluster(hostAndPort);

ShutdownQueue.register(client);

return client;
});

Expand All @@ -183,5 +186,4 @@ public JedisCluster get() {
return lazy.get();
}
}

}

0 comments on commit c402a20

Please sign in to comment.