TheAlgorithms/Java · error · IllegalArgumentException
Capacity must be > 0
Error message
Capacity must be > 0
What it means
Thrown by the FIFOCache.Builder constructor when capacity <= 0. Capacity bounds the internal HashMap and is fundamental to eviction logic; a non-positive capacity would make the cache unable to hold any entry. The check runs in the constructor, so no builder is created with an invalid capacity.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java:490
* strategy. Call {@link #build()} to create the configured cache instance.
*
* @param <K> the type of keys maintained by the cache
* @param <V> the type of values stored in the cache
*/
public static class Builder<K, V> {
private final int capacity;
private long defaultTTL = 0;
private BiConsumer<K, V> evictionListener;
private EvictionStrategy<K, V> evictionStrategy = new FIFOCache.ImmediateEvictionStrategy<>();
/**
* Creates a new {@code Builder} with the specified cache capacity.
*
* @param capacity the maximum number of entries the cache can hold; must be > 0
* @throws IllegalArgumentException if {@code capacity} is less than or equal to 0
*/
public Builder(int capacity) {
if (capacity <= 0) {
throw new IllegalArgumentException("Capacity must be > 0");
}
this.capacity = capacity;
}
/**
* Sets the default time-to-live (TTL) in milliseconds for cache entries.
*
* @param ttlMillis the TTL duration in milliseconds; must be >= 0
* @return this builder instance for chaining
* @throws IllegalArgumentException if {@code ttlMillis} is negative
*/
public Builder<K, V> defaultTTL(long ttlMillis) {
if (ttlMillis < 0) {
throw new IllegalArgumentException("Default TTL must be >= 0");
}
this.defaultTTL = ttlMillis;
return this;
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Default to a positive capacity (e.g. 256 or 1000) when config is missing.
- Validate the config value at startup and fail fast with a descriptive message.
- Compute capacity defensively: int cap = Math.max(1, configuredCap).
Example fix
// before
new FIFOCache.Builder<>(config.getCacheSize())
// after
int cap = config.getCacheSize();
if (cap <= 0) throw new IllegalStateException("cache.cacheSize must be > 0, got " + cap);
new FIFOCache.Builder<>(cap) Defensive patterns
Strategy: validation
Validate before calling
int cap = configuredCapacity;
if (cap <= 0) throw new IllegalStateException("cache.capacity must be > 0, got " + cap);
new FIFOCache.Builder<K, V>(cap); Type guard
static boolean isValidCapacity(int capacity) {
return capacity > 0;
} Prevention
- Default optional capacity config to a sane positive value.
- Validate capacity at startup rather than at first cache operation.
- Guard capacity math (memory/entrySize) against underflow to zero.
When it happens
Trigger: new FIFOCache.Builder<>(0); new FIFOCache.Builder<>(-10); capacity sourced from a config value or property that defaulted to 0 or was not set.
Common situations: Optional config that defaults to 0; capacity derived from maxMemory / entrySize where entrySize is mis-estimated to be larger than memory; tests that construct a builder with capacity 0 expecting an empty cache.
Related errors
- Default TTL must be >= 0
- Listener must not be null
- Eviction strategy must not be null
- Capacity must be > 0
- Key must not be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/cf15e0e6644f66d0.
Report an issue: GitHub.