TheAlgorithms/Java · error · IllegalArgumentException

Capacity must be > 0

Error message

Capacity must be > 0

What it means

Thrown by the LIFOCache.Builder constructor when capacity <= 0. Capacity bounds the internal structures and underpins eviction; a non-positive capacity makes the cache unable to hold entries. The guard runs in the constructor, so no builder is created in an invalid state.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java:504

     * 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 LIFOCache.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

  1. Default to a positive capacity (e.g. 256) when config is absent.
  2. Validate config at startup and fail fast.
  3. Compute defensively: int cap = Math.max(1, configuredCap).

Example fix

// before
new LIFOCache.Builder<>(config.getCacheSize())
// after
int cap = config.getCacheSize();
if (cap <= 0) throw new IllegalStateException("cache.size must be > 0, got " + cap);
new LIFOCache.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 LIFOCache.Builder<K, V>(cap);

Type guard

static boolean isValidCapacity(int capacity) {
    return capacity > 0;
}

Prevention

When it happens

Trigger: new LIFOCache.Builder<>(0); new LIFOCache.Builder<>(-5); capacity from config defaulting to 0 or computed as 0.

Common situations: Optional config defaulting to 0; capacity from memory/entrySize math that underflows; tests at capacity 0.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/d36671c4aac38b2b. Report an issue: GitHub.