Skip to content

Commit 6f8a872

Browse files
authored
chore: provider may not be null (#38)
* fix: cache provider may not be null * chore: don't log full ClassNotFound exception in provider discovery * feat: add NotNull getter for provider * feat: throw MisconfiguredCacheException for all CacheSpec errors
1 parent f860aff commit 6f8a872

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

core/src/main/java/io/github/xanthic/cache/core/CacheApiSettings.java

+2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ private static void populateProviders(CacheApiSettings cacheApiSettings) {
128128
Class<? extends CacheProvider> clazz = Class.forName(providerClass).asSubclass(CacheProvider.class);
129129
cacheApiSettings.registerCacheProvider(clazz, null); // lazy, init if needed
130130
registered.incrementAndGet();
131+
} catch (ClassNotFoundException cx) {
132+
log.trace("Xanthic: Could not find optional cache provider " + providerClass);
131133
} catch (Exception e) {
132134
log.trace("Xanthic: Could not find optional cache provider " + providerClass, e);
133135
}

core/src/main/java/io/github/xanthic/cache/core/CacheApiSpec.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,21 @@ public final class CacheApiSpec<K, V> implements ICacheSpec<K, V> {
4444

4545
private ScheduledExecutorService executor;
4646

47+
@NotNull
48+
public CacheProvider provider() {
49+
// noinspection ConstantConditions
50+
return provider != null ? provider : CacheApiSettings.getInstance().getDefaultCacheProvider();
51+
}
52+
4753
/**
4854
* Ensures the configured specification is valid.
4955
*
5056
* @throws NullPointerException if a provider is not specified and no default provider has been set
5157
* @throws MisconfiguredCacheException if the cache settings are invalid (e.g., negative max size or expiry time)
5258
*/
5359
public void validate() {
54-
Objects.requireNonNull(provider, "provider may not be null!");
60+
if (provider == null)
61+
throw new MisconfiguredCacheException("provider must not be null! You have not set a provider and no default cache provider was found - see https://xanthic.github.io/provider/ for instructions on how to add cache providers to your project.");
5562

5663
if (maxSize != null && maxSize < 0)
5764
throw new MisconfiguredCacheException("maxSize may not be negative!");
@@ -80,7 +87,7 @@ public static <K, V> CacheApiSpec<K, V> process(@NotNull Consumer<CacheApiSpec<K
8087
spec.accept(data);
8188

8289
// noinspection ConstantConditions
83-
if (data.provider() == null) {
90+
if (data.provider == null) {
8491
// set / init default cache provider if nothing is set
8592
data.provider(CacheApiSettings.getInstance().getDefaultCacheProvider());
8693
log.debug("No cache provider explicitly specified; cache defaults to {}!", data.provider.getClass().getCanonicalName());

kotlin/src/main/kotlin/io/github/xanthic/cache/ktx/CacheApiSpecExtensions.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fun <K, V> processSpec(init: CacheApiSpec<K, V>.() -> Unit): CacheApiSpec<K, V>
2121
/**
2222
* @see io.github.xanthic.cache.api.ICacheSpec.provider
2323
*/
24-
var <K, V> CacheApiSpec<K, V>.provider: CacheProvider?
24+
var <K, V> CacheApiSpec<K, V>.provider: CacheProvider
2525
get() = this.provider()
2626
set(value) {
2727
this.provider(value)

0 commit comments

Comments
 (0)